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 • 3.12 kB
Source Map (JSON)
{"version":3,"sources":["../src/asserts/string/isValidDateTime.ts"],"sourcesContent":["import { setToDefaultLocale } from '../../translationMap';\nimport type { ExceptionContext, RequiredValidation } from '../../core';\n\n// Default error messages and keys\nconst dateTimeErrorMessage = 'The received value is not a valid ISO 8601 datetime string';\nconst dateTimeErrorKey = 's:isValidDateTime';\n\ntype DateTimeOptions = {\n offset?: boolean;\n precision?: number;\n};\n\nconst defaultOptions: DateTimeOptions = {\n offset: false,\n precision: undefined,\n};\n\n/**\n * @description Asserts that a string value is a valid ISO 8601 datetime string.\n * @param {DateTimeOptions} options Options to control the validation:\n * - `offset`: If `true`, allows timezone offsets in the datetime string.\n * - `precision`: Specify the exact number of fractional second digits allowed (e.g., 3 for milliseconds).\n * @returns {RequiredValidation} A validation function that takes a received string and an exception context.\n * @throws {ValidationError} if the received value is not a valid datetime string according to the options.\n * @example\n * const schema = string().custom(isValidDateTime());\n * parseOrFail(schema, \"2024-01-01T00:00:00Z\"); // Valid\n * parseOrFail(schema, \"2024-01-01T00:00:00.123Z\"); // Valid\n * parseOrFail(schema, \"2024-01-01T00:00:00+03:00\"); // Invalid (no offsets allowed)\n *\n * const schemaWithOffset = string().custom(isValidDateTime({ offset: true }));\n * parseOrFail(schemaWithOffset, \"2024-01-01T00:00:00+04:00\"); // Valid\n *\n * const schemaWithPrecision = string().custom(isValidDateTime({ precision: 3 }));\n * parseOrFail(schemaWithPrecision, \"2024-01-01T00:00:00.123Z\"); // Valid\n * parseOrFail(schemaWithPrecision, \"2024-01-01T00:00:00.123456Z\"); // Invalid\n *\n * @translation Error Translation Key = 's:isValidDateTime'\n */\nexport const isValidDateTime = (options: DateTimeOptions = defaultOptions): RequiredValidation => {\n return (received: string, ctx: ExceptionContext) => {\n const { offset, precision } = options;\n\n const dateTimeRegex = new RegExp(\n `^\\\\d{4}-\\\\d{2}-\\\\d{2}T\\\\d{2}:\\\\d{2}:\\\\d{2}` +\n (precision !== undefined ? `(\\\\.\\\\d{${precision}})` : `(\\\\.\\\\d+)?`) +\n `(${offset ? `([+-]\\\\d{2}:\\\\d{2}|[+-]\\\\d{2}\\\\d{2}|[+-]\\\\d{2}|Z)` : 'Z'})$`,\n );\n\n if (!dateTimeRegex.test(received)) {\n ctx.addIssue(received, dateTimeErrorMessage, dateTimeErrorKey);\n }\n };\n};\n\nisValidDateTime.key = dateTimeErrorKey;\nisValidDateTime.message = dateTimeErrorMessage;\nsetToDefaultLocale(isValidDateTime);\n"],"mappings":";;;;;AAIA,IAAM,uBAAuB;AAC7B,IAAM,mBAAmB;AAOzB,IAAM,iBAAkC;AAAA,EACtC,QAAQ;AAAA,EACR,WAAW;AACb;AAwBO,IAAM,kBAAkB,CAAC,UAA2B,mBAAuC;AAChG,SAAO,CAAC,UAAkB,QAA0B;AAClD,UAAM,EAAE,QAAQ,UAAU,IAAI;AAE9B,UAAM,gBAAgB,IAAI;AAAA,MACxB,gDACG,cAAc,SAAY,WAAW,SAAS,OAAO,gBACtD,IAAI,SAAS,sDAAsD,GAAG;AAAA,IAC1E;AAEA,QAAI,CAAC,cAAc,KAAK,QAAQ,GAAG;AACjC,UAAI,SAAS,UAAU,sBAAsB,gBAAgB;AAAA,IAC/D;AAAA,EACF;AACF;AAEA,gBAAgB,MAAM;AACtB,gBAAgB,UAAU;AAC1B,mBAAmB,eAAe;","names":[]}