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.6 kB
Source Map (JSON)
{"version":3,"sources":["../src/asserts/string/maxLength.ts"],"sourcesContent":["import type { ExceptionContext, RequiredValidation } from '../../core';\nimport { setToDefaultLocale } from '../../translationMap';\n\nconst maxLengthErrorMessage = 'The received value length is greater than expected';\nconst maxLengthErrorKey = 's:maxLength';\n\n/**\n * @description Asserts that the length of a string value is not greater than a specified maximum length.\n * @param {number} expected The maximum allowed length for the string.\n * @returns {RequiredValidation} A validation function that takes a received string and an exception context.\n * @throws {ValidationError} if the length of the received value is greater than the expected length.\n * @example\n * const schema = string().custom(maxLength(10));\n * parseOrFail(schema, 'short'); // Valid\n * parseOrFail(schema, 'this is a very long string'); // Throws an error: 'The received value length is greater than expected'\n *\n * @translation Error Translation Key = 's:maxLength'\n */\nexport const maxLength =\n (expected: number): RequiredValidation =>\n (received: string, ctx: ExceptionContext) => {\n if (received.length > expected) ctx.addIssue(expected, received, maxLengthErrorMessage);\n };\n\nmaxLength.key = maxLengthErrorKey;\nmaxLength.message = maxLengthErrorMessage;\nsetToDefaultLocale(maxLength);\n"],"mappings":";;;;;AAGA,IAAM,wBAAwB;AAC9B,IAAM,oBAAoB;AAcnB,IAAM,YACX,CAAC,aACD,CAAC,UAAkB,QAA0B;AAC3C,MAAI,SAAS,SAAS,SAAU,KAAI,SAAS,UAAU,UAAU,qBAAqB;AACxF;AAEF,UAAU,MAAM;AAChB,UAAU,UAAU;AACpB,mBAAmB,SAAS;","names":[]}