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.86 kB
Source Map (JSON)
{"version":3,"sources":["../src/asserts/object/maxKeys.ts"],"sourcesContent":["import type { ExceptionContext, RequiredValidation } from '../../core';\nimport { setToDefaultLocale } from '../../translationMap';\n\nconst maxKeysErrorMessage = 'The received number of keys is greater than expected';\nconst maxKeysErrorKey = 'o:maxKeys';\n\n/**\n * @description Ensures that the object has no more than the specified number of keys.\n * @param {number} expected - The maximum number of keys allowed in the object.\n * @returns {RequiredValidation} A validation function that takes a received object and an exception context.\n * @throws {ValidationError} if the number of the received keys is greater than the expected value.\n * @example\n * const schema = object({\n * name: string(),\n * email: string(),\n * })\n * .allowUnrecognized()\n * .custom(maxKeys(2));\n *\n * // This will pass\n * parseOrFail(schema, { name: 'John', email: 'john@example.com' });\n *\n * // This will throw an error because there are 3 keys\n * parseOrFail(schema, { name: 'John', email: 'john@example.com', address: '123 Main St' });\n *\n * @translation Error Translation Key = 'o:maxKeys'\n */\nexport const maxKeys =\n (expected: number): RequiredValidation =>\n (receivedObject: Record<string, unknown>, ctx: ExceptionContext) => {\n const keysCount = Object.keys(receivedObject).length;\n if (keysCount > expected) {\n ctx.addIssue(expected, keysCount, maxKeysErrorKey);\n }\n };\n\nmaxKeys.key = maxKeysErrorKey;\nmaxKeys.message = maxKeysErrorMessage;\nsetToDefaultLocale(maxKeys);\n"],"mappings":";;;;;AAGA,IAAM,sBAAsB;AAC5B,IAAM,kBAAkB;AAuBjB,IAAM,UACX,CAAC,aACD,CAAC,gBAAyC,QAA0B;AAClE,QAAM,YAAY,OAAO,KAAK,cAAc,EAAE;AAC9C,MAAI,YAAY,UAAU;AACxB,QAAI,SAAS,UAAU,WAAW,eAAe;AAAA,EACnD;AACF;AAEF,QAAQ,MAAM;AACd,QAAQ,UAAU;AAClB,mBAAmB,OAAO;","names":[]}