From ce0df70a5c4286d0e42cb9ee090b242660bc812b Mon Sep 17 00:00:00 2001 From: Ihor Date: Thu, 14 Nov 2019 03:02:45 +0200 Subject: [PATCH 1/3] feat!: add strict mode for isISO8601 validator --- README.md | 6 +++--- src/decorator/decorators.ts | 5 +++-- src/validation/Validator.ts | 7 ++++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index de7d9f469b..37a8765b83 100644 --- a/README.md +++ b/README.md @@ -834,7 +834,7 @@ validator.isHexadecimal(str); // Checks if the string is a hexadecimal number. validator.isIP(str, version); // Checks if the string is an IP (version 4 or 6). validator.isISBN(str, version); // Checks if the string is an ISBN (version 10 or 13). validator.isISIN(str); // Checks if the string is an ISIN (stock/security identifier). -validator.isISO8601(str); // Checks if the string is a valid ISO 8601 date. +validator.isISO8601(strict, str); // Checks if the string is a valid ISO 8601 date. Use the option strict = true for additional checks for a valid date, e.g. invalidates dates like 2019-02-29. validator.isJSON(str); // Checks if the string is valid JSON (note: uses JSON.parse). validator.isLowercase(str); // Checks if the string is lowercase. validator.isMobilePhone(str, locale); // Checks if the string is a mobile phone number. @@ -920,7 +920,7 @@ validator.isInstance(value, target); // Checks value is an instance of the targe | `@IsIP(version?: "4"\|"6")` | Checks if the string is an IP (version 4 or 6). | | `@IsISBN(version?: "10"\|"13")` | Checks if the string is an ISBN (version 10 or 13). | | `@IsISIN()` | Checks if the string is an ISIN (stock/security identifier). | -| `@IsISO8601()` | Checks if the string is a valid ISO 8601 date. | +| `@IsISO8601(strict?: boolean)` | Checks if the string is a valid ISO 8601 date. Use the option strict = true for additional checks for a valid date, e.g. invalidates dates like 2019-02-29. | | `@IsJSON()` | Checks if the string is valid JSON. | | `@IsLowercase()` | Checks if the string is lowercase. | | `@IsMobilePhone(locale: string)` | Checks if the string is a mobile phone number. | @@ -1046,4 +1046,4 @@ See information about breaking changes and release notes [here][3]. [1]: https://github.com/chriso/validator.js [2]: https://github.com/pleerock/typedi -[3]: CHANGELOG.md \ No newline at end of file +[3]: CHANGELOG.md diff --git a/src/decorator/decorators.ts b/src/decorator/decorators.ts index 7e2bacba05..0ec0dc831f 100644 --- a/src/decorator/decorators.ts +++ b/src/decorator/decorators.ts @@ -846,14 +846,15 @@ export function IsISIN(validationOptions?: ValidationOptions) { } /** - * Checks if the string is a valid ISO 8601 date. + * Checks if the string is a valid ISO 8601 date. Use the option strict = true for additional checks for a valid date, e.g. invalidates dates like 2019-02-29. */ -export function IsISO8601(validationOptions?: ValidationOptions) { +export function IsISO8601(strict?: boolean, validationOptions?: ValidationOptions) { return function (object: Object, propertyName: string) { const args: ValidationMetadataArgs = { type: ValidationTypes.IS_ISO8601, target: object.constructor, propertyName: propertyName, + constraints: [strict], validationOptions: validationOptions }; getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args)); diff --git a/src/validation/Validator.ts b/src/validation/Validator.ts index ceeac049a1..d2aefe0474 100644 --- a/src/validation/Validator.ts +++ b/src/validation/Validator.ts @@ -212,7 +212,7 @@ export class Validator { case ValidationTypes.IS_ISIN: return this.isISIN(value); case ValidationTypes.IS_ISO8601: - return this.isISO8601(value); + return this.isISO8601(value, metadata.constraints[0]); case ValidationTypes.IS_JSON: return this.isJSON(value); case ValidationTypes.IS_LOWERCASE: @@ -630,6 +630,7 @@ export class Validator { /** * Checks if the string is an ISBN (version 10 or 13). * If given value is not a string, then it returns false. + * Use the option strict = true for additional checks for a valid date, e.g. invalidates dates like 2019-02-29. */ isISBN(value: string, version?: number): boolean { return typeof value === "string" && this.validatorJs.isISBN(value, version); @@ -647,8 +648,8 @@ export class Validator { * Checks if the string is a valid ISO 8601 date. * If given value is not a string, then it returns false. */ - isISO8601(value: string): boolean { - return typeof value === "string" && this.validatorJs.isISO8601(value); + isISO8601(value: string, strict?: boolean): boolean { + return typeof value === "string" && this.validatorJs.isISO8601(value, {strict}); } /** From 0e22828977e1ef95dbcc7ac7549d52bc4cd067ca Mon Sep 17 00:00:00 2001 From: Ihor Date: Thu, 14 Nov 2019 03:38:53 +0200 Subject: [PATCH 2/3] fix: fix merge comment typo for extend-iso8601 --- src/validation/Validator.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/validation/Validator.ts b/src/validation/Validator.ts index 5c1d483364..0099105d2a 100644 --- a/src/validation/Validator.ts +++ b/src/validation/Validator.ts @@ -274,7 +274,7 @@ export class Validator { return this.isHash(value, metadata.constraints[0]); case ValidationTypes.IS_ISSN: return this.isISSN(value, metadata.constraints[0]); - + /* array checkers */ case ValidationTypes.ARRAY_CONTAINS: return this.arrayContains(value, metadata.constraints[0]); @@ -444,7 +444,7 @@ export class Validator { return false; } } - + return Number.isFinite(value); } @@ -701,7 +701,6 @@ export class Validator { /** * Checks if the string is an ISBN (version 10 or 13). * If given value is not a string, then it returns false. - * Use the option strict = true for additional checks for a valid date, e.g. invalidates dates like 2019-02-29. */ isISBN(value: unknown, version?: number): boolean { return typeof value === "string" && this.validatorJs.isISBN(value, version); From dafe2fa56004085d726f89dea55dc3a5eb8b522c Mon Sep 17 00:00:00 2001 From: Ihor Date: Thu, 14 Nov 2019 12:42:47 +0200 Subject: [PATCH 3/3] fix: use ValidatorJS.IsISO8601Options instead of only one strict argument for the isISO8601 validator --- README.md | 6 +++--- src/decorator/decorators.ts | 8 ++++---- src/validation/Validator.ts | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 5ae45a77d3..0993bf13a6 100644 --- a/README.md +++ b/README.md @@ -836,7 +836,7 @@ validator.isIP(str, version); // Checks if the string is an IP (version 4 or 6). validator.isPort(str); // Check if the string is a valid port number. validator.isISBN(str, version); // Checks if the string is an ISBN (version 10 or 13). validator.isISIN(str); // Checks if the string is an ISIN (stock/security identifier). -validator.isISO8601(strict, str); // Checks if the string is a valid ISO 8601 date. Use the option strict = true for additional checks for a valid date, e.g. invalidates dates like 2019-02-29. +validator.isISO8601(str, options); // Checks if the string is a valid ISO 8601 date. Use the option strict = true for additional checks for a valid date, e.g. invalidates dates like 2019-02-29. validator.isJSON(str); // Checks if the string is valid JSON (note: uses JSON.parse). validator.isJWT(str) // Checks if the string is valid JWT. validator.isObject(object); // Checks if the object is valid Object (null, functions, arrays will return false) @@ -927,12 +927,12 @@ validator.isInstance(value, target); // Checks value is an instance of the targe | `@IsVariableWidth()` | Checks if the string contains a mixture of full and half-width chars. | | `@IsHexColor()` | Checks if the string is a hexadecimal color. | | `@IsHexadecimal()` | Checks if the string is a hexadecimal number. | -| `@IsMACAddress()` | Checks if the string is a MAC Address. | +| `@IsMACAddress()` | Checks if the string is a MAC Address. | | `@IsIP(version?: "4"\|"6")` | Checks if the string is an IP (version 4 or 6). | | `@IsPort()` | Check if the string is a valid port number. | | `@IsISBN(version?: "10"\|"13")` | Checks if the string is an ISBN (version 10 or 13). | | `@IsISIN()` | Checks if the string is an ISIN (stock/security identifier). | -| `@IsISO8601(strict?: boolean)` | Checks if the string is a valid ISO 8601 date. Use the option strict = true for additional checks for a valid date, e.g. invalidates dates like 2019-02-29. | +| `@IsISO8601(options?: IsISO8601Options)` | Checks if the string is a valid ISO 8601 date. Use the option strict = true for additional checks for a valid date, e.g. invalidates dates like 2019-02-29. | | `@IsJSON()` | Checks if the string is valid JSON. | | `@IsJWT()` | Checks if the string is valid JWT. | | `@IsObject()` | Checks if the object is valid Object (null, functions, arrays will return false). | diff --git a/src/decorator/decorators.ts b/src/decorator/decorators.ts index dea07599b8..0826e5ef8d 100644 --- a/src/decorator/decorators.ts +++ b/src/decorator/decorators.ts @@ -607,7 +607,7 @@ export function IsAlpha(locale?: string, validationOptions?: ValidationOptions) type: ValidationTypes.IS_ALPHA, target: object.constructor, propertyName: propertyName, - constraints: [locale], + constraints: [locale], validationOptions: validationOptions }; getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args)); @@ -623,7 +623,7 @@ export function IsAlphanumeric(locale?: string, validationOptions?: ValidationOp type: ValidationTypes.IS_ALPHANUMERIC, target: object.constructor, propertyName: propertyName, - constraints: [locale], + constraints: [locale], validationOptions: validationOptions }; getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args)); @@ -910,13 +910,13 @@ export function IsISIN(validationOptions?: ValidationOptions) { /** * Checks if the string is a valid ISO 8601 date. Use the option strict = true for additional checks for a valid date, e.g. invalidates dates like 2019-02-29. */ -export function IsISO8601(strict?: boolean, validationOptions?: ValidationOptions) { +export function IsISO8601(options?: ValidatorJS.IsISO8601Options, validationOptions?: ValidationOptions) { return function (object: Object, propertyName: string) { const args: ValidationMetadataArgs = { type: ValidationTypes.IS_ISO8601, target: object.constructor, propertyName: propertyName, - constraints: [strict], + constraints: [options], validationOptions: validationOptions }; getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args)); diff --git a/src/validation/Validator.ts b/src/validation/Validator.ts index 0099105d2a..5b6c68ce87 100644 --- a/src/validation/Validator.ts +++ b/src/validation/Validator.ts @@ -719,8 +719,8 @@ export class Validator { * If given value is not a string, then it returns false. * Use the option strict = true for additional checks for a valid date, e.g. invalidates dates like 2019-02-29. */ - isISO8601(value: unknown, strict?: boolean): boolean { - return typeof value === "string" && this.validatorJs.isISO8601(value, {strict}); + isISO8601(value: unknown, options?: ValidatorJS.IsISO8601Options): boolean { + return typeof value === "string" && this.validatorJs.isISO8601(value, options); } /**