@modular-forms/react
Version:
The modular and type-safe form library for React
23 lines (22 loc) • 621 B
JavaScript
import { getDotPath, safeParseAsync, } from 'valibot';
/**
* Creates a validation functions that parses the Valibot schema of a form.
*
* @param schema A Valibot schema.
*
* @returns A validation function.
*/
export function valiForm(schema) {
return async (values) => {
const result = await safeParseAsync(schema, values, {
abortPipeEarly: true,
});
const formErrors = {};
if (result.issues) {
for (const issue of result.issues) {
formErrors[getDotPath(issue)] = issue.message;
}
}
return formErrors;
};
}