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.44 kB
{"version":3,"sources":["../src/asserts/number/max.ts"],"sourcesContent":["import { setToDefaultLocale } from '../../translationMap';\nimport type { ExceptionContext, RequiredValidation } from '../../core';\n\nconst maxErrorMessage = 'The received value is greater than expected';\nconst maxErrorKey = 'n:max';\n\n/**\n * @description Asserts that a number value does not exceed a specified maximum value.\n * @param {number} expected The maximum allowable value.\n * @returns {RequiredValidation} A validation function that takes a received number and an exception context.\n * @throws {ValidationError} if the received value exceeds the expected maximum value.\n * @example\n * const schema = number().custom(max(100));\n * parseOrFail(schema, 99); // Valid\n * parseOrFail(schema, 100); // Valid\n * parseOrFail(schema, 101); // Throws an error: 'The received value is greater than expected'\n *\n * @translation Error Translation Key = 'n:max'\n */\nexport const max =\n (expected: number): RequiredValidation =>\n (received: number, ctx: ExceptionContext) => {\n if (expected < received) ctx.addIssue(expected, received, maxErrorKey);\n };\n\nmax.key = maxErrorKey;\nmax.message = maxErrorMessage;\nsetToDefaultLocale(max);\n"],"mappings":";;;;;AAGA,IAAM,kBAAkB;AACxB,IAAM,cAAc;AAeb,IAAM,MACX,CAAC,aACD,CAAC,UAAkB,QAA0B;AAC3C,MAAI,WAAW,SAAU,KAAI,SAAS,UAAU,UAAU,WAAW;AACvE;AAEF,IAAI,MAAM;AACV,IAAI,UAAU;AACd,mBAAmB,GAAG;","names":[]}