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.
48 lines (45 loc) • 1.81 kB
text/typescript
import { WithObject } from '../../commonTypes.mjs';
import { ObjectShapeSchemaType, CommonSchema, ValidatorContext } from '../../core.mjs';
import '../../InferType.mjs';
import '../../helpers/constants.mjs';
declare class ObjectSchema extends CommonSchema {
protected _object: number;
constructor(ctx: ValidatorContext, shapeSchema: ObjectShapeSchemaType);
private validateObjectEntry;
/**
* @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;
}
/**
* @description Creates a new schema for validating objects where each property must match the specified schema.
* @template T
* @param {T} shapeSchema - The schema that each property of the object must match.
* @returns {WithObject<ObjectSchema, T>} A new instance of `ObjectSchema` for validating objects with properties matching the specified schema.
* @example
* const schema = object({
* name: string(),
* age: number()
* });
* parseOrFail(schema, { name: 'John', age: 30 }); // Validates successfully
* parseOrFail(schema, { name: 'John', age: '30' }); // Throws a validation error
*
* @instance Of ObjectSchema
*/
declare function object<T extends ObjectShapeSchemaType>(shapeSchema: T): WithObject<ObjectSchema, T>;
export { object };