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.57 kB
Source Map (JSON)
{"version":3,"sources":["../src/asserts/string/minLength.ts"],"sourcesContent":["import type { ExceptionContext, RequiredValidation } from '../../core';\nimport { setToDefaultLocale } from '../../translationMap';\n\nconst minLengthErrorMessage = 'The received value length is less than expected';\nconst minLengthErrorKey = 's:minLength';\n\n/**\n * @description Asserts that the length of a string value is not less than a specified minimum length.\n * @param {number} expected The minimum required 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 less than the expected length.\n * @example\n * const schema = string().custom(minLength(5));\n * parseOrFail(schema, 'short'); // Throws an error: 'The received value length is less than expected'\n * parseOrFail(schema, 'adequate'); // Valid\n *\n * @translation Error Translation Key = 's:minLength'\n */\nexport const minLength =\n (expected: number): RequiredValidation =>\n (received: string, ctx: ExceptionContext) => {\n if (received.length < expected) ctx.addIssue(expected, received, minLengthErrorMessage);\n };\n\nminLength.key = minLengthErrorKey;\nminLength.message = minLengthErrorMessage;\nsetToDefaultLocale(minLength);\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":[]}