# Schemas

> Plugins that let the form take more from your schema library, starting with default values.

Source: https://forms.lunarhue.dev/react/plugins/schemas

A form already [validates with any schema](https://forms.lunarhue.dev/react/basics/schema-validation.md), 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.

```tsx title="sign-up.tsx"
import { createForms, inputProps, useForm } from "@lunarhue/forms-react";
import { zod } from "@lunarhue/forms-schema/zod";
import { z } from "zod";

const forms = createForms({ plugins: [zod()] });

const SignUp = z.object({
  name: z.string().min(1, "Enter your name."),
  // A number has no empty value, so it starts undefined.
  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(),
});

// No defaultValues: zod() works them out from the schema.
const signUp = forms.define({
  schema: SignUp,
  onSubmit: (value) => `Signed up ${value.name} on ${value.plan}.`,
});

export default function SignUpForm() {
  const form = useForm(signUp, { id: "sign-up" });
  return (
    <form.Form>
      {(formProps) => (
        <form {...formProps}>
          <form.Field name="name">
            {(field) => (
              <label>
                Name
                <input {...inputProps(field)} />
                {field.shownErrors[0]?.message}
              </label>
            )}
          </form.Field>
          <form.Field name="age">
            {(field) => (
              <label>
                Age
                <input {...inputProps(field, { kind: "number" })} />
                {field.shownErrors[0]?.message}
              </label>
            )}
          </form.Field>
          <form.Field name="plan">
            {(field) => (
              <label>
                Plan
                <select {...inputProps(field, { kind: "select" })}>
                  <option value="free">Free</option>
                  <option value="pro">Pro</option>
                </select>
              </label>
            )}
          </form.Field>
          <form.Field name="newsletter">
            {(field) => (
              <label>
                <input {...inputProps(field, { kind: "checkbox" })} />
                Send me the newsletter
              </label>
            )}
          </form.Field>
          <button type="submit">Sign up</button>
          <form.State select={(state) => state.submission}>
            {(submission) => <p role="status">{submission.status === "succeeded" ? submission.result : null}</p>}
          </form.State>
        </form>
      )}
    </form.Form>
  );
}
```

## 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:

```bash
npm install @lunarhue/forms-schema zod
```

```ts
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](https://forms.lunarhue.dev/react/writing-plugins/schema-adapters.md#ts2353-from-define-in-a-function-generic-over-the-schema)
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](https://forms.lunarhue.dev/react/writing-plugins/schema-adapters.md).
