Skip to content

Commit 7fe490c

Browse files
committed
fix(maxlength): Fix error message for maxlength, tidy grammar in README
1 parent bbc9538 commit 7fe490c

File tree

8 files changed

+69
-63
lines changed

8 files changed

+69
-63
lines changed

README.md

+37-39
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# class-validator
22

3-
Allows to use decorator and non-decorator based validation. Internally uses [validator.js][1] to perform validation.
3+
Allows use of decorator and non-decorator based validation. Internally uses [validator.js][1] to perform validation.
44

55
## Installation
66

@@ -67,7 +67,7 @@ validate(post).then(errors => { // errors is an array of validation errors
6767
## Validation messages
6868

6969
You can specify validation message in the decorator options and that message will be returned in `ValidationError`
70-
returned by `validate` method in the case if validation for this field fail.
70+
returned by `validate` method in the case that validation for this field fails.
7171

7272
```typescript
7373
import {MinLength, MaxLength} from "class-validator";
@@ -85,9 +85,9 @@ export class Post {
8585
```
8686

8787
There are few special tokens you can use in your messages:
88-
* `$value` - the value that is being validated right now
89-
* `$property` - name of the object's property being validated right now
90-
* `$target` - name of the object's class being validated right now
88+
* `$value` - the value that is being validated
89+
* `$property` - name of the object's property being validated
90+
* `$target` - name of the object's class being validated
9191
* `$constraint1`, `$constraint2`, ... `$constraintN` - constraints defined by specific validation type
9292

9393
Example of usage:
@@ -117,9 +117,9 @@ export class Post {
117117
@MinLength(10, {
118118
message: (args: ValidationArguments) => {
119119
if (args.value.length === 1) {
120-
return "Too short, minimal length is 1 character";
120+
return "Too short, minimum length is 1 character";
121121
} else {
122-
return "Too short, minimal length is " + args.constraints[0] + " characters";
122+
return "Too short, minimum length is " + args.constraints[0] + " characters";
123123
}
124124
}
125125
})
@@ -128,13 +128,11 @@ export class Post {
128128
```
129129

130130
Message function accepts `ValidationArguments` which contains following information:
131-
* `value` - the value that is being validated right now
131+
* `value` - the value that is being validated
132132
* `constraints` - array of constraints defined by specific validation type
133-
* `targetName` - name of the object's class being validated right now
133+
* `targetName` - name of the object's class being validated
134134
* `object` - object that is being validated
135-
* `property` - name of the object's property being validated right now
136-
137-
Values are being passed to this function, so
135+
* `property` - name of the object's property being validated
138136

139137
## Validating arrays
140138

@@ -157,8 +155,8 @@ This will validate each item in `post.tags` array.
157155

158156
## Validating nested objects
159157

160-
If your object contains nested objects and you want validator to perform their validation too, then you need to
161-
use `@ValidateNested()` decorator:
158+
If your object contains nested objects and you want the validator to perform their validation too, then you need to
159+
use the `@ValidateNested()` decorator:
162160

163161
```typescript
164162
import {ValidateNested} from "class-validator";
@@ -176,7 +174,7 @@ export class Post {
176174
Sometimes you may want to skip validation of the properties that does not exist in the validating object. This is
177175
usually desirable when you want to update some parts of the object, and want to validate only updated parts,
178176
but skip everything else, e.g. skip missing properties.
179-
In such situations you need to pass a special flag to `validate` method:
177+
In such situations you will need to pass a special flag to `validate` method:
180178

181179
```typescript
182180
import {validate} from "class-validator";
@@ -232,9 +230,9 @@ validate(user, {
232230

233231
## Custom validation classes
234232

235-
If you have custom validation logic you have a way to do it - you can create a *Constraint class*:
233+
If you have custom validation logic you can create a *Constraint class*:
236234

237-
1. First create a file, lets say `CustomTextLength.ts`, and create there a new class:
235+
1. First create a file, lets say `CustomTextLength.ts`, and define a new class:
238236

239237
```typescript
240238
import {ValidatorConstraint, ValidatorConstraintInterface, ValidationArguments} from "class-validator";
@@ -258,12 +256,12 @@ If you have custom validation logic you have a way to do it - you can create a *
258256
If you will not supply a constraint name - it will be auto-generated.
259257

260258
Our class must implement `ValidatorConstraintInterface` interface and its `validate` method,
261-
which defines validation logic. If validation succeed method return true, otherwise false.
259+
which defines validation logic. If validation succeeds, method returns true, otherwise false.
262260
Custom validator can be asynchronous, if you want to perform validation after some asynchronous
263261
operations, simply return a promise with boolean inside in `validate` method.
264262

265263
Also we defined optional method `defaultMessage` which defines a default error message,
266-
in the case if decorator's user didn't set error message.
264+
in the case that the decorator's implementation doesn't set an error message.
267265

268266

269267
2. Then you can use your new validation constraint in your class:
@@ -294,7 +292,7 @@ If you have custom validation logic you have a way to do it - you can create a *
294292
});
295293
```
296294

297-
You can also send constraints to your validator, like this:
295+
You can also pass constraints to your validator, like this:
298296

299297
```typescript
300298
import {Validate} from "class-validator";
@@ -328,16 +326,16 @@ export class CustomTextLength implements ValidatorConstraintInterface {
328326
## Custom validation decorators
329327

330328
You can also create a custom decorators. Its the most elegant way of using a custom validations.
331-
Lets create a decorator called `@IsLongerThen`:
329+
Lets create a decorator called `@IsLongerThan`:
332330

333331
1. Create a decorator itself:
334332

335333
```typescript
336334
import {registerDecorator, ValidationOptions} from "class-validator";
337335
338-
export function IsLongerThen(property: string, validationOptions?: ValidationOptions) {
336+
export function IsLongerThan(property: string, validationOptions?: ValidationOptions) {
339337
return function (object: Object, propertyName: string) {
340-
registerDecorator(object, propertyName, validationOptions, [property], "is_longer_then", (value, args) => {
338+
registerDecorator(object, propertyName, validationOptions, [property], "is_longer_than", (value, args) => {
341339
const [relatedPropertyName] = args.constraints;
342340
const relatedValue = (args.object as any)[relatedPropertyName];
343341
return typeof value === "string" &&
@@ -348,18 +346,18 @@ Lets create a decorator called `@IsLongerThen`:
348346
}
349347
```
350348

351-
2. Put it on use:
349+
2. Put it to use:
352350

353351
```typescript
354-
import {IsLongerThen} from "./IsLongerThen";
352+
import {IsLongerThan} from "./IsLongerThan";
355353
356354
export class Post {
357355
358356
title: string;
359357
360-
@IsLongerThen("title", {
358+
@IsLongerThan("title", {
361359
/* you can also use additional validation options, like "each", "groups" in your custom validation decorators */
362-
message: "Text must be longer the title"
360+
message: "Text must be longer than the title"
363361
})
364362
text: string;
365363
@@ -393,15 +391,15 @@ Lets create another custom validation decorator called `IsUserAlreadyExist`:
393391
}
394392
```
395393

396-
2. And put it on use:
394+
2. And put it to use:
397395

398396
```typescript
399397
import {IsUserAlreadyExist} from "./IsUserAlreadyExist";
400398
401399
export class User {
402400
403401
@IsUserAlreadyExist({
404-
message: "User $value already exist. Choose another name."
402+
message: "User $value already exists. Choose another name."
405403
})
406404
name: string;
407405
@@ -455,8 +453,8 @@ validator.isInt(value); // Checks if value is an integer.
455453
validator.isDivisibleBy(value, num); // Checks if value is a number that's divisible by another.
456454
validator.isPositive(value); // Checks if the value is a positive number.
457455
validator.isNegative(value); // Checks if the value is a negative number.
458-
validator.max(num, max); // Checks if the first number is greater then second.
459-
validator.min(num, min); // Checks if the first number is less then second.
456+
validator.max(num, max); // Checks if the first number is greater than second.
457+
validator.min(num, min); // Checks if the first number is less than second.
460458
461459
// date validation methods
462460
validator.minDate(date, minDate); // Checks if the value is a date that's after the specified date.
@@ -498,16 +496,16 @@ validator.isURL(str, options); // Checks if the string is an url.
498496
validator.isUUID(str, version); // Checks if the string is a UUID (version 3, 4 or 5).
499497
validator.isUppercase(str); // Checks if the string is uppercase.
500498
validator.length(str, min, max); // Checks if the string's length falls in a range.
501-
validator.minLength(str, min); // Checks if the string's length is not less then given number.
502-
validator.maxLength(str, max); // Checks if the string's length is not more then given number.
499+
validator.minLength(str, min); // Checks if the string's length is not less than given number.
500+
validator.maxLength(str, max); // Checks if the string's length is not more than given number.
503501
validator.matches(str, pattern, modifiers); // Checks if string matches the pattern. Either matches('foo', /foo/i) or matches('foo', 'foo', 'i').
504502
505503
// array validation methods
506504
validator.arrayContains(array, values); // Checks if array contains all values from the given array of values.
507505
validator.arrayNotContains(array, values); // Checks if array does not contain any of the given values.
508506
validator.arrayNotEmpty(array); // Checks if given array is not empty.
509-
validator.arrayMinSize(array, min); // Checks if array's length is as minimal this number.
510-
validator.arrayMaxSize(array, max); // Checks if array's length is as maximal this number.
507+
validator.arrayMinSize(array, min); // Checks if array's length is at least `min` number.
508+
validator.arrayMaxSize(array, max); // Checks if array's length is as most `max` number.
511509
validator.arrayUnique(array); // Checks if all array's values are unique. Comparison for objects is reference-based.
512510
```
513511

@@ -533,8 +531,8 @@ validator.arrayUnique(array); // Checks if all array's values are unique. Compar
533531
| `@IsDivisibleBy(num: number)` | Checks if the value is a number that's divisible by another. |
534532
| `@IsPositive()` | Checks if the value is a positive number. |
535533
| `@IsNegative()` | Checks if the value is a negative number. |
536-
| `@Max(max: number)` | Checks if the given number is greater then given number. |
537-
| `@Min(min: number)` | Checks if the given number is less then given number. |
534+
| `@Max(max: number)` | Checks if the given number is greater than given number. |
535+
| `@Min(min: number)` | Checks if the given number is less than given number. |
538536
| **Date validation decorators** |
539537
| `@MinDate(date: Date)` | Checks if the value is a date that's after the specified date. |
540538
| `@MaxDate(date: Date)` | Checks if the value is a date that's before the specified date. | |
@@ -574,8 +572,8 @@ validator.arrayUnique(array); // Checks if all array's values are unique. Compar
574572
| `@IsUUID(version?: "3"|"4"|"5")` | Checks if the string is a UUID (version 3, 4 or 5). |
575573
| `@IsUppercase()` | Checks if the string is uppercase. |
576574
| `@Length(min: number, max?: number)` | Checks if the string's length falls in a range. |
577-
| `@MinLength(min: number)` | Checks if the string's length is not less then given number. |
578-
| `@MaxLength(max: number)` | Checks if the string's length is not more then given number. |
575+
| `@MinLength(min: number)` | Checks if the string's length is not less than given number. |
576+
| `@MaxLength(max: number)` | Checks if the string's length is not more than given number. |
579577
| `@Matches(pattern: RegExp, modifiers?: string)` | Checks if string matches the pattern. Either matches('foo', /foo/i) or matches('foo', 'foo', 'i'). |
580578
| **Array validation decorators** |
581579
| `@ArrayContains(values: any[])` | Checks if array contains all values from the given array of values. |

sample/sample6-custom-decorator/IsLongerThen.ts sample/sample6-custom-decorator/IsLongerThan.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import {ValidatorConstraintInterface} from "../../src/validation/ValidatorConstr
44
import {ValidatorConstraint} from "../../src/decorator/decorators";
55
import {ValidationArguments} from "../../src/validation/ValidationArguments";
66

7-
export function IsLongerThen(property: string, validationOptions?: ValidationOptions) {
7+
export function IsLongerThan(property: string, validationOptions?: ValidationOptions) {
88
return function (object: Object, propertyName: string) {
9-
registerDecorator(object, propertyName, validationOptions, [property], IsLongerThenConstraint);
9+
registerDecorator(object, propertyName, validationOptions, [property], IsLongerThanConstraint);
1010
};
1111
}
1212

13-
@ValidatorConstraint("is_longer_then")
14-
export class IsLongerThenConstraint implements ValidatorConstraintInterface {
13+
@ValidatorConstraint("is_longer_than")
14+
export class IsLongerThanConstraint implements ValidatorConstraintInterface {
1515

1616
validate(value: any, args: ValidationArguments) {
1717
const [relatedPropertyName] = args.constraints;

sample/sample6-custom-decorator/User.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {IsUserAlreadyExist} from "./IsUserAlreadyExist";
2-
import {IsLongerThen} from "./IsLongerThen";
2+
import {IsLongerThan} from "./IsLongerThan";
33

44
export class User {
55

@@ -8,8 +8,8 @@ export class User {
88
})
99
firstName: string;
1010

11-
@IsLongerThen("firstName", {
12-
message: "User's last name must be longer then firstName"
11+
@IsLongerThan("firstName", {
12+
message: "User's last name must be longer than firstName"
1313
})
1414
lastName: string;
1515

src/decorator/decorators.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ export function IsNegative(validationOptions?: ValidationOptions) {
302302
};
303303
}
304304
/**
305-
* Checks if the given number is greater then given number.
305+
* Checks if the given number is greater than given number.
306306
*/
307307
export function Min(min: number, validationOptions?: ValidationOptions) {
308308
return function (object: Object, propertyName: string) {
@@ -318,7 +318,7 @@ export function Min(min: number, validationOptions?: ValidationOptions) {
318318
}
319319

320320
/**
321-
* Checks if the given number is less then given number.
321+
* Checks if the given number is less than given number.
322322
*/
323323
export function Max(max: number, validationOptions?: ValidationOptions) {
324324
return function (object: Object, propertyName: string) {
@@ -886,7 +886,7 @@ export function Length(min: number, max?: number, validationOptions?: Validation
886886
}
887887

888888
/**
889-
* Checks if the string's length is not less then given number. Note: this function takes into account surrogate pairs.
889+
* Checks if the string's length is not less than given number. Note: this function takes into account surrogate pairs.
890890
*/
891891
export function MinLength(min: number, validationOptions?: ValidationOptions) {
892892
return function (object: Object, propertyName: string) {
@@ -902,7 +902,7 @@ export function MinLength(min: number, validationOptions?: ValidationOptions) {
902902
}
903903

904904
/**
905-
* Checks if the string's length is not more then given number. Note: this function takes into account surrogate pairs.
905+
* Checks if the string's length is not more than given number. Note: this function takes into account surrogate pairs.
906906
*/
907907
export function MaxLength(max: number, validationOptions?: ValidationOptions) {
908908
return function (object: Object, propertyName: string) {

src/validation/ValidationTypes.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ export class ValidationTypes {
135135
case this.IS_NEGATIVE:
136136
return "$property must be a negative number";
137137
case this.MIN:
138-
return "$property must be greater then $constraint1";
138+
return "$property must be greater than $constraint1";
139139
case this.MAX:
140-
return "$property must be less then $constraint1";
140+
return "$property must be less than $constraint1";
141141

142142
/* date checkers */
143143
case this.MIN_DATE:
@@ -217,16 +217,16 @@ export class ValidationTypes {
217217
const isMinLength = args.constraints[0] !== null && args.constraints[0] !== undefined;
218218
const isMaxLength = args.constraints[1] !== null && args.constraints[1] !== undefined;
219219
if (isMinLength && (!args.value || args.value.length < args.constraints[0])) {
220-
return "$property must be longer then $constraint1";
220+
return "$property must be longer than $constraint1";
221221
} else if (isMaxLength && (args.value.length > args.constraints[1])) {
222-
return "$property must be shorter then $constraint2";
222+
return "$property must be shorter than $constraint2";
223223
}
224-
return "$property must be longer then $constraint1 and shorter then $constraint2";
224+
return "$property must be longer than $constraint1 and shorter than $constraint2";
225225
};
226226
case this.MIN_LENGTH:
227-
return "$property must be longer then $constraint1";
227+
return "$property must be longer than $constraint1";
228228
case this.MAX_LENGTH:
229-
return "$property must be shorter then $constraint2";
229+
return "$property must be shorter than $constraint1";
230230
case this.MATCHES:
231231
return "$property must match $constraint1 regular expression";
232232

@@ -240,7 +240,7 @@ export class ValidationTypes {
240240
case this.ARRAY_MIN_SIZE:
241241
return "$property must contain at least $constraint1 elements";
242242
case this.ARRAY_MAX_SIZE:
243-
return "$property must contain not more then $constraint1 elements";
243+
return "$property must contain not more than $constraint1 elements";
244244
case this.ARRAY_UNIQUE:
245245
return "All $property's elements must be unique";
246246
}

0 commit comments

Comments
 (0)