@modular-forms/react
Version:
The modular and type-safe form library for React
23 lines (22 loc) • 618 B
JavaScript
/**
* Creates a validation functions that parses the Zod schema of a form.
*
* @param schema A Zod schema.
*
* @returns A validation function.
*/
export function zodForm(schema) {
return async (values) => {
const result = await schema.safeParseAsync(values);
const formErrors = {};
if (!result.success) {
for (const issue of result.error.issues) {
const path = issue.path.join('.');
if (!formErrors[path]) {
formErrors[path] = issue.message;
}
}
}
return formErrors;
};
}