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.76 kB
{"version":3,"sources":["../src/asserts/array/maxArrayLength.ts"],"sourcesContent":["import type { ExceptionContext, RequiredValidation } from '../../core';\nimport { setToDefaultLocale } from '../../translationMap';\n\nconst maxArrayLengthErrorMessage = 'The received value length is greater than expected';\nconst maxArrayLengthErrorKey = 'a:maxArrayLength';\n\n/**\n * @description Asserts that the length of an array is not greater than a specified maximum length.\n * @param {number} expected The maximum allowed 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 greater than the expected length.\n * @example\n * const schema = array(string()).custom(maxArrayLength(3));\n * parseOrFail(schema, ['adequate', 'array']); // Valid\n * parseOrFail(schema, ['adequate', 'array', 'length']); // Valid\n * parseOrFail(schema, ['adequate', 'array', 'length', 'test']); // Throws an error: 'The received value length is greater than expected'\n *\n * @translation Error Translation Key = 'a:maxArrayLength'\n */\nexport const maxArrayLength =\n (expected: number): RequiredValidation =>\n (received: unknown[], ctx: ExceptionContext) => {\n if (received.length > expected) ctx.addIssue(expected, received, maxArrayLengthErrorMessage);\n };\n\nmaxArrayLength.key = maxArrayLengthErrorKey;\nmaxArrayLength.message = maxArrayLengthErrorMessage;\nsetToDefaultLocale(maxArrayLength);\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":[]}