import { isString } from '@rawmodel/utils/dist/helpers/is-string'; import { isUndefined } from '@rawmodel/utils/dist/helpers/is-undefined'; import { isNull } from '@rawmodel/utils/dist/helpers/is-null'; const ISO8601_REGEX = /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/; /** * Returns a function for detecting date values. */ export function dateValidator(recipe: { iso?: boolean; } = {}) { return (value?: any) => { if (isUndefined(value) || isNull(value)) { return true; } else if (!isString(value)) { return false; } const date = Date.parse(value); if (!date) return false; const { iso } = recipe; if (iso) { return ISO8601_REGEX.test(value); } return true; }; }