UNPKG

1.19 kBJavaScriptView Raw
1import { buildMessage, ValidateBy } from '../common/ValidateBy';
2import isIso8601Validator from 'validator/lib/isISO8601';
3export const IS_ISO8601 = 'isIso8601';
4/**
5 * Checks if the string is a valid ISO 8601 date.
6 * If given value is not a string, then it returns false.
7 * Use the option strict = true for additional checks for a valid date, e.g. invalidates dates like 2019-02-29.
8 */
9export function isISO8601(value, options) {
10 return typeof value === 'string' && isIso8601Validator(value, options);
11}
12/**
13 * Checks if the string is a valid ISO 8601 date.
14 * If given value is not a string, then it returns false.
15 * Use the option strict = true for additional checks for a valid date, e.g. invalidates dates like 2019-02-29.
16 */
17export function IsISO8601(options, validationOptions) {
18 return ValidateBy({
19 name: IS_ISO8601,
20 constraints: [options],
21 validator: {
22 validate: (value, args) => isISO8601(value, args.constraints[0]),
23 defaultMessage: buildMessage(eachPrefix => eachPrefix + '$property must be a valid ISO 8601 date string', validationOptions),
24 },
25 }, validationOptions);
26}
27//# sourceMappingURL=IsISO8601.js.map
\No newline at end of file