@hookform/resolvers 2.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +420 -0
- package/class-validator/dist/class-validator.d.ts +2 -0
- package/class-validator/dist/class-validator.js +2 -0
- package/class-validator/dist/class-validator.js.map +1 -0
- package/class-validator/dist/class-validator.mjs +2 -0
- package/class-validator/dist/class-validator.modern.js +2 -0
- package/class-validator/dist/class-validator.modern.js.map +1 -0
- package/class-validator/dist/class-validator.module.js +2 -0
- package/class-validator/dist/class-validator.module.js.map +1 -0
- package/class-validator/dist/class-validator.umd.js +2 -0
- package/class-validator/dist/class-validator.umd.js.map +1 -0
- package/class-validator/dist/index.d.ts +2 -0
- package/class-validator/dist/types.d.ts +8 -0
- package/class-validator/package.json +19 -0
- package/class-validator/src/__tests__/Form-native-validation.tsx +83 -0
- package/class-validator/src/__tests__/Form.tsx +55 -0
- package/class-validator/src/__tests__/__fixtures__/data.ts +88 -0
- package/class-validator/src/__tests__/__snapshots__/class-validator.ts.snap +207 -0
- package/class-validator/src/__tests__/class-validator.ts +87 -0
- package/class-validator/src/class-validator.ts +62 -0
- package/class-validator/src/index.ts +2 -0
- package/class-validator/src/types.ts +22 -0
- package/computed-types/dist/computed-types.d.ts +2 -0
- package/computed-types/dist/computed-types.js +2 -0
- package/computed-types/dist/computed-types.js.map +1 -0
- package/computed-types/dist/computed-types.mjs +2 -0
- package/computed-types/dist/computed-types.modern.js +2 -0
- package/computed-types/dist/computed-types.modern.js.map +1 -0
- package/computed-types/dist/computed-types.module.js +2 -0
- package/computed-types/dist/computed-types.module.js.map +1 -0
- package/computed-types/dist/computed-types.umd.js +2 -0
- package/computed-types/dist/computed-types.umd.js.map +1 -0
- package/computed-types/dist/index.d.ts +2 -0
- package/computed-types/dist/types.d.ts +2 -0
- package/computed-types/package.json +17 -0
- package/computed-types/src/__tests__/Form-native-validation.tsx +85 -0
- package/computed-types/src/__tests__/Form.tsx +54 -0
- package/computed-types/src/__tests__/__fixtures__/data.ts +72 -0
- package/computed-types/src/__tests__/__snapshots__/computed-types.ts.snap +55 -0
- package/computed-types/src/__tests__/computed-types.ts +24 -0
- package/computed-types/src/computed-types.ts +43 -0
- package/computed-types/src/index.ts +2 -0
- package/computed-types/src/types.ts +14 -0
- package/dist/index.d.ts +2 -0
- package/dist/resolvers.js +2 -0
- package/dist/resolvers.js.map +1 -0
- package/dist/resolvers.mjs +2 -0
- package/dist/resolvers.mjs.map +1 -0
- package/dist/resolvers.module.js +2 -0
- package/dist/resolvers.module.js.map +1 -0
- package/dist/resolvers.umd.js +2 -0
- package/dist/resolvers.umd.js.map +1 -0
- package/dist/toNestError.d.ts +2 -0
- package/dist/validateFieldsNatively.d.ts +2 -0
- package/io-ts/dist/arrayToPath.d.ts +3 -0
- package/io-ts/dist/errorsToRecord.d.ts +5 -0
- package/io-ts/dist/index.d.ts +2 -0
- package/io-ts/dist/io-ts.d.ts +2 -0
- package/io-ts/dist/io-ts.js +2 -0
- package/io-ts/dist/io-ts.js.map +1 -0
- package/io-ts/dist/io-ts.mjs +2 -0
- package/io-ts/dist/io-ts.modern.js +2 -0
- package/io-ts/dist/io-ts.modern.js.map +1 -0
- package/io-ts/dist/io-ts.module.js +2 -0
- package/io-ts/dist/io-ts.module.js.map +1 -0
- package/io-ts/dist/io-ts.umd.js +2 -0
- package/io-ts/dist/io-ts.umd.js.map +1 -0
- package/io-ts/dist/types.d.ts +7 -0
- package/io-ts/package.json +19 -0
- package/io-ts/src/__tests__/Form-native-validation.tsx +89 -0
- package/io-ts/src/__tests__/Form.tsx +65 -0
- package/io-ts/src/__tests__/__fixtures__/data.ts +129 -0
- package/io-ts/src/__tests__/__snapshots__/io-ts.ts.snap +89 -0
- package/io-ts/src/__tests__/io-ts.ts +37 -0
- package/io-ts/src/arrayToPath.ts +18 -0
- package/io-ts/src/errorsToRecord.ts +131 -0
- package/io-ts/src/index.ts +2 -0
- package/io-ts/src/io-ts.ts +33 -0
- package/io-ts/src/types.ts +23 -0
- package/joi/dist/index.d.ts +2 -0
- package/joi/dist/joi.d.ts +2 -0
- package/joi/dist/joi.js +2 -0
- package/joi/dist/joi.js.map +1 -0
- package/joi/dist/joi.mjs +2 -0
- package/joi/dist/joi.modern.js +2 -0
- package/joi/dist/joi.modern.js.map +1 -0
- package/joi/dist/joi.module.js +2 -0
- package/joi/dist/joi.module.js.map +1 -0
- package/joi/dist/joi.umd.js +2 -0
- package/joi/dist/joi.umd.js.map +1 -0
- package/joi/dist/types.d.ts +5 -0
- package/joi/package.json +17 -0
- package/joi/src/__tests__/Form-native-validation.tsx +89 -0
- package/joi/src/__tests__/Form.tsx +61 -0
- package/joi/src/__tests__/__fixtures__/data.ts +85 -0
- package/joi/src/__tests__/__snapshots__/joi.ts.snap +293 -0
- package/joi/src/__tests__/joi.ts +98 -0
- package/joi/src/index.ts +2 -0
- package/joi/src/joi.ts +81 -0
- package/joi/src/types.ts +16 -0
- package/nope/dist/index.d.ts +2 -0
- package/nope/dist/nope.d.ts +2 -0
- package/nope/dist/nope.js +2 -0
- package/nope/dist/nope.js.map +1 -0
- package/nope/dist/nope.mjs +2 -0
- package/nope/dist/nope.modern.js +2 -0
- package/nope/dist/nope.modern.js.map +1 -0
- package/nope/dist/nope.module.js +2 -0
- package/nope/dist/nope.module.js.map +1 -0
- package/nope/dist/nope.umd.js +2 -0
- package/nope/dist/nope.umd.js.map +1 -0
- package/nope/dist/types.d.ts +6 -0
- package/nope/package.json +18 -0
- package/nope/src/__tests__/Form-native-validation.tsx +89 -0
- package/nope/src/__tests__/Form.tsx +57 -0
- package/nope/src/__tests__/__fixtures__/data.ts +70 -0
- package/nope/src/__tests__/__snapshots__/nope.ts.snap +43 -0
- package/nope/src/__tests__/nope.ts +28 -0
- package/nope/src/index.ts +2 -0
- package/nope/src/nope.ts +46 -0
- package/nope/src/types.ts +22 -0
- package/package.json +216 -0
- package/superstruct/dist/index.d.ts +2 -0
- package/superstruct/dist/superstruct.d.ts +2 -0
- package/superstruct/dist/superstruct.js +2 -0
- package/superstruct/dist/superstruct.js.map +1 -0
- package/superstruct/dist/superstruct.mjs +2 -0
- package/superstruct/dist/superstruct.modern.js +2 -0
- package/superstruct/dist/superstruct.modern.js.map +1 -0
- package/superstruct/dist/superstruct.module.js +2 -0
- package/superstruct/dist/superstruct.module.js.map +1 -0
- package/superstruct/dist/superstruct.umd.js +2 -0
- package/superstruct/dist/superstruct.umd.js.map +1 -0
- package/superstruct/dist/types.d.ts +5 -0
- package/superstruct/package.json +18 -0
- package/superstruct/src/__tests__/Form-native-validation.tsx +86 -0
- package/superstruct/src/__tests__/Form.tsx +62 -0
- package/superstruct/src/__tests__/__fixtures__/data.ts +75 -0
- package/superstruct/src/__tests__/__snapshots__/superstruct.ts.snap +64 -0
- package/superstruct/src/__tests__/superstruct.ts +24 -0
- package/superstruct/src/index.ts +2 -0
- package/superstruct/src/superstruct.ts +37 -0
- package/superstruct/src/types.ts +17 -0
- package/typanion/dist/index.d.ts +2 -0
- package/typanion/dist/typanion.d.ts +2 -0
- package/typanion/dist/typanion.js +2 -0
- package/typanion/dist/typanion.js.map +1 -0
- package/typanion/dist/typanion.mjs +2 -0
- package/typanion/dist/typanion.modern.js +2 -0
- package/typanion/dist/typanion.modern.js.map +1 -0
- package/typanion/dist/typanion.module.js +2 -0
- package/typanion/dist/typanion.module.js.map +1 -0
- package/typanion/dist/typanion.umd.js +2 -0
- package/typanion/dist/typanion.umd.js.map +1 -0
- package/typanion/dist/types.d.ts +6 -0
- package/typanion/package.json +18 -0
- package/typanion/src/__tests__/Form-native-validation.tsx +89 -0
- package/typanion/src/__tests__/Form.tsx +61 -0
- package/typanion/src/__tests__/__fixtures__/data.ts +77 -0
- package/typanion/src/__tests__/__snapshots__/typanion.ts.snap +67 -0
- package/typanion/src/__tests__/typanion.ts +31 -0
- package/typanion/src/index.ts +2 -0
- package/typanion/src/typanion.ts +44 -0
- package/typanion/src/types.ts +28 -0
- package/vest/dist/index.d.ts +1 -0
- package/vest/dist/vest.d.ts +2 -0
- package/vest/dist/vest.js +2 -0
- package/vest/dist/vest.js.map +1 -0
- package/vest/dist/vest.mjs +2 -0
- package/vest/dist/vest.modern.js +2 -0
- package/vest/dist/vest.modern.js.map +1 -0
- package/vest/dist/vest.module.js +2 -0
- package/vest/dist/vest.module.js.map +1 -0
- package/vest/dist/vest.umd.js +2 -0
- package/vest/dist/vest.umd.js.map +1 -0
- package/vest/package.json +18 -0
- package/vest/src/__tests__/Form-native-validation.tsx +93 -0
- package/vest/src/__tests__/Form.tsx +64 -0
- package/vest/src/__tests__/__fixtures__/data.ts +67 -0
- package/vest/src/__tests__/__snapshots__/vest.ts.snap +141 -0
- package/vest/src/__tests__/vest.ts +71 -0
- package/vest/src/index.ts +1 -0
- package/vest/src/types.ts +20 -0
- package/vest/src/vest.ts +51 -0
- package/yup/dist/index.d.ts +2 -0
- package/yup/dist/types.d.ts +8 -0
- package/yup/dist/yup.d.ts +2 -0
- package/yup/dist/yup.js +2 -0
- package/yup/dist/yup.js.map +1 -0
- package/yup/dist/yup.mjs +2 -0
- package/yup/dist/yup.modern.js +2 -0
- package/yup/dist/yup.modern.js.map +1 -0
- package/yup/dist/yup.module.js +2 -0
- package/yup/dist/yup.module.js.map +1 -0
- package/yup/dist/yup.umd.js +2 -0
- package/yup/dist/yup.umd.js.map +1 -0
- package/yup/package.json +17 -0
- package/yup/src/__tests__/Form-native-validation.tsx +85 -0
- package/yup/src/__tests__/Form.tsx +54 -0
- package/yup/src/__tests__/__fixtures__/data.ts +72 -0
- package/yup/src/__tests__/__snapshots__/yup.ts.snap +231 -0
- package/yup/src/__tests__/yup.ts +174 -0
- package/yup/src/index.ts +2 -0
- package/yup/src/types.ts +21 -0
- package/yup/src/yup.ts +75 -0
- package/zod/dist/index.d.ts +2 -0
- package/zod/dist/types.d.ts +5 -0
- package/zod/dist/zod.d.ts +2 -0
- package/zod/dist/zod.js +2 -0
- package/zod/dist/zod.js.map +1 -0
- package/zod/dist/zod.mjs +2 -0
- package/zod/dist/zod.modern.js +2 -0
- package/zod/dist/zod.modern.js.map +1 -0
- package/zod/dist/zod.module.js +2 -0
- package/zod/dist/zod.module.js.map +1 -0
- package/zod/dist/zod.umd.js +2 -0
- package/zod/dist/zod.umd.js.map +1 -0
- package/zod/package.json +17 -0
- package/zod/src/__tests__/Form-native-validation.tsx +85 -0
- package/zod/src/__tests__/Form.tsx +54 -0
- package/zod/src/__tests__/__fixtures__/data.ts +81 -0
- package/zod/src/__tests__/__snapshots__/zod.ts.snap +377 -0
- package/zod/src/__tests__/zod.ts +77 -0
- package/zod/src/index.ts +2 -0
- package/zod/src/types.ts +16 -0
- package/zod/src/zod.ts +85 -0
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
+
|
|
3
|
+
exports[`zodResolver should return a single error from zodResolver when validation fails 1`] = `
|
|
4
|
+
Object {
|
|
5
|
+
"errors": Object {
|
|
6
|
+
"accessToken": Object {
|
|
7
|
+
"message": "Required",
|
|
8
|
+
"ref": undefined,
|
|
9
|
+
"type": "invalid_type",
|
|
10
|
+
},
|
|
11
|
+
"birthYear": Object {
|
|
12
|
+
"message": "Expected number, received string",
|
|
13
|
+
"ref": undefined,
|
|
14
|
+
"type": "invalid_type",
|
|
15
|
+
},
|
|
16
|
+
"email": Object {
|
|
17
|
+
"message": "Invalid email",
|
|
18
|
+
"ref": Object {
|
|
19
|
+
"name": "email",
|
|
20
|
+
},
|
|
21
|
+
"type": "invalid_string",
|
|
22
|
+
},
|
|
23
|
+
"enabled": Object {
|
|
24
|
+
"message": "Required",
|
|
25
|
+
"ref": undefined,
|
|
26
|
+
"type": "invalid_type",
|
|
27
|
+
},
|
|
28
|
+
"like": Array [
|
|
29
|
+
Object {
|
|
30
|
+
"id": Object {
|
|
31
|
+
"message": "Expected number, received string",
|
|
32
|
+
"ref": undefined,
|
|
33
|
+
"type": "invalid_type",
|
|
34
|
+
},
|
|
35
|
+
"name": Object {
|
|
36
|
+
"message": "Required",
|
|
37
|
+
"ref": undefined,
|
|
38
|
+
"type": "invalid_type",
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
"password": Object {
|
|
43
|
+
"message": "One uppercase character",
|
|
44
|
+
"ref": Object {
|
|
45
|
+
"name": "password",
|
|
46
|
+
},
|
|
47
|
+
"type": "invalid_string",
|
|
48
|
+
},
|
|
49
|
+
"repeatPassword": Object {
|
|
50
|
+
"message": "Required",
|
|
51
|
+
"ref": undefined,
|
|
52
|
+
"type": "invalid_type",
|
|
53
|
+
},
|
|
54
|
+
"tags": Object {
|
|
55
|
+
"message": "Required",
|
|
56
|
+
"ref": undefined,
|
|
57
|
+
"type": "invalid_type",
|
|
58
|
+
},
|
|
59
|
+
"url": Object {
|
|
60
|
+
"message": "Custom error url",
|
|
61
|
+
"ref": undefined,
|
|
62
|
+
"type": "invalid_string",
|
|
63
|
+
},
|
|
64
|
+
"username": Object {
|
|
65
|
+
"message": "Required",
|
|
66
|
+
"ref": Object {
|
|
67
|
+
"name": "username",
|
|
68
|
+
},
|
|
69
|
+
"type": "invalid_type",
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
"values": Object {},
|
|
73
|
+
}
|
|
74
|
+
`;
|
|
75
|
+
|
|
76
|
+
exports[`zodResolver should return a single error from zodResolver with \`mode: sync\` when validation fails 1`] = `
|
|
77
|
+
Object {
|
|
78
|
+
"errors": Object {
|
|
79
|
+
"accessToken": Object {
|
|
80
|
+
"message": "Required",
|
|
81
|
+
"ref": undefined,
|
|
82
|
+
"type": "invalid_type",
|
|
83
|
+
},
|
|
84
|
+
"birthYear": Object {
|
|
85
|
+
"message": "Expected number, received string",
|
|
86
|
+
"ref": undefined,
|
|
87
|
+
"type": "invalid_type",
|
|
88
|
+
},
|
|
89
|
+
"email": Object {
|
|
90
|
+
"message": "Invalid email",
|
|
91
|
+
"ref": Object {
|
|
92
|
+
"name": "email",
|
|
93
|
+
},
|
|
94
|
+
"type": "invalid_string",
|
|
95
|
+
},
|
|
96
|
+
"enabled": Object {
|
|
97
|
+
"message": "Required",
|
|
98
|
+
"ref": undefined,
|
|
99
|
+
"type": "invalid_type",
|
|
100
|
+
},
|
|
101
|
+
"like": Array [
|
|
102
|
+
Object {
|
|
103
|
+
"id": Object {
|
|
104
|
+
"message": "Expected number, received string",
|
|
105
|
+
"ref": undefined,
|
|
106
|
+
"type": "invalid_type",
|
|
107
|
+
},
|
|
108
|
+
"name": Object {
|
|
109
|
+
"message": "Required",
|
|
110
|
+
"ref": undefined,
|
|
111
|
+
"type": "invalid_type",
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
],
|
|
115
|
+
"password": Object {
|
|
116
|
+
"message": "One uppercase character",
|
|
117
|
+
"ref": Object {
|
|
118
|
+
"name": "password",
|
|
119
|
+
},
|
|
120
|
+
"type": "invalid_string",
|
|
121
|
+
},
|
|
122
|
+
"repeatPassword": Object {
|
|
123
|
+
"message": "Required",
|
|
124
|
+
"ref": undefined,
|
|
125
|
+
"type": "invalid_type",
|
|
126
|
+
},
|
|
127
|
+
"tags": Object {
|
|
128
|
+
"message": "Required",
|
|
129
|
+
"ref": undefined,
|
|
130
|
+
"type": "invalid_type",
|
|
131
|
+
},
|
|
132
|
+
"url": Object {
|
|
133
|
+
"message": "Custom error url",
|
|
134
|
+
"ref": undefined,
|
|
135
|
+
"type": "invalid_string",
|
|
136
|
+
},
|
|
137
|
+
"username": Object {
|
|
138
|
+
"message": "Required",
|
|
139
|
+
"ref": Object {
|
|
140
|
+
"name": "username",
|
|
141
|
+
},
|
|
142
|
+
"type": "invalid_type",
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
"values": Object {},
|
|
146
|
+
}
|
|
147
|
+
`;
|
|
148
|
+
|
|
149
|
+
exports[`zodResolver should return all the errors from zodResolver when validation fails with \`validateAllFieldCriteria\` set to true 1`] = `
|
|
150
|
+
Object {
|
|
151
|
+
"errors": Object {
|
|
152
|
+
"accessToken": Object {
|
|
153
|
+
"message": "Required",
|
|
154
|
+
"ref": undefined,
|
|
155
|
+
"type": "invalid_type",
|
|
156
|
+
"types": Object {
|
|
157
|
+
"invalid_type": Array [
|
|
158
|
+
"Required",
|
|
159
|
+
"Required",
|
|
160
|
+
],
|
|
161
|
+
"invalid_union": "Invalid input",
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
"birthYear": Object {
|
|
165
|
+
"message": "Expected number, received string",
|
|
166
|
+
"ref": undefined,
|
|
167
|
+
"type": "invalid_type",
|
|
168
|
+
"types": Object {
|
|
169
|
+
"invalid_type": "Expected number, received string",
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
"email": Object {
|
|
173
|
+
"message": "Invalid email",
|
|
174
|
+
"ref": Object {
|
|
175
|
+
"name": "email",
|
|
176
|
+
},
|
|
177
|
+
"type": "invalid_string",
|
|
178
|
+
"types": Object {
|
|
179
|
+
"invalid_string": "Invalid email",
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
"enabled": Object {
|
|
183
|
+
"message": "Required",
|
|
184
|
+
"ref": undefined,
|
|
185
|
+
"type": "invalid_type",
|
|
186
|
+
"types": Object {
|
|
187
|
+
"invalid_type": "Required",
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
"like": Array [
|
|
191
|
+
Object {
|
|
192
|
+
"id": Object {
|
|
193
|
+
"message": "Expected number, received string",
|
|
194
|
+
"ref": undefined,
|
|
195
|
+
"type": "invalid_type",
|
|
196
|
+
"types": Object {
|
|
197
|
+
"invalid_type": "Expected number, received string",
|
|
198
|
+
},
|
|
199
|
+
},
|
|
200
|
+
"name": Object {
|
|
201
|
+
"message": "Required",
|
|
202
|
+
"ref": undefined,
|
|
203
|
+
"type": "invalid_type",
|
|
204
|
+
"types": Object {
|
|
205
|
+
"invalid_type": "Required",
|
|
206
|
+
},
|
|
207
|
+
},
|
|
208
|
+
},
|
|
209
|
+
],
|
|
210
|
+
"password": Object {
|
|
211
|
+
"message": "One uppercase character",
|
|
212
|
+
"ref": Object {
|
|
213
|
+
"name": "password",
|
|
214
|
+
},
|
|
215
|
+
"type": "invalid_string",
|
|
216
|
+
"types": Object {
|
|
217
|
+
"invalid_string": Array [
|
|
218
|
+
"One uppercase character",
|
|
219
|
+
"One lowercase character",
|
|
220
|
+
"One number",
|
|
221
|
+
],
|
|
222
|
+
"too_small": "Must be at least 8 characters in length",
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
"repeatPassword": Object {
|
|
226
|
+
"message": "Required",
|
|
227
|
+
"ref": undefined,
|
|
228
|
+
"type": "invalid_type",
|
|
229
|
+
"types": Object {
|
|
230
|
+
"invalid_type": "Required",
|
|
231
|
+
},
|
|
232
|
+
},
|
|
233
|
+
"tags": Object {
|
|
234
|
+
"message": "Required",
|
|
235
|
+
"ref": undefined,
|
|
236
|
+
"type": "invalid_type",
|
|
237
|
+
"types": Object {
|
|
238
|
+
"invalid_type": "Required",
|
|
239
|
+
},
|
|
240
|
+
},
|
|
241
|
+
"url": Object {
|
|
242
|
+
"message": "Custom error url",
|
|
243
|
+
"ref": undefined,
|
|
244
|
+
"type": "invalid_string",
|
|
245
|
+
"types": Object {
|
|
246
|
+
"invalid_string": "Custom error url",
|
|
247
|
+
},
|
|
248
|
+
},
|
|
249
|
+
"username": Object {
|
|
250
|
+
"message": "Required",
|
|
251
|
+
"ref": Object {
|
|
252
|
+
"name": "username",
|
|
253
|
+
},
|
|
254
|
+
"type": "invalid_type",
|
|
255
|
+
"types": Object {
|
|
256
|
+
"invalid_type": "Required",
|
|
257
|
+
},
|
|
258
|
+
},
|
|
259
|
+
},
|
|
260
|
+
"values": Object {},
|
|
261
|
+
}
|
|
262
|
+
`;
|
|
263
|
+
|
|
264
|
+
exports[`zodResolver should return all the errors from zodResolver when validation fails with \`validateAllFieldCriteria\` set to true and \`mode: sync\` 1`] = `
|
|
265
|
+
Object {
|
|
266
|
+
"errors": Object {
|
|
267
|
+
"accessToken": Object {
|
|
268
|
+
"message": "Required",
|
|
269
|
+
"ref": undefined,
|
|
270
|
+
"type": "invalid_type",
|
|
271
|
+
"types": Object {
|
|
272
|
+
"invalid_type": Array [
|
|
273
|
+
"Required",
|
|
274
|
+
"Required",
|
|
275
|
+
],
|
|
276
|
+
"invalid_union": "Invalid input",
|
|
277
|
+
},
|
|
278
|
+
},
|
|
279
|
+
"birthYear": Object {
|
|
280
|
+
"message": "Expected number, received string",
|
|
281
|
+
"ref": undefined,
|
|
282
|
+
"type": "invalid_type",
|
|
283
|
+
"types": Object {
|
|
284
|
+
"invalid_type": "Expected number, received string",
|
|
285
|
+
},
|
|
286
|
+
},
|
|
287
|
+
"email": Object {
|
|
288
|
+
"message": "Invalid email",
|
|
289
|
+
"ref": Object {
|
|
290
|
+
"name": "email",
|
|
291
|
+
},
|
|
292
|
+
"type": "invalid_string",
|
|
293
|
+
"types": Object {
|
|
294
|
+
"invalid_string": "Invalid email",
|
|
295
|
+
},
|
|
296
|
+
},
|
|
297
|
+
"enabled": Object {
|
|
298
|
+
"message": "Required",
|
|
299
|
+
"ref": undefined,
|
|
300
|
+
"type": "invalid_type",
|
|
301
|
+
"types": Object {
|
|
302
|
+
"invalid_type": "Required",
|
|
303
|
+
},
|
|
304
|
+
},
|
|
305
|
+
"like": Array [
|
|
306
|
+
Object {
|
|
307
|
+
"id": Object {
|
|
308
|
+
"message": "Expected number, received string",
|
|
309
|
+
"ref": undefined,
|
|
310
|
+
"type": "invalid_type",
|
|
311
|
+
"types": Object {
|
|
312
|
+
"invalid_type": "Expected number, received string",
|
|
313
|
+
},
|
|
314
|
+
},
|
|
315
|
+
"name": Object {
|
|
316
|
+
"message": "Required",
|
|
317
|
+
"ref": undefined,
|
|
318
|
+
"type": "invalid_type",
|
|
319
|
+
"types": Object {
|
|
320
|
+
"invalid_type": "Required",
|
|
321
|
+
},
|
|
322
|
+
},
|
|
323
|
+
},
|
|
324
|
+
],
|
|
325
|
+
"password": Object {
|
|
326
|
+
"message": "One uppercase character",
|
|
327
|
+
"ref": Object {
|
|
328
|
+
"name": "password",
|
|
329
|
+
},
|
|
330
|
+
"type": "invalid_string",
|
|
331
|
+
"types": Object {
|
|
332
|
+
"invalid_string": Array [
|
|
333
|
+
"One uppercase character",
|
|
334
|
+
"One lowercase character",
|
|
335
|
+
"One number",
|
|
336
|
+
],
|
|
337
|
+
"too_small": "Must be at least 8 characters in length",
|
|
338
|
+
},
|
|
339
|
+
},
|
|
340
|
+
"repeatPassword": Object {
|
|
341
|
+
"message": "Required",
|
|
342
|
+
"ref": undefined,
|
|
343
|
+
"type": "invalid_type",
|
|
344
|
+
"types": Object {
|
|
345
|
+
"invalid_type": "Required",
|
|
346
|
+
},
|
|
347
|
+
},
|
|
348
|
+
"tags": Object {
|
|
349
|
+
"message": "Required",
|
|
350
|
+
"ref": undefined,
|
|
351
|
+
"type": "invalid_type",
|
|
352
|
+
"types": Object {
|
|
353
|
+
"invalid_type": "Required",
|
|
354
|
+
},
|
|
355
|
+
},
|
|
356
|
+
"url": Object {
|
|
357
|
+
"message": "Custom error url",
|
|
358
|
+
"ref": undefined,
|
|
359
|
+
"type": "invalid_string",
|
|
360
|
+
"types": Object {
|
|
361
|
+
"invalid_string": "Custom error url",
|
|
362
|
+
},
|
|
363
|
+
},
|
|
364
|
+
"username": Object {
|
|
365
|
+
"message": "Required",
|
|
366
|
+
"ref": Object {
|
|
367
|
+
"name": "username",
|
|
368
|
+
},
|
|
369
|
+
"type": "invalid_type",
|
|
370
|
+
"types": Object {
|
|
371
|
+
"invalid_type": "Required",
|
|
372
|
+
},
|
|
373
|
+
},
|
|
374
|
+
},
|
|
375
|
+
"values": Object {},
|
|
376
|
+
}
|
|
377
|
+
`;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { zodResolver } from '..';
|
|
2
|
+
import { schema, validData, invalidData, fields } from './__fixtures__/data';
|
|
3
|
+
|
|
4
|
+
const shouldUseNativeValidation = false;
|
|
5
|
+
|
|
6
|
+
describe('zodResolver', () => {
|
|
7
|
+
it('should return values from zodResolver when validation pass', async () => {
|
|
8
|
+
const parseAsyncSpy = jest.spyOn(schema, 'parseAsync');
|
|
9
|
+
|
|
10
|
+
const result = await zodResolver(schema)(validData, undefined, {
|
|
11
|
+
fields,
|
|
12
|
+
shouldUseNativeValidation,
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
expect(parseAsyncSpy).toHaveBeenCalledTimes(1);
|
|
16
|
+
expect(result).toEqual({ errors: {}, values: validData });
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('should return values from zodResolver with `mode: sync` when validation pass', async () => {
|
|
20
|
+
const parseSpy = jest.spyOn(schema, 'parse');
|
|
21
|
+
const parseAsyncSpy = jest.spyOn(schema, 'parseAsync');
|
|
22
|
+
|
|
23
|
+
const result = await zodResolver(schema, undefined, {
|
|
24
|
+
mode: 'sync',
|
|
25
|
+
})(validData, undefined, { fields, shouldUseNativeValidation });
|
|
26
|
+
|
|
27
|
+
expect(parseSpy).toHaveBeenCalledTimes(1);
|
|
28
|
+
expect(parseAsyncSpy).not.toHaveBeenCalled();
|
|
29
|
+
expect(result).toEqual({ errors: {}, values: validData });
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should return a single error from zodResolver when validation fails', async () => {
|
|
33
|
+
const result = await zodResolver(schema)(invalidData, undefined, {
|
|
34
|
+
fields,
|
|
35
|
+
shouldUseNativeValidation,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
expect(result).toMatchSnapshot();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('should return a single error from zodResolver with `mode: sync` when validation fails', async () => {
|
|
42
|
+
const parseSpy = jest.spyOn(schema, 'parse');
|
|
43
|
+
const parseAsyncSpy = jest.spyOn(schema, 'parseAsync');
|
|
44
|
+
|
|
45
|
+
const result = await zodResolver(schema, undefined, {
|
|
46
|
+
mode: 'sync',
|
|
47
|
+
})(invalidData, undefined, { fields, shouldUseNativeValidation });
|
|
48
|
+
|
|
49
|
+
expect(parseSpy).toHaveBeenCalledTimes(1);
|
|
50
|
+
expect(parseAsyncSpy).not.toHaveBeenCalled();
|
|
51
|
+
expect(result).toMatchSnapshot();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('should return all the errors from zodResolver when validation fails with `validateAllFieldCriteria` set to true', async () => {
|
|
55
|
+
const result = await zodResolver(schema)(invalidData, undefined, {
|
|
56
|
+
fields,
|
|
57
|
+
criteriaMode: 'all',
|
|
58
|
+
shouldUseNativeValidation,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
expect(result).toMatchSnapshot();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it.only('should return all the errors from zodResolver when validation fails with `validateAllFieldCriteria` set to true and `mode: sync`', async () => {
|
|
65
|
+
const result = await zodResolver(schema, undefined, { mode: 'sync' })(
|
|
66
|
+
invalidData,
|
|
67
|
+
undefined,
|
|
68
|
+
{
|
|
69
|
+
fields,
|
|
70
|
+
criteriaMode: 'all',
|
|
71
|
+
shouldUseNativeValidation,
|
|
72
|
+
},
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
expect(result).toMatchSnapshot();
|
|
76
|
+
});
|
|
77
|
+
});
|
package/zod/src/index.ts
ADDED
package/zod/src/types.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/ban-types */
|
|
2
|
+
import { ResolverResult, ResolverOptions, FieldValues } from 'react-hook-form';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
|
|
5
|
+
export type Resolver = <
|
|
6
|
+
TFieldValues extends FieldValues,
|
|
7
|
+
TContext extends object = object,
|
|
8
|
+
>(
|
|
9
|
+
schema: z.Schema<TFieldValues, any>,
|
|
10
|
+
schemaOptions?: Partial<z.ParseParamsNoData>,
|
|
11
|
+
factoryOptions?: { mode?: 'async' | 'sync' },
|
|
12
|
+
) => (
|
|
13
|
+
values: unknown,
|
|
14
|
+
context: TContext | undefined,
|
|
15
|
+
options: ResolverOptions<TFieldValues>,
|
|
16
|
+
) => Promise<ResolverResult<TFieldValues>>;
|
package/zod/src/zod.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { appendErrors, FieldError, FieldErrors } from 'react-hook-form';
|
|
2
|
+
import { z, ZodError } from 'zod';
|
|
3
|
+
import { toNestError, validateFieldsNatively } from '@hookform/resolvers';
|
|
4
|
+
import type { Resolver } from './types';
|
|
5
|
+
|
|
6
|
+
const parseErrorSchema = (
|
|
7
|
+
zodErrors: z.ZodIssue[],
|
|
8
|
+
validateAllFieldCriteria: boolean,
|
|
9
|
+
) => {
|
|
10
|
+
const errors: Record<string, FieldError> = {};
|
|
11
|
+
for (; zodErrors.length; ) {
|
|
12
|
+
const error = zodErrors[0];
|
|
13
|
+
const { code, message, path } = error;
|
|
14
|
+
const _path = path.join('.');
|
|
15
|
+
|
|
16
|
+
if (!errors[_path]) {
|
|
17
|
+
if ('unionErrors' in error) {
|
|
18
|
+
const unionError = error.unionErrors[0].errors[0];
|
|
19
|
+
|
|
20
|
+
errors[_path] = {
|
|
21
|
+
message: unionError.message,
|
|
22
|
+
type: unionError.code,
|
|
23
|
+
};
|
|
24
|
+
} else {
|
|
25
|
+
errors[_path] = { message, type: code };
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if ('unionErrors' in error) {
|
|
30
|
+
error.unionErrors.forEach((unionError) =>
|
|
31
|
+
unionError.errors.forEach((e) => zodErrors.push(e)),
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (validateAllFieldCriteria) {
|
|
36
|
+
const types = errors[_path].types;
|
|
37
|
+
const messages = types && types[error.code];
|
|
38
|
+
|
|
39
|
+
errors[_path] = appendErrors(
|
|
40
|
+
_path,
|
|
41
|
+
validateAllFieldCriteria,
|
|
42
|
+
errors,
|
|
43
|
+
code,
|
|
44
|
+
messages
|
|
45
|
+
? ([] as string[]).concat(messages as string[], error.message)
|
|
46
|
+
: error.message,
|
|
47
|
+
) as FieldError;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
zodErrors.shift();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return errors;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export const zodResolver: Resolver =
|
|
57
|
+
(schema, schemaOptions, resolverOptions = {}) =>
|
|
58
|
+
async (values, _, options) => {
|
|
59
|
+
try {
|
|
60
|
+
const data = await schema[
|
|
61
|
+
resolverOptions.mode === 'sync' ? 'parse' : 'parseAsync'
|
|
62
|
+
](values, schemaOptions);
|
|
63
|
+
|
|
64
|
+
options.shouldUseNativeValidation && validateFieldsNatively({}, options);
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
errors: {} as FieldErrors<any>,
|
|
68
|
+
values: data,
|
|
69
|
+
};
|
|
70
|
+
} catch (error) {
|
|
71
|
+
return {
|
|
72
|
+
values: {},
|
|
73
|
+
errors: (error as ZodError).isEmpty
|
|
74
|
+
? ({} as FieldErrors<any>)
|
|
75
|
+
: (toNestError(
|
|
76
|
+
parseErrorSchema(
|
|
77
|
+
(error as ZodError).errors,
|
|
78
|
+
!options.shouldUseNativeValidation &&
|
|
79
|
+
options.criteriaMode === 'all',
|
|
80
|
+
),
|
|
81
|
+
options,
|
|
82
|
+
) as FieldErrors<any>),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
};
|