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 3.34 kB
{"version":3,"sources":["../src/asserts/object/index.ts"],"sourcesContent":["import { WithObject } from '../../commonTypes';\nimport { BuildSchemaError } from '../../exceptions';\nimport { ctxSymbol } from '../../helpers/constants';\nimport { CommonSchema, ValidatorContext, type ObjectShapeSchemaType } from '../../core';\n\nclass ObjectSchema extends CommonSchema {\n protected _object = 1;\n constructor(ctx: ValidatorContext, shapeSchema: ObjectShapeSchemaType) {\n super(ctx);\n this.validateObjectEntry(shapeSchema);\n this[ctxSymbol].object = shapeSchema;\n }\n\n private validateObjectEntry(shapeSchema: ObjectShapeSchemaType) {\n if (!shapeSchema) throw new BuildSchemaError('Missing schema in object method');\n if (shapeSchema instanceof CommonSchema) throw new BuildSchemaError('Invalid schema in object');\n for (const [key, value] of Object.entries(shapeSchema)) {\n if (!(value instanceof CommonSchema))\n throw new BuildSchemaError(`Invalid schema in object method for property '${key}'`);\n }\n }\n\n /**\n * @method allowUnrecognized\n * @description Allows unrecognized properties in the validated object.\n * When this method is called, the validation will not fail\n * if the received object contains properties not specified\n * in the validation schema.\n * @returns {this} The current ObjectSchema instance.\n * @example\n * const userSchema = object({\n * name: string(),\n * age: number(),\n * }).allowUnrecognized();\n *\n * parseOrFail(userSchema, ({ name: 'John', age: 30, extra: 'value' }););\n * // No error thrown\n *\n * @public\n */\n public allowUnrecognized(): this {\n this[ctxSymbol].allowUnrecognizedObjectProps = true;\n return this;\n }\n}\n\n/**\n * @description Creates a new schema for validating objects where each property must match the specified schema.\n * @template T\n * @param {T} shapeSchema - The schema that each property of the object must match.\n * @returns {WithObject<ObjectSchema, T>} A new instance of `ObjectSchema` for validating objects with properties matching the specified schema.\n * @example\n * const schema = object({\n * name: string(),\n * age: number()\n * });\n * parseOrFail(schema, { name: 'John', age: 30 }); // Validates successfully\n * parseOrFail(schema, { name: 'John', age: '30' }); // Throws a validation error\n *\n * @instance Of ObjectSchema\n */\nexport function object<T extends ObjectShapeSchemaType>(shapeSchema: T): WithObject<ObjectSchema, T> {\n return new ObjectSchema({ type: [], requiredValidations: [] }, shapeSchema) as WithObject<ObjectSchema, T>;\n}\n"],"mappings":";;;;;;;;;;;AAKA,IAAM,eAAN,cAA2B,aAAa;AAAA,EAC5B,UAAU;AAAA,EACpB,YAAY,KAAuB,aAAoC;AACrE,UAAM,GAAG;AACT,SAAK,oBAAoB,WAAW;AACpC,SAAK,SAAS,EAAE,SAAS;AAAA,EAC3B;AAAA,EAEQ,oBAAoB,aAAoC;AAC9D,QAAI,CAAC,YAAa,OAAM,IAAI,iBAAiB,iCAAiC;AAC9E,QAAI,uBAAuB,aAAc,OAAM,IAAI,iBAAiB,0BAA0B;AAC9F,eAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,WAAW,GAAG;AACtD,UAAI,EAAE,iBAAiB;AACrB,cAAM,IAAI,iBAAiB,iDAAiD,GAAG,GAAG;AAAA,IACtF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAoBO,oBAA0B;AAC/B,SAAK,SAAS,EAAE,+BAA+B;AAC/C,WAAO;AAAA,EACT;AACF;AAiBO,SAAS,OAAwC,aAA6C;AACnG,SAAO,IAAI,aAAa,EAAE,MAAM,CAAC,GAAG,qBAAqB,CAAC,EAAE,GAAG,WAAW;AAC5E;","names":[]}