Skip to main content
← Back to docs

Playground

Try schema transforms and the form generator in your browser. The five code examples open an editable Sandpack preview.

Schema transforms

Input text, parsed output

Try an invalid value, then correct it. The field keeps text while the submit callback receives a number.

Edit a string. A valid submit delivers a number that is at least 3.

What the form holds while you edit

{
  "quantity": ""
}

What a valid callback receives

No valid submission yet

Waiting for a valid submission

Schema used by this lab

const schema = z.object({
  quantity: z.string()
    .transform(value => value.trim())
    .refine(value => /^\d+$/.test(value), "Enter a whole number")
    .transform(Number)
    .refine(Number.isSafeInteger, "Enter a safe whole number")
    .refine(value => value >= 3, "Enter 3 or more"),
});

const form = useForm({ schema, defaultValues: { quantity: "" } });
form.handleSubmit(({ quantity }) => save(quantity));