Documentation

class Schema

A class that represents a validation schema for input values, objects, and arrays.

class Schema<TBuilder extends Schema.Builder = Schema.Builder>;

Description

This class validates individual values (strings, numbers, booleans, dates) as well as objects and arrays. Error messages can be customized per validation step; use Schema.errors to override defaults. Schema instances are reusable and can describe nested objects and arrays.

This class is used by FormState for form validation.

The constructor accepts a callback that uses the provided builder functions to define a validation schema. The resulting instance can parse unknown values safely, either throwing an error or returning a success/failure result with parsed data or error messages.

Examples

const userSchema = new Schema((f) =>
  f.object({
    name: f.string().required("Name is required"),
    age: f.number().required().positive(),
    email: f.string().email("Invalid email")
  })
);

const result = userSchema.safeParse(formData);
if (result.success) {
  console.log(result.data); // Typed as { name: string, age: number, email: string }
} else {
  console.log(result.error); // First error message
  console.log(result.errors); // All object property errors
}

Constructor

Type members

Static members

Instance members