bguard
Version:
**bguard** is a powerful, flexible, and type-safe validation library for TypeScript. It allows developers to define validation schemas for their data structures and ensures that data conforms to the expected types and constraints.
1 lines • 2.91 kB
Source Map (JSON)
{"version":3,"sources":["../src/asserts/string/isValidTime.ts"],"sourcesContent":["import { setToDefaultLocale } from '../../translationMap';\nimport type { ExceptionContext, RequiredValidation } from '../../core';\n\nconst timeErrorMessage = 'The received value is not a valid time';\nconst timeErrorKey = 's:isValidTime';\n\ninterface IsValidTimeOptions {\n precision?: number;\n}\n\n/**\n * @description Asserts that a string is a valid time in the format HH:mm:ss, with optional fractional seconds.\n * @param {IsValidTimeOptions} options Optional settings to configure the validation.\n * @returns {RequiredValidation} A validation function that takes a received string and an exception context.\n * @throws {ValidationError} if the received string is not a valid time.\n * @example\n * const schema = string().custom(isValidTime());\n * parseOrFail(schema, \"00:00:00\"); // Valid\n * parseOrFail(schema, \"23:59:59.9999999\"); // Valid\n * parseOrFail(schema, \"00:00:00.256Z\"); // Throws an error: 'The received value is not a valid time'\n *\n * const schemaWithPrecision = string().custom(isValidTime({ precision: 3 }));\n * parseOrFail(schemaWithPrecision, \"00:00:00.256\"); // Valid\n * parseOrFail(schemaWithPrecision, \"00:00:00\"); // Throws an error: 'The received value is not a valid time'\n *\n * @translation Error Translation Key = 's:isValidTime'\n */\nexport const isValidTime = (options: IsValidTimeOptions = {}): RequiredValidation => {\n return (received: string, ctx: ExceptionContext) => {\n const { precision } = options;\n\n // Base regex for HH:mm:ss format\n let timeRegex = /^([01]\\d|2[0-3]):([0-5]\\d):([0-5]\\d)(\\.\\d+)?$/;\n\n // If precision is specified, enforce exact number of digits in fractional seconds\n if (precision !== undefined) {\n timeRegex = new RegExp(`^([01]\\\\d|2[0-3]):([0-5]\\\\d):([0-5]\\\\d)(\\\\.\\\\d{${precision}})?$`);\n }\n\n // Check if the string is a valid time\n if (!timeRegex.test(received)) {\n ctx.addIssue(received, timeErrorMessage, timeErrorKey);\n }\n\n // Additional check for precision if it's specified and the time does not have fractional seconds\n if (precision !== undefined && !received.includes('.')) {\n ctx.addIssue(received, timeErrorMessage, timeErrorKey);\n }\n };\n};\n\nisValidTime.key = timeErrorKey;\nisValidTime.message = timeErrorMessage;\nsetToDefaultLocale(isValidTime);\n"],"mappings":";;;;;AAGA,IAAM,mBAAmB;AACzB,IAAM,eAAe;AAuBd,IAAM,cAAc,CAAC,UAA8B,CAAC,MAA0B;AACnF,SAAO,CAAC,UAAkB,QAA0B;AAClD,UAAM,EAAE,UAAU,IAAI;AAGtB,QAAI,YAAY;AAGhB,QAAI,cAAc,QAAW;AAC3B,kBAAY,IAAI,OAAO,kDAAkD,SAAS,MAAM;AAAA,IAC1F;AAGA,QAAI,CAAC,UAAU,KAAK,QAAQ,GAAG;AAC7B,UAAI,SAAS,UAAU,kBAAkB,YAAY;AAAA,IACvD;AAGA,QAAI,cAAc,UAAa,CAAC,SAAS,SAAS,GAAG,GAAG;AACtD,UAAI,SAAS,UAAU,kBAAkB,YAAY;AAAA,IACvD;AAAA,EACF;AACF;AAEA,YAAY,MAAM;AAClB,YAAY,UAAU;AACtB,mBAAmB,WAAW;","names":[]}