UNPKG

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 1.73 kB
{"version":3,"sources":["../src/asserts/string/isValidDate.ts"],"sourcesContent":["import { setToDefaultLocale } from '../../translationMap';\nimport type { ExceptionContext, RequiredValidation } from '../../core';\n\nconst dateErrorMessage = 'The received value is not a valid date';\nconst dateErrorKey = 's:isValidDate';\n\n// Regex to match YYYY-MM-DD format\nconst dateRegexPattern = /^(19|20)\\d\\d-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01])$/;\n\n/**\n * @description Asserts that a string is a valid date in the format YYYY-MM-DD.\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 date.\n * @example\n * const schema = string().custom(isValidDate());\n * parseOrFail(schema, \"2020-01-01\"); // Valid\n * parseOrFail(schema, \"2020-1-1\"); // Throws an error: 'The received value is not a valid date'\n * parseOrFail(schema, \"2020-01-32\"); // Throws an error: 'The received value is not a valid date'\n *\n * @translation Error Translation Key = 's:isValidDate'\n */\nexport const isValidDate = (): RequiredValidation => {\n return (received: string, ctx: ExceptionContext) => {\n if (!dateRegexPattern.test(received)) {\n ctx.addIssue(received, dateErrorMessage, dateErrorKey);\n }\n };\n};\n\nisValidDate.key = dateErrorKey;\nisValidDate.message = dateErrorMessage;\nsetToDefaultLocale(isValidDate);\n"],"mappings":";;;;;AAGA,IAAM,mBAAmB;AACzB,IAAM,eAAe;AAGrB,IAAM,mBAAmB;AAclB,IAAM,cAAc,MAA0B;AACnD,SAAO,CAAC,UAAkB,QAA0B;AAClD,QAAI,CAAC,iBAAiB,KAAK,QAAQ,GAAG;AACpC,UAAI,SAAS,UAAU,kBAAkB,YAAY;AAAA,IACvD;AAAA,EACF;AACF;AAEA,YAAY,MAAM;AAClB,YAAY,UAAU;AACtB,mBAAmB,WAAW;","names":[]}