diff --git a/src/validateFieldsNatively.ts b/src/validateFieldsNatively.ts index 38f06f2d..640799f6 100644 --- a/src/validateFieldsNatively.ts +++ b/src/validateFieldsNatively.ts @@ -23,7 +23,7 @@ export const validateFieldsNatively = ( if (field && field.ref && 'reportValidity' in field.ref) { setCustomValidity(field.ref, fieldPath, errors) } else if (field.refs) { - field.refs.forEach((ref: HTMLInputElement, i) => setCustomValidity(ref, fieldPath + '.' + i, errors)) + field.refs.forEach((ref: HTMLInputElement) => setCustomValidity(ref, fieldPath, errors)) } } }; diff --git a/zod/src/zod.ts b/zod/src/zod.ts index 0f167554..847afa01 100644 --- a/zod/src/zod.ts +++ b/zod/src/zod.ts @@ -2,58 +2,17 @@ import { appendErrors, FieldError, FieldErrors, - set, - get, - Field, - ResolverOptions, } from 'react-hook-form'; import { z } from 'zod'; +import { toNestError, validateFieldsNatively } from '@hookform/resolvers'; import type { Resolver } from './types'; -// Native validation (web only) -export const validateFieldsNatively = ( - errors: Record, - options: ResolverOptions, -): void => { - for (const fieldPath in options.fields) { - const field = options.fields[fieldPath]; - - if (field && field.ref && 'reportValidity' in field.ref) { - const error = get(errors, fieldPath) as FieldError | undefined; - - field.ref.setCustomValidity((error && error.message) || ''); - - field.ref.reportValidity(); - } - } -}; - -const toNestError = ( - errors: Record, - options: ResolverOptions, -): FieldErrors => { - options.shouldUseNativeValidation && validateFieldsNatively(errors, options); - - const fieldErrors = {} as FieldErrors; - for (const path in errors) { - const field = get(options.fields, path) as Field['_f'] | undefined; - - set( - fieldErrors, - path, - Object.assign(errors[path], { ref: field && field.ref }), - ); - } - - return fieldErrors; -}; - const parseErrorSchema = ( zodErrors: z.ZodIssue[], validateAllFieldCriteria: boolean, ) => { const errors: Record = {}; - for (; zodErrors.length; ) { + for (; zodErrors.length;) { const error = zodErrors[0]; const { code, message, path } = error; const _path = path.join('.'); @@ -100,31 +59,31 @@ const parseErrorSchema = ( export const zodResolver: Resolver = (schema, schemaOptions, resolverOptions = {}) => - async (values, _, options) => { - try { - const data = await schema[ - resolverOptions.mode === 'sync' ? 'parse' : 'parseAsync' - ](values, schemaOptions); + async (values, _, options) => { + try { + const data = await schema[ + resolverOptions.mode === 'sync' ? 'parse' : 'parseAsync' + ](values, schemaOptions); - options.shouldUseNativeValidation && validateFieldsNatively({}, options); + options.shouldUseNativeValidation && validateFieldsNatively({}, options); - return { - errors: {} as FieldErrors, - values: data, - }; - } catch (error: any) { - return { - values: {}, - errors: error.isEmpty - ? {} - : toNestError( + return { + errors: {} as FieldErrors, + values: data, + }; + } catch (error: any) { + return { + values: {}, + errors: error.isEmpty + ? {} + : toNestError( parseErrorSchema( error.errors, !options.shouldUseNativeValidation && - options.criteriaMode === 'all', + options.criteriaMode === 'all', ), options, ), - }; - } - }; + }; + } + };