@conform-to/yup
Yup schema resolver for conform
API Reference
resolve
It resolves yup schema to a conform schema:
import { useForm, useFieldset } from '@conform-to/react';
import { resolve } from '@conform-to/yup';
import * as yup from 'yup';
const schema = resolve(
yup.object({
email: yup.string().required(),
password: yup.string().required(),
})
);
function ExampleForm() {
const formProps = useForm({
validate: schema.validate
onSubmit: event => {
const payload = new FormData(e.target);
const submission = schema.parse(payload);
console.log(submission.state);
console.log(submission.data);
console.log(submission.form.value);
console.log(submission.form.error);
};
})
const [setupFieldset, { email, password }] = useFieldset({
constraint: schema.constraint
});
}
Or parse the request payload on server side (e.g. Remix):
import { resolve } from '@conform-to/yup';
import * as yup from 'yup';
const schema = resolve(
yup.object({
}),
);
export let action = async ({ request }) => {
const formData = await request.formData();
const submission = schema.parse(formData);
if (submission.state !== 'accepted') {
return json(submission.form);
}
};
export default function ExampleRoute() {
const formState = useActionData();
}