Schemas
Plugins that let the form take more from your schema library, starting with default values.
A form already validates with any schema, but
core only sees what Standard Schema exposes: a validate function and types.
Everything else the schema says stays inside it, so you write the shape a
second time as defaultValues. A schema plugin knows its library and reads
the schema itself. Today it gives the form its starting values. The demo has
no defaultValues at all.
Zod and Valibot
zod() from @lunarhue/forms-schema/zod takes Zod 4 schemas, from zod or
zod/mini. valibot() from @lunarhue/forms-schema/valibot takes Valibot
schemas, sync or async. The library is an optional peer dependency, so install
only the one you use:
import { zod } from "@lunarhue/forms-schema/zod";
import { z } from "zod";
const forms = createForms({ plugins: [zod()] });
const signUp = forms.define({
schema: z.object({
name: z.string().min(1, "Enter your name."),
age: z.number("Enter your age.").int().min(18, "You must be 18 or over."),
plan: z.enum(["free", "pro"]).default("free"),
newsletter: z.boolean(),
}),
onSubmit: (value) => value,
});Starting values
The starting values come from the schema: strings start empty, booleans
false, arrays empty, and a declared .default() wins. A number or an enum has
no empty value, so it starts undefined. defaultValues is optional and
overrides any part of what the schema gives.
A schema from a library no installed plugin takes still validates. It just
needs complete defaultValues, as in Basics.
What comes next
The plugin is where more of the schema will reach the form. Constraints such
as min, max and maxLength are planned, so an input can take its limits
from the schema instead of repeating them as props.
A helper generic over the schema
A helper that takes any schema and returns a definition fails with TS2353 when its type parameter is the schema type. Make it generic over the schema’s values instead. Writing a plugin explains why and has both fixes.
Another library
A schema plugin for another Standard Schema library is a plugin with a
schema adapter. See
Writing a plugin.
Last updated