@conform-to/zod
Zod schema resolver for conform
API Reference
resolve
This resolves zod schema to a conform schema:
import { useFieldset } from '@conform-to/react';
import { resolve } from '@conform-to/zod';
import { z } from 'zod';
const schema = z.object({
email: z.string(),
password: z.string(),
});
function RandomForm() {
const [setupFieldset, { email, password }] = useFieldset(resolve(schema));
}
parse
The parse
function could be used to parse the FormData on client side:
const schema = z.object({
});
form.addEventListener('submit', (e) => {
e.preventDefault();
const formData = new FormData(e.target);
const result = parse(formData, schema);
console.log(result.state);
console.log(result.value);
console.log(result.error);
});
Or parse the request payload on server side (e.g. Remix):
import { parse } from '@conform-to/zod';
import { z } from 'zod';
const schema = z.object({
});
export let action = async ({ request }) => {
const formData = await request.formData();
const result = parse(formData, schema);
if (result.state !== 'accepted') {
return json(result);
}
};
export default function SomeRoute() {
const result = useActionData();
}