Skip to content

Commit 90a6638

Browse files
ihorTymofieievvlapo
authored andcommittedNov 14, 2019
feat: add options for isISO8601 validator (#460)
1 parent 98e9382 commit 90a6638

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed
 

‎README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ validator.isIP(str, version); // Checks if the string is an IP (version 4 or 6).
836836
validator.isPort(str); // Check if the string is a valid port number.
837837
validator.isISBN(str, version); // Checks if the string is an ISBN (version 10 or 13).
838838
validator.isISIN(str); // Checks if the string is an ISIN (stock/security identifier).
839-
validator.isISO8601(str); // Checks if the string is a valid ISO 8601 date.
839+
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.
840840
validator.isJSON(str); // Checks if the string is valid JSON (note: uses JSON.parse).
841841
validator.isJWT(str) // Checks if the string is valid JWT.
842842
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
927927
| `@IsVariableWidth()` | Checks if the string contains a mixture of full and half-width chars. |
928928
| `@IsHexColor()` | Checks if the string is a hexadecimal color. |
929929
| `@IsHexadecimal()` | Checks if the string is a hexadecimal number. |
930-
| `@IsMACAddress()` | Checks if the string is a MAC Address. |
930+
| `@IsMACAddress()` | Checks if the string is a MAC Address. |
931931
| `@IsIP(version?: "4"\|"6")` | Checks if the string is an IP (version 4 or 6). |
932932
| `@IsPort()` | Check if the string is a valid port number. |
933933
| `@IsISBN(version?: "10"\|"13")` | Checks if the string is an ISBN (version 10 or 13). |
934934
| `@IsISIN()` | Checks if the string is an ISIN (stock/security identifier). |
935-
| `@IsISO8601()` | Checks if the string is a valid ISO 8601 date. |
935+
| `@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. |
936936
| `@IsJSON()` | Checks if the string is valid JSON. |
937937
| `@IsJWT()` | Checks if the string is valid JWT. |
938938
| `@IsObject()` | Checks if the object is valid Object (null, functions, arrays will return false). |

‎src/decorator/decorators.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ export function IsAlpha(locale?: string, validationOptions?: ValidationOptions)
607607
type: ValidationTypes.IS_ALPHA,
608608
target: object.constructor,
609609
propertyName: propertyName,
610-
constraints: [locale],
610+
constraints: [locale],
611611
validationOptions: validationOptions
612612
};
613613
getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args));
@@ -623,7 +623,7 @@ export function IsAlphanumeric(locale?: string, validationOptions?: ValidationOp
623623
type: ValidationTypes.IS_ALPHANUMERIC,
624624
target: object.constructor,
625625
propertyName: propertyName,
626-
constraints: [locale],
626+
constraints: [locale],
627627
validationOptions: validationOptions
628628
};
629629
getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args));
@@ -908,14 +908,15 @@ export function IsISIN(validationOptions?: ValidationOptions) {
908908
}
909909

910910
/**
911-
* Checks if the string is a valid ISO 8601 date.
911+
* 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.
912912
*/
913-
export function IsISO8601(validationOptions?: ValidationOptions) {
913+
export function IsISO8601(options?: ValidatorJS.IsISO8601Options, validationOptions?: ValidationOptions) {
914914
return function (object: Object, propertyName: string) {
915915
const args: ValidationMetadataArgs = {
916916
type: ValidationTypes.IS_ISO8601,
917917
target: object.constructor,
918918
propertyName: propertyName,
919+
constraints: [options],
919920
validationOptions: validationOptions
920921
};
921922
getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args));

‎src/validation/Validator.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ export class Validator {
229229
case ValidationTypes.IS_ISIN:
230230
return this.isISIN(value);
231231
case ValidationTypes.IS_ISO8601:
232-
return this.isISO8601(value);
232+
return this.isISO8601(value, metadata.constraints[0]);
233233
case ValidationTypes.IS_JSON:
234234
return this.isJSON(value);
235235
case ValidationTypes.IS_JWT:
@@ -274,7 +274,7 @@ export class Validator {
274274
return this.isHash(value, metadata.constraints[0]);
275275
case ValidationTypes.IS_ISSN:
276276
return this.isISSN(value, metadata.constraints[0]);
277-
277+
278278
/* array checkers */
279279
case ValidationTypes.ARRAY_CONTAINS:
280280
return this.arrayContains(value, metadata.constraints[0]);
@@ -444,7 +444,7 @@ export class Validator {
444444
return false;
445445
}
446446
}
447-
447+
448448
return Number.isFinite(value);
449449
}
450450

@@ -717,9 +717,10 @@ export class Validator {
717717
/**
718718
* Checks if the string is a valid ISO 8601 date.
719719
* If given value is not a string, then it returns false.
720+
* Use the option strict = true for additional checks for a valid date, e.g. invalidates dates like 2019-02-29.
720721
*/
721-
isISO8601(value: unknown): boolean {
722-
return typeof value === "string" && this.validatorJs.isISO8601(value);
722+
isISO8601(value: unknown, options?: ValidatorJS.IsISO8601Options): boolean {
723+
return typeof value === "string" && this.validatorJs.isISO8601(value, options);
723724
}
724725

725726
/**

0 commit comments

Comments
 (0)
Please sign in to comment.