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.09 kB
Source Map (JSON)
{"version":3,"sources":["../src/asserts/date/dateMax.ts"],"sourcesContent":["import { setToDefaultLocale } from '../../translationMap';\nimport { BuildSchemaError } from '../../exceptions';\nimport type { ExceptionContext, RequiredValidation } from '../../core';\nimport { isValidDateInner } from '../../helpers/isValidDateInner';\n\nconst dateMaxErrorMessage = 'The received value is greater than expected';\nconst dateMaxErrorKey = 'dt:max';\n/**\n * @description Asserts that a date value is not greater than a specified maximum value.\n * @param {Date | string} expected The maximum allowable value.\n * @returns {RequiredValidation} A validation function that takes a received Date or string and an exception context.\n * @throws {ValidationError} if the received value is greater than the expected maximum value.\n * @example\n * const schema = date().custom(dateMax('2024-12-31'));\n * parseOrFail(schema, new Date('2024-12-30')); // Valid\n * parseOrFail(schema, new Date('2024-12-31')); // Valid\n * parseOrFail(schema, new Date('2025-01-01')); // Throws an error: 'The received value is greater than expected'\n *\n * @translation Error Translation Key = 'dt:max'\n */\nexport const dateMax = (expected: Date | string): RequiredValidation => {\n const transformedExpected = typeof expected === 'string' ? new Date(expected) : expected;\n if (!isValidDateInner(transformedExpected)) throw new BuildSchemaError('Invalid date in Date assertion');\n return (received: Date, ctx: ExceptionContext) => {\n if (transformedExpected < received) ctx.addIssue(transformedExpected, received, dateMaxErrorKey);\n };\n};\n\ndateMax.key = dateMaxErrorKey;\ndateMax.message = dateMaxErrorMessage;\nsetToDefaultLocale(dateMax);\n"],"mappings":";;;;;;;;;;;AAKA,IAAM,sBAAsB;AAC5B,IAAM,kBAAkB;AAcjB,IAAM,UAAU,CAAC,aAAgD;AACtE,QAAM,sBAAsB,OAAO,aAAa,WAAW,IAAI,KAAK,QAAQ,IAAI;AAChF,MAAI,CAAC,iBAAiB,mBAAmB,EAAG,OAAM,IAAI,iBAAiB,gCAAgC;AACvG,SAAO,CAAC,UAAgB,QAA0B;AAChD,QAAI,sBAAsB,SAAU,KAAI,SAAS,qBAAqB,UAAU,eAAe;AAAA,EACjG;AACF;AAEA,QAAQ,MAAM;AACd,QAAQ,UAAU;AAClB,mBAAmB,OAAO;","names":[]}