UNPKG

@next-safe-action/adapter-react-hook-form

Version:

This adapter offers a way to seamlessly integrate next-safe-action with react-hook-form.

32 lines (31 loc) 856 B
// src/index.ts function mapToHookFormErrors(validationErrors, props) { if (!validationErrors || Object.keys(validationErrors).length === 0) { return void 0; } const fieldErrors = {}; function mapper(ve, paths = []) { for (const key of Object.keys(ve)) { if (typeof ve[key] === "object" && ve[key] && !Array.isArray(ve[key])) { mapper(ve[key], [...paths, key]); } if (key === "_errors" && Array.isArray(ve[key])) { let ref = fieldErrors; for (let i = 0; i < paths.length - 1; i++) { const p = paths[i]; ref[p] ??= {}; ref = ref[p]; } const path = paths.at(-1) ?? "root"; ref[path] = { type: "validate", message: ve[key].join(props?.joinBy ?? " "), }; } } } mapper(validationErrors ?? {}); return fieldErrors; } export { mapToHookFormErrors }; //# sourceMappingURL=index.mjs.map