UNPKG

1.17 kBJavaScriptView Raw
1import { buildMessage, ValidateBy } from '../common/ValidateBy';
2import matchesValidator from 'validator/lib/matches';
3export const IS_MILITARY_TIME = 'isMilitaryTime';
4/**
5 * Checks if the string represents a time without a given timezone in the format HH:MM (military)
6 * If the given value does not match the pattern HH:MM, then it returns false.
7 */
8export function isMilitaryTime(value) {
9 const militaryTimeRegex = /^([01]\d|2[0-3]):?([0-5]\d)$/;
10 return typeof value === 'string' && matchesValidator(value, militaryTimeRegex);
11}
12/**
13 * Checks if the string represents a time without a given timezone in the format HH:MM (military)
14 * If the given value does not match the pattern HH:MM, then it returns false.
15 */
16export function IsMilitaryTime(validationOptions) {
17 return ValidateBy({
18 name: IS_MILITARY_TIME,
19 validator: {
20 validate: (value, args) => isMilitaryTime(value),
21 defaultMessage: buildMessage(eachPrefix => eachPrefix + '$property must be a valid representation of military time in the format HH:MM', validationOptions),
22 },
23 }, validationOptions);
24}
25//# sourceMappingURL=IsMilitaryTime.js.map
\No newline at end of file