@@ -2,58 +2,17 @@ import {
22 appendErrors ,
33 FieldError ,
44 FieldErrors ,
5- set ,
6- get ,
7- Field ,
8- ResolverOptions ,
95} from 'react-hook-form' ;
106import { z } from 'zod' ;
7+ import { toNestError , validateFieldsNatively } from '@hookform/resolvers' ;
118import type { Resolver } from './types' ;
129
13- // Native validation (web only)
14- export const validateFieldsNatively = < TFieldValues > (
15- errors : Record < string , FieldError > ,
16- options : ResolverOptions < TFieldValues > ,
17- ) : void => {
18- for ( const fieldPath in options . fields ) {
19- const field = options . fields [ fieldPath ] ;
20-
21- if ( field && field . ref && 'reportValidity' in field . ref ) {
22- const error = get ( errors , fieldPath ) as FieldError | undefined ;
23-
24- field . ref . setCustomValidity ( ( error && error . message ) || '' ) ;
25-
26- field . ref . reportValidity ( ) ;
27- }
28- }
29- } ;
30-
31- const toNestError = < TFieldValues > (
32- errors : Record < string , FieldError > ,
33- options : ResolverOptions < TFieldValues > ,
34- ) : FieldErrors < TFieldValues > => {
35- options . shouldUseNativeValidation && validateFieldsNatively ( errors , options ) ;
36-
37- const fieldErrors = { } as FieldErrors < TFieldValues > ;
38- for ( const path in errors ) {
39- const field = get ( options . fields , path ) as Field [ '_f' ] | undefined ;
40-
41- set (
42- fieldErrors ,
43- path ,
44- Object . assign ( errors [ path ] , { ref : field && field . ref } ) ,
45- ) ;
46- }
47-
48- return fieldErrors ;
49- } ;
50-
5110const parseErrorSchema = (
5211 zodErrors : z . ZodIssue [ ] ,
5312 validateAllFieldCriteria : boolean ,
5413) => {
5514 const errors : Record < string , FieldError > = { } ;
56- for ( ; zodErrors . length ; ) {
15+ for ( ; zodErrors . length ; ) {
5716 const error = zodErrors [ 0 ] ;
5817 const { code, message, path } = error ;
5918 const _path = path . join ( '.' ) ;
@@ -100,31 +59,31 @@ const parseErrorSchema = (
10059
10160export const zodResolver : Resolver =
10261 ( schema , schemaOptions , resolverOptions = { } ) =>
103- async ( values , _ , options ) => {
104- try {
105- const data = await schema [
106- resolverOptions . mode === 'sync' ? 'parse' : 'parseAsync'
107- ] ( values , schemaOptions ) ;
62+ async ( values , _ , options ) => {
63+ try {
64+ const data = await schema [
65+ resolverOptions . mode === 'sync' ? 'parse' : 'parseAsync'
66+ ] ( values , schemaOptions ) ;
10867
109- options . shouldUseNativeValidation && validateFieldsNatively ( { } , options ) ;
68+ options . shouldUseNativeValidation && validateFieldsNatively ( { } , options ) ;
11069
111- return {
112- errors : { } as FieldErrors ,
113- values : data ,
114- } ;
115- } catch ( error : any ) {
116- return {
117- values : { } ,
118- errors : error . isEmpty
119- ? { }
120- : toNestError (
70+ return {
71+ errors : { } as FieldErrors ,
72+ values : data ,
73+ } ;
74+ } catch ( error : any ) {
75+ return {
76+ values : { } ,
77+ errors : error . isEmpty
78+ ? { }
79+ : toNestError (
12180 parseErrorSchema (
12281 error . errors ,
12382 ! options . shouldUseNativeValidation &&
124- options . criteriaMode === 'all' ,
83+ options . criteriaMode === 'all' ,
12584 ) ,
12685 options ,
12786 ) ,
128- } ;
129- }
130- } ;
87+ } ;
88+ }
89+ } ;
0 commit comments