UNPKG

@modular-forms/qwik

Version:

The modular and type-safe form library for Qwik

27 lines (26 loc) 874 B
import { $, implicit$FirstArg } from '@builder.io/qwik'; import { getDotPath, safeParseAsync, } from 'valibot'; /** * See {@link valiForm$} */ export function valiFormQrl(schema) { return $(async (values) => { const resolvedSchema = await schema.resolve(); const result = await safeParseAsync(typeof resolvedSchema === 'function' ? resolvedSchema() : resolvedSchema, values, { abortPipeEarly: true }); const formErrors = {}; if (result.issues) { for (const issue of result.issues) { formErrors[getDotPath(issue)] = issue.message; } } return formErrors; }); } /** * Creates a validation functions that parses the Valibot schema of a form. * * @param schema A Valibot schema. * * @returns A validation function. */ export const valiForm$ = implicit$FirstArg(valiFormQrl);