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.

57 lines (48 loc) 2.01 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); var _class; var _chunkVN4GLOENjs = require('./chunk-VN4GLOEN.js'); var _chunkFIDPCCCWjs = require('./chunk-FIDPCCCW.js'); var _chunkAXKX5BGHjs = require('./chunk-AXKX5BGH.js'); // src/asserts/object/index.ts var ObjectSchema = (_class = class extends _chunkVN4GLOENjs.CommonSchema { __init() {this._object = 1} constructor(ctx, shapeSchema) { super(ctx);_class.prototype.__init.call(this);; this.validateObjectEntry(shapeSchema); this[_chunkAXKX5BGHjs.ctxSymbol].object = shapeSchema; } validateObjectEntry(shapeSchema) { if (!shapeSchema) throw new (0, _chunkFIDPCCCWjs.BuildSchemaError)("Missing schema in object method"); if (shapeSchema instanceof _chunkVN4GLOENjs.CommonSchema) throw new (0, _chunkFIDPCCCWjs.BuildSchemaError)("Invalid schema in object"); for (const [key, value] of Object.entries(shapeSchema)) { if (!(value instanceof _chunkVN4GLOENjs.CommonSchema)) throw new (0, _chunkFIDPCCCWjs.BuildSchemaError)(`Invalid schema in object method for property '${key}'`); } } /** * @method allowUnrecognized * @description Allows unrecognized properties in the validated object. * When this method is called, the validation will not fail * if the received object contains properties not specified * in the validation schema. * @returns {this} The current ObjectSchema instance. * @example * const userSchema = object({ * name: string(), * age: number(), * }).allowUnrecognized(); * * parseOrFail(userSchema, ({ name: 'John', age: 30, extra: 'value' });); * // No error thrown * * @public */ allowUnrecognized() { this[_chunkAXKX5BGHjs.ctxSymbol].allowUnrecognizedObjectProps = true; return this; } }, _class); function object(shapeSchema) { return new ObjectSchema({ type: [], requiredValidations: [] }, shapeSchema); } exports.object = object; //# sourceMappingURL=chunk-DED5KDLM.js.map