@@ -2,58 +2,17 @@ import {
2
2
appendErrors ,
3
3
FieldError ,
4
4
FieldErrors ,
5
- set ,
6
- get ,
7
- Field ,
8
- ResolverOptions ,
9
5
} from 'react-hook-form' ;
10
6
import { z } from 'zod' ;
7
+ import { toNestError , validateFieldsNatively } from '@hookform/resolvers' ;
11
8
import type { Resolver } from './types' ;
12
9
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
-
51
10
const parseErrorSchema = (
52
11
zodErrors : z . ZodIssue [ ] ,
53
12
validateAllFieldCriteria : boolean ,
54
13
) => {
55
14
const errors : Record < string , FieldError > = { } ;
56
- for ( ; zodErrors . length ; ) {
15
+ for ( ; zodErrors . length ; ) {
57
16
const error = zodErrors [ 0 ] ;
58
17
const { code, message, path } = error ;
59
18
const _path = path . join ( '.' ) ;
@@ -100,31 +59,31 @@ const parseErrorSchema = (
100
59
101
60
export const zodResolver : Resolver =
102
61
( 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 ) ;
108
67
109
- options . shouldUseNativeValidation && validateFieldsNatively ( { } , options ) ;
68
+ options . shouldUseNativeValidation && validateFieldsNatively ( { } , options ) ;
110
69
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 (
121
80
parseErrorSchema (
122
81
error . errors ,
123
82
! options . shouldUseNativeValidation &&
124
- options . criteriaMode === 'all' ,
83
+ options . criteriaMode === 'all' ,
125
84
) ,
126
85
options ,
127
86
) ,
128
- } ;
129
- }
130
- } ;
87
+ } ;
88
+ }
89
+ } ;
0 commit comments