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
new Schema()
Creates a new schema instance.
Type members
type Schema.ParseResult static
The result of a schema parse operation.type Schema.Infer static
A utility type that infers the parsed value type from a Builder or Schema instance.type Schema.Initializer static
A type that can be used to initialize a Schema instance as the constructor argument.type Schema.Builders static
The type of the Schema.build object, passed to the initializer function.interface Schema.StringBuilder static
A validation builder for string values.interface Schema.NumberBuilder static
A validation builder for number values.interface Schema.ArrayBuilder static
A validation builder for array values.interface Schema.BooleanBuilder static
A validation builder for boolean values.interface Schema.DateBuilder static
A validation builder for Date values.interface Schema.ObjectBuilder static
A validation builder for object values.interface Schema.LiteralBuilder static
A validation builder for literal values.type Schema.StandardResult static
Standard Schema v1 result type.interface Schema.StandardSchemaV1 static
Standard Schema v1 interface (inlined to avoid dependency).class Builder static
A class that represents a validation builder for an input value.
Static members
Schema.errors static
The default error messages; override with localizable messages at application startup.Schema.build static
An object containing functions that create Builder instances for specific types.
Instance members
schema
The schema definition, as a read-only Schema.Builder instance.parse()
Parses and validates the input, throwing an error if validation fails.safeParse()
Parses and validates the input, returning a Schema.ParseResult object.
