Interface for validating object structures with defined schemas for each property.

example

const objectSchema = val.ObjectSchema({ name: val.StringSchema().minLength(3), age: val.NumberSchema().min(18), });

const validData = objectSchema.validate({ name: "John", age: 25 }); // Passes validation

throws

ValidationError If the object does not match the schema.

example

const invalidData = { name: "Jo", age: 17 }; objectSchema.validate(invalidData); // Throws ValidationError // Example error message: // "Validation failed for property 'name': ✖ String is too short. Minimum length is 3 → at name" // "Validation failed for property 'age': ✖ Expected a number greater than or equal to 18 → at age"

Type parameters

  • T: SchemaMap

    A map of property names to their respective schemas.

Hierarchy

Index

Properties

Methods

Properties

isOptional

isOptional: boolean

Indicates whether the schema allows undefined as a valid value.

example

const schema = val.string().optional(); console.log(schema.isOptional); // true

type

type: string

The type of data that the schema validates, represented as a string.

example

const schema = val.string(); console.log(schema.type); // "string"

Methods

optional

  • optional(): this
  • Marks the schema as optional, allowing undefined as a valid value.

    example

    const objectSchema = val.ObjectSchema({ name: val.StringSchema().minLength(3), age: val.NumberSchema().min(18), }).optional();

    objectSchema.validate(undefined); // Passes validation

    Returns this

    The current schema instance.

parse

  • parse(data: unknown): {}
  • Parses the input data and returns the parsed value.

    This method is typically used to coerce or transform the input data into the desired format.

    throws

    ValidationError If the input data cannot be parsed into the desired format.

    example

    const schema = val.number().coerce(); const parsedValue = schema.parse("42"); // Returns 42 as a number

    Parameters

    • data: unknown

      The input data to parse.

    Returns {}

    The parsed value.

validate

  • validate(data: unknown): {}
  • Validates the input data against the object schema.

    throws

    ValidationError If any property in the object does not match its schema.

    example

    const objectSchema = val.ObjectSchema({ name: val.StringSchema().minLength(3), age: val.NumberSchema().min(18), });

    const validData = objectSchema.validate({ name: "Alice", age: 30 }); // Passes validation

    const invalidData = { name: "Al", age: 15 }; objectSchema.validate(invalidData); // Throws ValidationError // Example error message: // "Validation failed for property 'name': ✖ String is too short. Minimum length is 3 → at name" // "Validation failed for property 'age': ✖ Expected a number greater than or equal to 18 → at age"

    Parameters

    • data: unknown

      The data to validate.

    Returns {}

    The validated object with all properties conforming to their respective schemas.

Generated using TypeDoc