@conform-to/zod
Advanced tools
Weekly downloads
Changelog
v0.6.1
function Example() {
const [form, { email, password }] = useForm({
// This is now deprecated in favor of the new configs
initialReport: 'onBlur',
// Define when Conform should start validation. Default `onSubmit`.
shouldValidate: 'onBlur',
// Define when Conform should revalidate again. Default `onInput`.
shouldRevalidate: 'onBlur',
});
}
function Example() {
const ref = useRef<HTMLFormElement>(null);
const [form, { email, password }] = useForm({
ref,
});
// `form.ref / form.props.ref` will now be the same as `ref`
return (
<form {...form.props}>
{/* ... */}
</form>
);
}
descriptionId
to support accessible input hint. (#126)function Example() {
const [form, { email }] = useForm();
return (
<form {...form.props}>
<label htmlFor={email.id}>Email</label>
<input {...conform.input(email, { type: "email", description: true })} />
{/* If invalid, it will set the aria-describedby to "${errorId} ${descriptionId}" */}
<div id={email.descriptionId}>
Email hint
</div>
<div id={email.errorId}>
{email.error}
</div>
</form>
)
}
Full Changelog: https://github.com/edmundhung/conform/compare/v0.6.0...v0.6.1
Readme
This tries to infer constraint of each field based on the zod schema. This is useful for:
:required
import { useForm } from '@conform-to/react';
import { getFieldsetConstraint } from '@conform-to/zod';
import { z } from 'zod';
const schema = z.object({
email: z.string().min(1, 'Email is required'),
password: z.string().min(1, 'Password is required'),
});
function Example() {
const [form, { email, password }] = useForm({
constraint: getFieldsetConstraint(schema),
});
// ...
}
It parses the formData and returns a submission result with the validation error. If no error is found, the parsed data will also be populated as submission.value
.
import { useForm } from '@conform-to/react';
import { parse } from '@conform-to/zod';
import { z } from 'zod';
const schema = z.object({
email: z.string().min(1, 'Email is required'),
password: z.string().min(1, 'Password is required'),
});
function ExampleForm() {
const [form, { email, password }] = useForm({
onValidate({ formData }) {
return parse(formData, { schema });
},
});
// ...
}
Or when parsing the formData on server side (e.g. Remix):
import { useForm } from '@conform-to/react';
import { parse } from '@conform-to/zod';
import { z } from 'zod';
const schema = z.object({
// Define the schema with zod
});
export let action = async ({ request }) => {
const formData = await request.formData();
const submission = await parse(formData, {
// If you need extra validation on server side
schema: schema.refine(/* ... */),
// If the schema definition includes async validation
async: true,
});
if (!submission.value || submission.intent !== 'submit') {
return submission;
}
// ...
};
FAQs
Conform helpers for integrating with Zod
The npm package @conform-to/zod receives a total of 437 weekly downloads. As such, @conform-to/zod popularity was classified as not popular.
We found that @conform-to/zod demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket installs a Github app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.