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.74 kB
{"version":3,"sources":["../src/asserts/array/minArrayLength.ts"],"sourcesContent":["import type { ExceptionContext, RequiredValidation } from '../../core';\nimport { setToDefaultLocale } from '../../translationMap';\n\nconst minArrayLengthErrorMessage = 'The received value length is less than expected';\nconst minArrayLengthErrorKey = 'a:minArrayLength';\n\n/**\n * @description Asserts that the length of na array is not less than a specified minimum length.\n * @param {number} expected The minimum required length for the array.\n * @returns {RequiredValidation} A validation function that takes a received array 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 = array(string()).custom(minArrayLength(3));\n * parseOrFail(schema, ['short', 'array']); // Throws an error: 'The received value length is less than expected'\n * parseOrFail(schema, ['adequate', 'array', 'length']); // Valid\n * parseOrFail(schema, ['adequate', 'array', 'length', 'test']); // Valid\n *\n * @translation Error Translation Key = 'a:minArrayLength'\n */\nexport const minArrayLength =\n (expected: number): RequiredValidation =>\n (received: unknown[], ctx: ExceptionContext) => {\n if (received.length < expected) ctx.addIssue(expected, received, minArrayLengthErrorMessage);\n };\n\nminArrayLength.key = minArrayLengthErrorKey;\nminArrayLength.message = minArrayLengthErrorMessage;\nsetToDefaultLocale(minArrayLength);\n"],"mappings":";;;;;AAGA,IAAM,6BAA6B;AACnC,IAAM,yBAAyB;AAexB,IAAM,iBACX,CAAC,aACD,CAAC,UAAqB,QAA0B;AAC9C,MAAI,SAAS,SAAS,SAAU,KAAI,SAAS,UAAU,UAAU,0BAA0B;AAC7F;AAEF,eAAe,MAAM;AACrB,eAAe,UAAU;AACzB,mBAAmB,cAAc;","names":[]}