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 (55 loc) • 1.69 kB
JavaScript
import {
CommonSchema
} from "./chunk-QIPGUTIG.mjs";
import {
BuildSchemaError
} from "./chunk-ZTCXXAKD.mjs";
import {
ctxSymbol
} from "./chunk-2ANPCB4O.mjs";
// src/asserts/object/index.ts
var ObjectSchema = class extends CommonSchema {
_object = 1;
constructor(ctx, shapeSchema) {
super(ctx);
this.validateObjectEntry(shapeSchema);
this[ctxSymbol].object = shapeSchema;
}
validateObjectEntry(shapeSchema) {
if (!shapeSchema) throw new BuildSchemaError("Missing schema in object method");
if (shapeSchema instanceof CommonSchema) throw new BuildSchemaError("Invalid schema in object");
for (const [key, value] of Object.entries(shapeSchema)) {
if (!(value instanceof CommonSchema))
throw new 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[ctxSymbol].allowUnrecognizedObjectProps = true;
return this;
}
};
function object(shapeSchema) {
return new ObjectSchema({ type: [], requiredValidations: [] }, shapeSchema);
}
export {
object
};
//# sourceMappingURL=chunk-M3HYPZLE.mjs.map