Skip to content

Commit a736187

Browse files
author
Umed Khudoiberdiev
committed
additional fixes + updates in docs
1 parent 69ade59 commit a736187

File tree

5 files changed

+129
-124
lines changed

5 files changed

+129
-124
lines changed

README.md

+40-26
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ Internally uses [validator.js][1] to perform validation.
1515

1616
and use it somewhere in the global place of your app:
1717

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`)
1919
* for browser: `<script src="path-to-shim/es6-shim.js">` in your `index.html`
2020

21+
For node.js users this step is only required if you are using old versions of node.
22+
2123
## Usage
2224

2325
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
5456
post.email = "google.com"; // should not pass
5557
post.site = "googlecom"; // should not pass
5658

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
5866
```
5967

6068
## Validation messages
@@ -282,35 +290,40 @@ import {Validator} from "class-validator";
282290
283291
// Validation methods
284292
const validator = new Validator();
293+
294+
// common validation methods
285295
validator.equals(value, comparison); // Checks if value matches ("===") the comparison.
286296
validator.notEquals(value, comparison); // Checks if value does not match ("!==") the comparison.
287297
validator.empty(value); // Checks if given value is empty (=== '', === null, === undefined).
288298
validator.notEmpty(value); // Checks if given value is not empty (!== '', !== null, !== undefined).
289299
validator.isIn(value, possibleValues); // Checks if given value is in a array of allowed values.
290300
validator.isNotIn(value, possibleValues); // Checks if given value not in a array of allowed values.
291301
302+
// type validation methods
292303
validator.isBoolean(value); // Checks if a given value is a real boolean.
293304
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.
295305
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.
299307
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.
300312
validator.isPositive(value); // Checks if the value is a positive number.
301313
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.
304316
317+
// date validation methods
305318
validator.minDate(date, minDate); // Checks if the value is a date that's after the specified date.
306319
validator.maxDate(date, minDate); // Checks if the value is a date that's before the specified date.
307320
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
310322
validator.isBooleanString(str); // Checks if a string is a boolean.
311323
validator.isDateString(str); // Checks if the string is a date.
312324
validator.isNumberString(str); // Checks if the string is numeric.
313325
326+
// string validation methods
314327
validator.contains(str, seed); // Checks if the string contains the seed.
315328
validator.notContains(str, seed); // Checks if the string does not contain the seed.
316329
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.
343356
validator.length(str, min, max); // Checks if the string's length falls in a range.
344357
validator.minLength(str, min); // Checks if the string's length is not less then given number.
345358
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').
346360
361+
// array validation methods
347362
validator.arrayContains(array, values); // Checks if array contains all values from the given array of values.
348363
validator.arrayNotContains(array, values); // Checks if array does not contain any of the given values.
349364
validator.arrayNotEmpty(array); // Checks if given array is not empty.
350365
validator.arrayMinSize(array, min); // Checks if array's length is as minimal this number.
351366
validator.arrayMaxSize(array, max); // Checks if array's length is as maximal this number.
352367
validator.arrayUnique(array); // Checks if all array's values are unique. Comparison for objects is reference-based.
353-
354368
```
355369

356370
## Validation decorators
357371

358372
| Decorator | Description |
359373
|-------------------------------------------------|----------------------------------------------------------------------------------------------------|
374+
| **Common validation decorators** |
360375
| `@Equals(comparison: any)` | Checks if value equals ("===") comparison. |
361376
| `@NotEquals(comparison: any)` | Checks if value not equal ("!==") comparison. |
362377
| `@Empty()` | Checks if given value is empty (=== '', === null, === undefined). |
363378
| `@NotEmpty()` | Checks if given value is not empty (!== '', !== null, !== undefined). |
364379
| `@In(values: any[])` | Checks if value is in a array of allowed values. |
365380
| `@NotIn(values: any[])` | Checks if value is not in a array of disallowed values. |
366-
| |
381+
| **Type validation decorators** |
367382
| `@IsBoolean()` | Checks if a value is a boolean. |
368383
| `@IsDate()` | Checks if the string is a date. |
369-
| `@IsNumber()` | Checks if the string is a number. |
370384
| `@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. |
374386
| `@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. |
375390
| `@IsPositive()` | Checks if the value is a positive number. |
376391
| `@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** |
380395
| `@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** |
385398
| `@IsBooleanString()` | Checks if a string is a boolean (e.g. is "true" or "false"). |
386399
| `@IsDateString()` | Checks if a string is a date. |
387400
| `@IsNumberString()` | Checks if a string is a number. |
388-
| |
401+
| **String validation decorators** |
389402
| `@Contains(seed: string)` | Checks if the string contains the seed. |
390403
| `@NotContains(seed: string)` | Checks if the string not contains the seed. |
391404
| `@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
419432
| `@Length(min: number, max?: number)` | Checks if the string's length falls in a range. |
420433
| `@MinLength(min: number)` | Checks if the string's length is not less then given number. |
421434
| `@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** |
423437
| `@ArrayContains(values: any[])` | Checks if array contains all values from the given array of values. |
424438
| `@ArrayNotContains(values: any[])` | Checks if array does not contain any of the given values. |
425439
| `@ArrayNotEmpty()` | Checks if given array is not empty. |
426440
| `@ArrayMinSize(min: number)` | Checks if array's length is as minimal this number. |
427441
| `@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. |
429443

430444
## Samples
431445

0 commit comments

Comments
 (0)