Schema validation
The schema prop
Added in v0.17.
Pass any Standard Schema (Zod 3.24+ / 4, Valibot 1, ArkType 2, Yup 1.7+, Effect Schema, …). No resolver package is needed:
import { z } from "zod";
const SignupSchema = z.object({
name: z.string().trim().min(1, "Name is required"),
age: z.coerce.number().int().min(18),
});
<Form
schema={SignupSchema}
defaultValues={{ name: "", age: "" }}
config={{ name: { type: "text" }, age: { type: "text" } }}
onSubmit={(data) => {
data.age.toFixed(); // number: the schema output
}}
/>;
The types follow the schema, the same way React Hook Form's TTransformedValues does:
So a schema that trims, coerces or transforms gives onSubmit the parsed result, and type errors point at the right side. Mapping the output to an API payload belongs in onSubmit; there is no separate transform option.
- Field-level
rules are ignored when schema is set (a dev warning lists them).
schema and formOptions.resolver cannot be combined (type error; at runtime schema wins with a warning).
- Issues map to field errors by path; an issue on an array itself goes to
<path>.root, like useFieldArray rules.
import { useState } from "react";
import { z } from "zod";
import { Form } from "../form.setup";
// The inputs hold strings; the schema trims the name and parses the age
const signupSchema = z.object({
name: z.string().trim().min(1, "Name is required"),
email: z.email("Invalid email"),
age: z.coerce.number<string>().int().min(18, "Must be 18 or older"),
});
export default function SignupForm() {
const [output, setOutput] = useState<z.output<typeof signupSchema>>();
return (
<Form
schema={signupSchema}
defaultValues={{ name: "", email: "", age: "" }}
config={{
name: { type: "text", label: "Name" },
email: { type: "text", label: "Email" },
age: { type: "text", label: "Age", props: { inputMode: "numeric" } },
}}
// `data` is the schema output: the name trimmed, the age a number
onSubmit={(data) => setOutput(data)}
>
<button type="submit">Sign up</button>
{output && <pre data-testid="output">{JSON.stringify(output)}</pre>}
</Form>
);
}
The same form with Valibot, ArkType, Yup, Effect Schema, Ajv, Joi and Superstruct: Schema example.
For libraries without Standard Schema support (e.g. Ajv, Joi, Superstruct, or Yup before 1.7) or custom resolvers, pass a resolver from @hookform/resolvers.
Setup
Install your schema library and the RHF resolvers package:
# Zod
pnpm add zod @hookform/resolvers
# Yup
pnpm add yup @hookform/resolvers
Pass the resolver via formOptions:
import { z } from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
const schema = z.object({
name: z.string().min(1, "Name is required"),
email: z.string().email("Invalid email"),
});
type FormData = z.infer<typeof schema>;
<Form<FormData>
formOptions={{ resolver: zodResolver(schema) }}
config={{
name: { type: "text", label: "Name" },
email: { type: "text", label: "Email" },
}}
onSubmit={(data) => console.log(data)}
/>;
Resolver vs field-level rules
When schema or formOptions.resolver is provided, field-level rules are automatically disabled. BlueForm detects the conflict and strips rules from useController to prevent both running simultaneously and producing unpredictable error messages.
A dev-mode warning will appear if both are present:
[react-headless-form] `rules` defined in field config are automatically disabled when `schema` or
`formOptions.resolver` is provided. Define all validation in your schema instead.
Bottom line: pick one approach and stick with it.
Handling undefined field values
BlueForm fields start as undefined when untouched. Most schema libraries expect a specific type and will throw a type error instead of a custom message when they receive undefined.
The recommended fix is to provide defaultValue for each field:
config={{
name: {
type: "text",
defaultValue: "", // field starts as "" instead of undefined
},
age: {
type: "text",
defaultValue: 0,
},
}}
Alternatively, set defaultValues at the form level:
<Form
defaultValues={{ name: "", email: "", age: 0 }}
formOptions={{ resolver: zodResolver(schema) }}
...
/>
Zod-specific note
z.string() does not accept undefined and will throw a type error before reaching your custom message. Use z.preprocess to coerce undefined to "":
// without defaultValue in config
const schema = z.object({
name: z.preprocess((v) => v ?? "", z.string().min(1, "Name is required")),
});
// with defaultValue: "" in config — no preprocess needed
const schema = z.object({
name: z.string().min(1, "Name is required"),
});
Yup-specific note
Yup's .required() correctly handles empty strings out of the box. Setting defaultValue: "" in field config is sufficient — no additional schema workaround needed.
Yup defaults to abortEarly: true, which stops at the first error. To show all errors simultaneously (recommended for form UX), pass { abortEarly: false }:
formOptions={{ resolver: yupResolver(schema, { abortEarly: false }) }}
Example: Zod
import { z } from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
const schema = z.object({
username: z.string().min(1, "Username is required"),
password: z.string().min(6, "Password must be at least 6 characters"),
});
type LoginData = z.infer<typeof schema>;
<Form<LoginData>
defaultValues={{ username: "", password: "" }}
formOptions={{ resolver: zodResolver(schema) }}
config={{
username: { type: "text", label: "Username" },
password: { type: "text", label: "Password", props: { type: "password" } },
}}
onSubmit={(data) => console.log(data)}
>
<button type="submit">Login</button>
</Form>;
Example: Yup
import * as yup from "yup";
import { yupResolver } from "@hookform/resolvers/yup";
const schema = yup.object({
username: yup.string().required("Username is required"),
password: yup
.string()
.min(6, "Password must be at least 6 characters")
.required(),
});
type LoginData = yup.InferType<typeof schema>;
<Form<LoginData>
defaultValues={{ username: "", password: "" }}
formOptions={{ resolver: yupResolver(schema, { abortEarly: false }) }}
config={{
username: { type: "text", label: "Username" },
password: { type: "text", label: "Password", props: { type: "password" } },
}}
onSubmit={(data) => console.log(data)}
>
<button type="submit">Login</button>
</Form>;