@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.
Files changed (227) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +420 -0
  3. package/class-validator/dist/class-validator.d.ts +2 -0
  4. package/class-validator/dist/class-validator.js +2 -0
  5. package/class-validator/dist/class-validator.js.map +1 -0
  6. package/class-validator/dist/class-validator.mjs +2 -0
  7. package/class-validator/dist/class-validator.modern.js +2 -0
  8. package/class-validator/dist/class-validator.modern.js.map +1 -0
  9. package/class-validator/dist/class-validator.module.js +2 -0
  10. package/class-validator/dist/class-validator.module.js.map +1 -0
  11. package/class-validator/dist/class-validator.umd.js +2 -0
  12. package/class-validator/dist/class-validator.umd.js.map +1 -0
  13. package/class-validator/dist/index.d.ts +2 -0
  14. package/class-validator/dist/types.d.ts +8 -0
  15. package/class-validator/package.json +19 -0
  16. package/class-validator/src/__tests__/Form-native-validation.tsx +83 -0
  17. package/class-validator/src/__tests__/Form.tsx +55 -0
  18. package/class-validator/src/__tests__/__fixtures__/data.ts +88 -0
  19. package/class-validator/src/__tests__/__snapshots__/class-validator.ts.snap +207 -0
  20. package/class-validator/src/__tests__/class-validator.ts +87 -0
  21. package/class-validator/src/class-validator.ts +62 -0
  22. package/class-validator/src/index.ts +2 -0
  23. package/class-validator/src/types.ts +22 -0
  24. package/computed-types/dist/computed-types.d.ts +2 -0
  25. package/computed-types/dist/computed-types.js +2 -0
  26. package/computed-types/dist/computed-types.js.map +1 -0
  27. package/computed-types/dist/computed-types.mjs +2 -0
  28. package/computed-types/dist/computed-types.modern.js +2 -0
  29. package/computed-types/dist/computed-types.modern.js.map +1 -0
  30. package/computed-types/dist/computed-types.module.js +2 -0
  31. package/computed-types/dist/computed-types.module.js.map +1 -0
  32. package/computed-types/dist/computed-types.umd.js +2 -0
  33. package/computed-types/dist/computed-types.umd.js.map +1 -0
  34. package/computed-types/dist/index.d.ts +2 -0
  35. package/computed-types/dist/types.d.ts +2 -0
  36. package/computed-types/package.json +17 -0
  37. package/computed-types/src/__tests__/Form-native-validation.tsx +85 -0
  38. package/computed-types/src/__tests__/Form.tsx +54 -0
  39. package/computed-types/src/__tests__/__fixtures__/data.ts +72 -0
  40. package/computed-types/src/__tests__/__snapshots__/computed-types.ts.snap +55 -0
  41. package/computed-types/src/__tests__/computed-types.ts +24 -0
  42. package/computed-types/src/computed-types.ts +43 -0
  43. package/computed-types/src/index.ts +2 -0
  44. package/computed-types/src/types.ts +14 -0
  45. package/dist/index.d.ts +2 -0
  46. package/dist/resolvers.js +2 -0
  47. package/dist/resolvers.js.map +1 -0
  48. package/dist/resolvers.mjs +2 -0
  49. package/dist/resolvers.mjs.map +1 -0
  50. package/dist/resolvers.module.js +2 -0
  51. package/dist/resolvers.module.js.map +1 -0
  52. package/dist/resolvers.umd.js +2 -0
  53. package/dist/resolvers.umd.js.map +1 -0
  54. package/dist/toNestError.d.ts +2 -0
  55. package/dist/validateFieldsNatively.d.ts +2 -0
  56. package/io-ts/dist/arrayToPath.d.ts +3 -0
  57. package/io-ts/dist/errorsToRecord.d.ts +5 -0
  58. package/io-ts/dist/index.d.ts +2 -0
  59. package/io-ts/dist/io-ts.d.ts +2 -0
  60. package/io-ts/dist/io-ts.js +2 -0
  61. package/io-ts/dist/io-ts.js.map +1 -0
  62. package/io-ts/dist/io-ts.mjs +2 -0
  63. package/io-ts/dist/io-ts.modern.js +2 -0
  64. package/io-ts/dist/io-ts.modern.js.map +1 -0
  65. package/io-ts/dist/io-ts.module.js +2 -0
  66. package/io-ts/dist/io-ts.module.js.map +1 -0
  67. package/io-ts/dist/io-ts.umd.js +2 -0
  68. package/io-ts/dist/io-ts.umd.js.map +1 -0
  69. package/io-ts/dist/types.d.ts +7 -0
  70. package/io-ts/package.json +19 -0
  71. package/io-ts/src/__tests__/Form-native-validation.tsx +89 -0
  72. package/io-ts/src/__tests__/Form.tsx +65 -0
  73. package/io-ts/src/__tests__/__fixtures__/data.ts +129 -0
  74. package/io-ts/src/__tests__/__snapshots__/io-ts.ts.snap +89 -0
  75. package/io-ts/src/__tests__/io-ts.ts +37 -0
  76. package/io-ts/src/arrayToPath.ts +18 -0
  77. package/io-ts/src/errorsToRecord.ts +131 -0
  78. package/io-ts/src/index.ts +2 -0
  79. package/io-ts/src/io-ts.ts +33 -0
  80. package/io-ts/src/types.ts +23 -0
  81. package/joi/dist/index.d.ts +2 -0
  82. package/joi/dist/joi.d.ts +2 -0
  83. package/joi/dist/joi.js +2 -0
  84. package/joi/dist/joi.js.map +1 -0
  85. package/joi/dist/joi.mjs +2 -0
  86. package/joi/dist/joi.modern.js +2 -0
  87. package/joi/dist/joi.modern.js.map +1 -0
  88. package/joi/dist/joi.module.js +2 -0
  89. package/joi/dist/joi.module.js.map +1 -0
  90. package/joi/dist/joi.umd.js +2 -0
  91. package/joi/dist/joi.umd.js.map +1 -0
  92. package/joi/dist/types.d.ts +5 -0
  93. package/joi/package.json +17 -0
  94. package/joi/src/__tests__/Form-native-validation.tsx +89 -0
  95. package/joi/src/__tests__/Form.tsx +61 -0
  96. package/joi/src/__tests__/__fixtures__/data.ts +85 -0
  97. package/joi/src/__tests__/__snapshots__/joi.ts.snap +293 -0
  98. package/joi/src/__tests__/joi.ts +98 -0
  99. package/joi/src/index.ts +2 -0
  100. package/joi/src/joi.ts +81 -0
  101. package/joi/src/types.ts +16 -0
  102. package/nope/dist/index.d.ts +2 -0
  103. package/nope/dist/nope.d.ts +2 -0
  104. package/nope/dist/nope.js +2 -0
  105. package/nope/dist/nope.js.map +1 -0
  106. package/nope/dist/nope.mjs +2 -0
  107. package/nope/dist/nope.modern.js +2 -0
  108. package/nope/dist/nope.modern.js.map +1 -0
  109. package/nope/dist/nope.module.js +2 -0
  110. package/nope/dist/nope.module.js.map +1 -0
  111. package/nope/dist/nope.umd.js +2 -0
  112. package/nope/dist/nope.umd.js.map +1 -0
  113. package/nope/dist/types.d.ts +6 -0
  114. package/nope/package.json +18 -0
  115. package/nope/src/__tests__/Form-native-validation.tsx +89 -0
  116. package/nope/src/__tests__/Form.tsx +57 -0
  117. package/nope/src/__tests__/__fixtures__/data.ts +70 -0
  118. package/nope/src/__tests__/__snapshots__/nope.ts.snap +43 -0
  119. package/nope/src/__tests__/nope.ts +28 -0
  120. package/nope/src/index.ts +2 -0
  121. package/nope/src/nope.ts +46 -0
  122. package/nope/src/types.ts +22 -0
  123. package/package.json +216 -0
  124. package/superstruct/dist/index.d.ts +2 -0
  125. package/superstruct/dist/superstruct.d.ts +2 -0
  126. package/superstruct/dist/superstruct.js +2 -0
  127. package/superstruct/dist/superstruct.js.map +1 -0
  128. package/superstruct/dist/superstruct.mjs +2 -0
  129. package/superstruct/dist/superstruct.modern.js +2 -0
  130. package/superstruct/dist/superstruct.modern.js.map +1 -0
  131. package/superstruct/dist/superstruct.module.js +2 -0
  132. package/superstruct/dist/superstruct.module.js.map +1 -0
  133. package/superstruct/dist/superstruct.umd.js +2 -0
  134. package/superstruct/dist/superstruct.umd.js.map +1 -0
  135. package/superstruct/dist/types.d.ts +5 -0
  136. package/superstruct/package.json +18 -0
  137. package/superstruct/src/__tests__/Form-native-validation.tsx +86 -0
  138. package/superstruct/src/__tests__/Form.tsx +62 -0
  139. package/superstruct/src/__tests__/__fixtures__/data.ts +75 -0
  140. package/superstruct/src/__tests__/__snapshots__/superstruct.ts.snap +64 -0
  141. package/superstruct/src/__tests__/superstruct.ts +24 -0
  142. package/superstruct/src/index.ts +2 -0
  143. package/superstruct/src/superstruct.ts +37 -0
  144. package/superstruct/src/types.ts +17 -0
  145. package/typanion/dist/index.d.ts +2 -0
  146. package/typanion/dist/typanion.d.ts +2 -0
  147. package/typanion/dist/typanion.js +2 -0
  148. package/typanion/dist/typanion.js.map +1 -0
  149. package/typanion/dist/typanion.mjs +2 -0
  150. package/typanion/dist/typanion.modern.js +2 -0
  151. package/typanion/dist/typanion.modern.js.map +1 -0
  152. package/typanion/dist/typanion.module.js +2 -0
  153. package/typanion/dist/typanion.module.js.map +1 -0
  154. package/typanion/dist/typanion.umd.js +2 -0
  155. package/typanion/dist/typanion.umd.js.map +1 -0
  156. package/typanion/dist/types.d.ts +6 -0
  157. package/typanion/package.json +18 -0
  158. package/typanion/src/__tests__/Form-native-validation.tsx +89 -0
  159. package/typanion/src/__tests__/Form.tsx +61 -0
  160. package/typanion/src/__tests__/__fixtures__/data.ts +77 -0
  161. package/typanion/src/__tests__/__snapshots__/typanion.ts.snap +67 -0
  162. package/typanion/src/__tests__/typanion.ts +31 -0
  163. package/typanion/src/index.ts +2 -0
  164. package/typanion/src/typanion.ts +44 -0
  165. package/typanion/src/types.ts +28 -0
  166. package/vest/dist/index.d.ts +1 -0
  167. package/vest/dist/vest.d.ts +2 -0
  168. package/vest/dist/vest.js +2 -0
  169. package/vest/dist/vest.js.map +1 -0
  170. package/vest/dist/vest.mjs +2 -0
  171. package/vest/dist/vest.modern.js +2 -0
  172. package/vest/dist/vest.modern.js.map +1 -0
  173. package/vest/dist/vest.module.js +2 -0
  174. package/vest/dist/vest.module.js.map +1 -0
  175. package/vest/dist/vest.umd.js +2 -0
  176. package/vest/dist/vest.umd.js.map +1 -0
  177. package/vest/package.json +18 -0
  178. package/vest/src/__tests__/Form-native-validation.tsx +93 -0
  179. package/vest/src/__tests__/Form.tsx +64 -0
  180. package/vest/src/__tests__/__fixtures__/data.ts +67 -0
  181. package/vest/src/__tests__/__snapshots__/vest.ts.snap +141 -0
  182. package/vest/src/__tests__/vest.ts +71 -0
  183. package/vest/src/index.ts +1 -0
  184. package/vest/src/types.ts +20 -0
  185. package/vest/src/vest.ts +51 -0
  186. package/yup/dist/index.d.ts +2 -0
  187. package/yup/dist/types.d.ts +8 -0
  188. package/yup/dist/yup.d.ts +2 -0
  189. package/yup/dist/yup.js +2 -0
  190. package/yup/dist/yup.js.map +1 -0
  191. package/yup/dist/yup.mjs +2 -0
  192. package/yup/dist/yup.modern.js +2 -0
  193. package/yup/dist/yup.modern.js.map +1 -0
  194. package/yup/dist/yup.module.js +2 -0
  195. package/yup/dist/yup.module.js.map +1 -0
  196. package/yup/dist/yup.umd.js +2 -0
  197. package/yup/dist/yup.umd.js.map +1 -0
  198. package/yup/package.json +17 -0
  199. package/yup/src/__tests__/Form-native-validation.tsx +85 -0
  200. package/yup/src/__tests__/Form.tsx +54 -0
  201. package/yup/src/__tests__/__fixtures__/data.ts +72 -0
  202. package/yup/src/__tests__/__snapshots__/yup.ts.snap +231 -0
  203. package/yup/src/__tests__/yup.ts +174 -0
  204. package/yup/src/index.ts +2 -0
  205. package/yup/src/types.ts +21 -0
  206. package/yup/src/yup.ts +75 -0
  207. package/zod/dist/index.d.ts +2 -0
  208. package/zod/dist/types.d.ts +5 -0
  209. package/zod/dist/zod.d.ts +2 -0
  210. package/zod/dist/zod.js +2 -0
  211. package/zod/dist/zod.js.map +1 -0
  212. package/zod/dist/zod.mjs +2 -0
  213. package/zod/dist/zod.modern.js +2 -0
  214. package/zod/dist/zod.modern.js.map +1 -0
  215. package/zod/dist/zod.module.js +2 -0
  216. package/zod/dist/zod.module.js.map +1 -0
  217. package/zod/dist/zod.umd.js +2 -0
  218. package/zod/dist/zod.umd.js.map +1 -0
  219. package/zod/package.json +17 -0
  220. package/zod/src/__tests__/Form-native-validation.tsx +85 -0
  221. package/zod/src/__tests__/Form.tsx +54 -0
  222. package/zod/src/__tests__/__fixtures__/data.ts +81 -0
  223. package/zod/src/__tests__/__snapshots__/zod.ts.snap +377 -0
  224. package/zod/src/__tests__/zod.ts +77 -0
  225. package/zod/src/index.ts +2 -0
  226. package/zod/src/types.ts +16 -0
  227. 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
+ });
@@ -0,0 +1,2 @@
1
+ export * from './zod';
2
+ export * from './types';
@@ -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
+ };