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 2.08 kB
{"version":3,"sources":["../src/asserts/date/dateMin.ts"],"sourcesContent":["import { setToDefaultLocale } from '../../translationMap';\nimport { BuildSchemaError } from '../../exceptions';\nimport type { ExceptionContext, RequiredValidation } from '../../core';\nimport { isValidDateInner } from '../../helpers/isValidDateInner';\n\nconst dateMinErrorMessage = 'The received value is less than expected';\nconst dateMinErrorKey = 'dt:min';\n/**\n * @description Asserts that a number value is not less than a specified minimum value.\n * @param {Date | string} expected The minimum 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 less than the expected minimum value.\n * @example\n * const schema = date().custom(dateMin('2023-01-01'));\n * parseOrFail(schema, new Date('2023-01-02')); // Valid\n * parseOrFail(schema, new Date('2023-01-01')); // Valid\n * parseOrFail(schema, new Date('2022-12-31')); // Throws an error: 'The received value is less than expected'\n *\n * @translation Error Translation Key = 'dt:min'\n */\nexport const dateMin = (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, dateMinErrorKey);\n };\n};\n\ndateMin.key = dateMinErrorKey;\ndateMin.message = dateMinErrorMessage;\nsetToDefaultLocale(dateMin);\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":[]}