@@ -15,9 +15,11 @@ Internally uses [validator.js][1] to perform validation.
15
15
16
16
and use it somewhere in the global place of your app:
17
17
18
- * for nodejs (only need for older versions of node) : ` require("es6-shim") ` in your app's entry point (for example ` app.ts ` )
18
+ * for nodejs: ` require("es6-shim") ` in your app's entry point (for example in ` app.ts ` )
19
19
* for browser: ` <script src="path-to-shim/es6-shim.js"> ` in your ` index.html `
20
20
21
+ For node.js users this step is only required if you are using old versions of node.
22
+
21
23
## Usage
22
24
23
25
Create your class and put some validation decorators on its properties you want to validate:
@@ -54,7 +56,13 @@ post.rating = 11; // should not pass
54
56
post .email = " google.com" ; // should not pass
55
57
post .site = " googlecom" ; // should not pass
56
58
57
- let errors = validate (post ); // returns you array of errors
59
+ validate (post ).then (errors => {
60
+ if (errors .length > 0 ) {
61
+ console .log (" validation failed. errors: " , errors );
62
+ } else {
63
+ console .log (" validation succeed" );
64
+ }
65
+ }); // returns you array of errors
58
66
```
59
67
60
68
## Validation messages
@@ -282,35 +290,40 @@ import {Validator} from "class-validator";
282
290
283
291
// Validation methods
284
292
const validator = new Validator();
293
+
294
+ // common validation methods
285
295
validator.equals(value, comparison); // Checks if value matches ("===") the comparison.
286
296
validator.notEquals(value, comparison); // Checks if value does not match ("!==") the comparison.
287
297
validator.empty(value); // Checks if given value is empty (=== '', === null, === undefined).
288
298
validator.notEmpty(value); // Checks if given value is not empty (!== '', !== null, !== undefined).
289
299
validator.isIn(value, possibleValues); // Checks if given value is in a array of allowed values.
290
300
validator.isNotIn(value, possibleValues); // Checks if given value not in a array of allowed values.
291
301
302
+ // type validation methods
292
303
validator.isBoolean(value); // Checks if a given value is a real boolean.
293
304
validator.isDate(value); // Checks if a given value is a real date.
294
- validator.isNumber(value); // Checks if a given value is a real number.
295
305
validator.isString(value); // Checks if a given value is a real string.
296
-
297
- validator.divisibleBy(value, number); // Checks if value is a number that's divisible by another.
298
- validator.isDecimal(value); // Checks if value represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.
306
+ validator.isNumber(value); // Checks if a given value is a real number.
299
307
validator.isInt(value); // Checks if value is an integer.
308
+ validator.isDecimal(value); // Checks if value represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.
309
+
310
+ // number validation methods
311
+ validator.divisibleBy(value, num); // Checks if value is a number that's divisible by another.
300
312
validator.isPositive(value); // Checks if the value is a positive number.
301
313
validator.isNegative(value); // Checks if the value is a negative number.
302
- validator.greater (firstNumber, secondNumber); // Checks if the first number is greater then second.
303
- validator.less (firstNumber, secondNumber); // Checks if the first number is less then second.
314
+ validator.greaterThen (firstNumber, secondNumber); // Checks if the first number is greater then second.
315
+ validator.lessThen (firstNumber, secondNumber); // Checks if the first number is less then second.
304
316
317
+ // date validation methods
305
318
validator.minDate(date, minDate); // Checks if the value is a date that's after the specified date.
306
319
validator.maxDate(date, minDate); // Checks if the value is a date that's before the specified date.
307
320
308
- validator.matches(str, pattern, modifiers); // Checks if string matches the pattern. Either matches('foo', /foo/i) or matches('foo', 'foo', 'i').
309
-
321
+ // string-type validation methods
310
322
validator.isBooleanString(str); // Checks if a string is a boolean.
311
323
validator.isDateString(str); // Checks if the string is a date.
312
324
validator.isNumberString(str); // Checks if the string is numeric.
313
325
326
+ // string validation methods
314
327
validator.contains(str, seed); // Checks if the string contains the seed.
315
328
validator.notContains(str, seed); // Checks if the string does not contain the seed.
316
329
validator.isAlpha(str); // Checks if the string contains only letters (a-zA-Z).
@@ -343,49 +356,49 @@ validator.isUppercase(str); // Checks if the string is uppercase.
343
356
validator.length(str, min, max); // Checks if the string's length falls in a range.
344
357
validator.minLength(str, min); // Checks if the string's length is not less then given number.
345
358
validator.maxLength(str, max); // Checks if the string's length is not more then given number.
359
+ validator.matches(str, pattern, modifiers); // Checks if string matches the pattern. Either matches('foo', /foo/i) or matches('foo', 'foo', 'i').
346
360
361
+ // array validation methods
347
362
validator.arrayContains(array, values); // Checks if array contains all values from the given array of values.
348
363
validator.arrayNotContains(array, values); // Checks if array does not contain any of the given values.
349
364
validator.arrayNotEmpty(array); // Checks if given array is not empty.
350
365
validator.arrayMinSize(array, min); // Checks if array's length is as minimal this number.
351
366
validator.arrayMaxSize(array, max); // Checks if array's length is as maximal this number.
352
367
validator.arrayUnique(array); // Checks if all array's values are unique. Comparison for objects is reference-based.
353
-
354
368
` ` `
355
369
356
370
## Validation decorators
357
371
358
372
| Decorator | Description |
359
373
| ------------------------------------------------ - | ---------------------------------------------------------------------------------------------------- |
374
+ | ** Common validation decorators ** |
360
375
| ` @Equals(comparison: any) ` | Checks if value equals (" ===" ) comparison . |
361
376
| ` @NotEquals(comparison: any) ` | Checks if value not equal (" !==" ) comparison . |
362
377
| ` @Empty() ` | Checks if given value is empty (=== ' ' , === null , === undefined ). |
363
378
| ` @NotEmpty() ` | Checks if given value is not empty (!== ' ' , !== null , !== undefined ). |
364
379
| ` @In(values: any[]) ` | Checks if value is in a array of allowed values . |
365
380
| ` @NotIn(values: any[]) ` | Checks if value is not in a array of disallowed values . |
366
- | |
381
+ | ** Type validation decorators ** |
367
382
| ` @IsBoolean() ` | Checks if a value is a boolean . |
368
383
| ` @IsDate() ` | Checks if the string is a date . |
369
- | ` @IsNumber() ` | Checks if the string is a number . |
370
384
| ` @IsString() ` | Checks if the string is a string . |
371
- | |
372
- | ` @DivisibleBy(num: number) ` | Checks if the value is a number that ' s divisible by another. |
373
- | ` @IsDecimal() ` | Checks if the value represents a decimal number , such as 0.1 , .3 , 1.1 , 1.00003 , 4.0 , etc . |
385
+ | ` @IsNumber() ` | Checks if the string is a number . |
374
386
| ` @IsInt() ` | Checks if the value is an integer number . |
387
+ | ` @IsDecimal() ` | Checks if the value represents a decimal number , such as 0.1 , .3 , 1.1 , 1.00003 , 4.0 , etc . |
388
+ | ** Number validation decorators ** |
389
+ | ` @DivisibleBy(num: number) ` | Checks if the value is a number that ' s divisible by another. |
375
390
| ` @IsPositive() ` | Checks if the value is a positive number . |
376
391
| ` @IsNegative() ` | Checks if the value is a negative number . |
377
- | ` @Greater (num: number) ` | Checks if the given number is greater then given number . |
378
- | ` @Less (num: number) ` | Checks if the given number is less then given number . |
379
- | |
392
+ | ` @GreaterThen (num: number) ` | Checks if the given number is greater then given number . |
393
+ | ` @LessThen (num: number) ` | Checks if the given number is less then given number . |
394
+ | ** Date validation decorators ** |
380
395
| ` @MinDate(date: Date) ` | Checks if the value is a date that ' s after the specified date. |
381
- | ` @MaxDate(date: Date) ` | Checks if the value is a date that ' s before the specified date. |
382
- | |
383
- | ` @Matches(pattern: RegExp, modifiers?: string) ` | Checks if string matches the pattern . Either matches (' foo' , / foo/ i ) or matches (' foo' , ' foo' , ' i' ). |
384
- | |
396
+ | ` @MaxDate(date: Date) ` | Checks if the value is a date that ' s before the specified date. | |
397
+ | ** String - type validation decorators** |
385
398
| `@IsBooleanString()` | Checks if a string is a boolean (e.g. is "true" or "false"). |
386
399
| `@IsDateString()` | Checks if a string is a date. |
387
400
| `@IsNumberString()` | Checks if a string is a number. |
388
- | |
401
+ | **String validation decorators** |
389
402
| `@Contains(seed: string)` | Checks if the string contains the seed. |
390
403
| `@NotContains(seed: string)` | Checks if the string not contains the seed. |
391
404
| `@IsAlpha()` | Checks if the string contains only letters (a-zA-Z). |
@@ -419,13 +432,14 @@ validator.arrayUnique(array); // Checks if all array's values are unique. Compar
419
432
| `@Length(min: number, max?: number)` | Checks if the string's length falls in a range. |
420
433
| `@MinLength(min: number)` | Checks if the string's length is not less then given number. |
421
434
| `@MaxLength(max: number)` | Checks if the string's length is not more then given number. |
422
- | |
435
+ | `@Matches(pattern: RegExp, modifiers?: string)` | Checks if string matches the pattern. Either matches('foo', /foo/i) or matches('foo', 'foo', 'i'). |
436
+ | **Array validation decorators** |
423
437
| `@ArrayContains(values: any[])` | Checks if array contains all values from the given array of values. |
424
438
| `@ArrayNotContains(values: any[])` | Checks if array does not contain any of the given values. |
425
439
| `@ArrayNotEmpty()` | Checks if given array is not empty. |
426
440
| `@ArrayMinSize(min: number)` | Checks if array's length is as minimal this number. |
427
441
| `@ArrayMaxSize(max: number)` | Checks if array's length is as maximal this number. |
428
- | ` @ArrayUnique() ` | Checks if all array ' s values are unique. Comparison for objects is reference-based. |
442
+ | `@ArrayUnique()` | Checks if all array's values are unique. Comparison for objects is reference-based. |
429
443
430
444
## Samples
431
445
0 commit comments