@hookform/resolvers 4.0.0 → 4.1.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 (258) hide show
  1. package/README.md +35 -2
  2. package/ajv/dist/ajv.d.ts +20 -0
  3. package/ajv/dist/ajv.js.map +1 -1
  4. package/ajv/dist/ajv.modern.mjs.map +1 -1
  5. package/ajv/dist/ajv.module.js.map +1 -1
  6. package/ajv/dist/ajv.umd.js.map +1 -1
  7. package/ajv/src/ajv.ts +20 -0
  8. package/arktype/dist/arktype.d.ts +20 -2
  9. package/arktype/dist/arktype.js +1 -1
  10. package/arktype/dist/arktype.js.map +1 -1
  11. package/arktype/dist/arktype.mjs +1 -1
  12. package/arktype/dist/arktype.modern.mjs +1 -1
  13. package/arktype/dist/arktype.modern.mjs.map +1 -1
  14. package/arktype/dist/arktype.module.js +1 -1
  15. package/arktype/dist/arktype.module.js.map +1 -1
  16. package/arktype/dist/arktype.umd.js +1 -1
  17. package/arktype/dist/arktype.umd.js.map +1 -1
  18. package/arktype/dist/index.d.ts +0 -1
  19. package/arktype/src/__tests__/Form.tsx +32 -6
  20. package/arktype/src/__tests__/__fixtures__/data.ts +1 -1
  21. package/arktype/src/arktype.ts +29 -9
  22. package/arktype/src/index.ts +0 -1
  23. package/class-validator/dist/class-validator.d.ts +29 -2
  24. package/class-validator/dist/class-validator.js +1 -1
  25. package/class-validator/dist/class-validator.js.map +1 -1
  26. package/class-validator/dist/class-validator.mjs +1 -1
  27. package/class-validator/dist/class-validator.modern.mjs +1 -1
  28. package/class-validator/dist/class-validator.modern.mjs.map +1 -1
  29. package/class-validator/dist/class-validator.module.js +1 -1
  30. package/class-validator/dist/class-validator.module.js.map +1 -1
  31. package/class-validator/dist/class-validator.umd.js +1 -1
  32. package/class-validator/dist/class-validator.umd.js.map +1 -1
  33. package/class-validator/dist/index.d.ts +0 -1
  34. package/class-validator/src/__tests__/Form.tsx +26 -0
  35. package/class-validator/src/__tests__/__fixtures__/data.ts +1 -1
  36. package/class-validator/src/__tests__/class-validator.ts +21 -10
  37. package/class-validator/src/class-validator.ts +46 -12
  38. package/class-validator/src/index.ts +0 -1
  39. package/computed-types/dist/computed-types.d.ts +18 -2
  40. package/computed-types/dist/computed-types.js.map +1 -1
  41. package/computed-types/dist/computed-types.mjs +1 -1
  42. package/computed-types/dist/computed-types.modern.mjs +1 -1
  43. package/computed-types/dist/computed-types.modern.mjs.map +1 -1
  44. package/computed-types/dist/computed-types.module.js +1 -1
  45. package/computed-types/dist/computed-types.module.js.map +1 -1
  46. package/computed-types/dist/computed-types.umd.js.map +1 -1
  47. package/computed-types/dist/index.d.ts +0 -1
  48. package/computed-types/src/__tests__/Form-native-validation.tsx +2 -4
  49. package/computed-types/src/__tests__/Form.tsx +30 -6
  50. package/computed-types/src/__tests__/__fixtures__/data.ts +1 -1
  51. package/computed-types/src/__tests__/computed-types.ts +2 -0
  52. package/computed-types/src/computed-types.ts +24 -7
  53. package/computed-types/src/index.ts +0 -1
  54. package/effect-ts/dist/effect-ts.d.ts +19 -2
  55. package/effect-ts/dist/effect-ts.js.map +1 -1
  56. package/effect-ts/dist/effect-ts.mjs +1 -1
  57. package/effect-ts/dist/effect-ts.modern.mjs +1 -1
  58. package/effect-ts/dist/effect-ts.modern.mjs.map +1 -1
  59. package/effect-ts/dist/effect-ts.module.js +1 -1
  60. package/effect-ts/dist/effect-ts.module.js.map +1 -1
  61. package/effect-ts/dist/effect-ts.umd.js.map +1 -1
  62. package/effect-ts/dist/index.d.ts +0 -1
  63. package/effect-ts/src/__tests__/Form.tsx +30 -4
  64. package/effect-ts/src/__tests__/__fixtures__/data.ts +1 -1
  65. package/effect-ts/src/effect-ts.ts +30 -8
  66. package/effect-ts/src/index.ts +0 -1
  67. package/fluentvalidation-ts/dist/fluentvalidation-ts.d.ts +21 -0
  68. package/fluentvalidation-ts/dist/fluentvalidation-ts.js +1 -1
  69. package/fluentvalidation-ts/dist/fluentvalidation-ts.js.map +1 -1
  70. package/fluentvalidation-ts/dist/fluentvalidation-ts.mjs +1 -1
  71. package/fluentvalidation-ts/dist/fluentvalidation-ts.modern.mjs +1 -1
  72. package/fluentvalidation-ts/dist/fluentvalidation-ts.modern.mjs.map +1 -1
  73. package/fluentvalidation-ts/dist/fluentvalidation-ts.module.js +1 -1
  74. package/fluentvalidation-ts/dist/fluentvalidation-ts.module.js.map +1 -1
  75. package/fluentvalidation-ts/dist/fluentvalidation-ts.umd.js +1 -1
  76. package/fluentvalidation-ts/dist/fluentvalidation-ts.umd.js.map +1 -1
  77. package/fluentvalidation-ts/src/fluentvalidation-ts.ts +24 -3
  78. package/io-ts/dist/errorsToRecord.d.ts +5 -1
  79. package/io-ts/dist/index.d.ts +0 -1
  80. package/io-ts/dist/io-ts.d.ts +19 -2
  81. package/io-ts/dist/io-ts.js.map +1 -1
  82. package/io-ts/dist/io-ts.mjs +1 -1
  83. package/io-ts/dist/io-ts.modern.mjs +1 -1
  84. package/io-ts/dist/io-ts.modern.mjs.map +1 -1
  85. package/io-ts/dist/io-ts.module.js +1 -1
  86. package/io-ts/dist/io-ts.module.js.map +1 -1
  87. package/io-ts/dist/io-ts.umd.js.map +1 -1
  88. package/io-ts/src/__tests__/Form-native-validation.tsx +4 -11
  89. package/io-ts/src/__tests__/Form.tsx +30 -6
  90. package/io-ts/src/__tests__/__fixtures__/data.ts +2 -19
  91. package/io-ts/src/errorsToRecord.ts +4 -1
  92. package/io-ts/src/index.ts +0 -1
  93. package/io-ts/src/io-ts.ts +47 -25
  94. package/joi/dist/joi.d.ts +17 -0
  95. package/joi/dist/joi.js.map +1 -1
  96. package/joi/dist/joi.modern.mjs.map +1 -1
  97. package/joi/dist/joi.module.js.map +1 -1
  98. package/joi/dist/joi.umd.js.map +1 -1
  99. package/joi/src/joi.ts +17 -0
  100. package/nope/dist/nope.d.ts +17 -0
  101. package/nope/dist/nope.js +1 -1
  102. package/nope/dist/nope.js.map +1 -1
  103. package/nope/dist/nope.mjs +1 -1
  104. package/nope/dist/nope.modern.mjs.map +1 -1
  105. package/nope/dist/nope.module.js +1 -1
  106. package/nope/dist/nope.module.js.map +1 -1
  107. package/nope/dist/nope.umd.js +1 -1
  108. package/nope/dist/nope.umd.js.map +1 -1
  109. package/nope/src/nope.ts +17 -0
  110. package/package.json +5 -1
  111. package/standard-schema/dist/index.d.ts +0 -1
  112. package/standard-schema/dist/standard-schema.d.ts +25 -2
  113. package/standard-schema/dist/standard-schema.js +1 -1
  114. package/standard-schema/dist/standard-schema.js.map +1 -1
  115. package/standard-schema/dist/standard-schema.mjs +1 -1
  116. package/standard-schema/dist/standard-schema.modern.mjs +1 -1
  117. package/standard-schema/dist/standard-schema.modern.mjs.map +1 -1
  118. package/standard-schema/dist/standard-schema.module.js +1 -1
  119. package/standard-schema/dist/standard-schema.module.js.map +1 -1
  120. package/standard-schema/dist/standard-schema.umd.js +1 -1
  121. package/standard-schema/dist/standard-schema.umd.js.map +1 -1
  122. package/standard-schema/package.json +1 -0
  123. package/standard-schema/src/__tests__/Form.tsx +32 -6
  124. package/standard-schema/src/__tests__/__fixtures__/data.ts +67 -21
  125. package/standard-schema/src/__tests__/__snapshots__/standard-schema.ts.snap +222 -11
  126. package/standard-schema/src/__tests__/standard-schema.ts +49 -3
  127. package/standard-schema/src/index.ts +0 -1
  128. package/standard-schema/src/standard-schema.ts +54 -11
  129. package/superstruct/dist/index.d.ts +0 -1
  130. package/superstruct/dist/superstruct.d.ts +22 -2
  131. package/superstruct/dist/superstruct.js.map +1 -1
  132. package/superstruct/dist/superstruct.mjs +1 -1
  133. package/superstruct/dist/superstruct.modern.mjs +1 -1
  134. package/superstruct/dist/superstruct.modern.mjs.map +1 -1
  135. package/superstruct/dist/superstruct.module.js +1 -1
  136. package/superstruct/dist/superstruct.module.js.map +1 -1
  137. package/superstruct/dist/superstruct.umd.js.map +1 -1
  138. package/superstruct/src/__tests__/Form.tsx +26 -0
  139. package/superstruct/src/__tests__/__fixtures__/data.ts +1 -1
  140. package/superstruct/src/index.ts +0 -1
  141. package/superstruct/src/superstruct.ts +31 -9
  142. package/typanion/dist/index.d.ts +0 -1
  143. package/typanion/dist/typanion.d.ts +18 -2
  144. package/typanion/dist/typanion.js.map +1 -1
  145. package/typanion/dist/typanion.mjs +1 -1
  146. package/typanion/dist/typanion.modern.mjs +1 -1
  147. package/typanion/dist/typanion.modern.mjs.map +1 -1
  148. package/typanion/dist/typanion.module.js +1 -1
  149. package/typanion/dist/typanion.module.js.map +1 -1
  150. package/typanion/dist/typanion.umd.js.map +1 -1
  151. package/typanion/src/__tests__/Form-native-validation.tsx +6 -14
  152. package/typanion/src/__tests__/Form.tsx +32 -8
  153. package/typanion/src/__tests__/__fixtures__/data.ts +9 -9
  154. package/typanion/src/__tests__/typanion.ts +3 -3
  155. package/typanion/src/index.ts +0 -1
  156. package/typanion/src/typanion.ts +32 -9
  157. package/typebox/dist/index.d.ts +0 -1
  158. package/typebox/dist/typebox.d.ts +20 -2
  159. package/typebox/dist/typebox.js +1 -1
  160. package/typebox/dist/typebox.js.map +1 -1
  161. package/typebox/dist/typebox.mjs +1 -1
  162. package/typebox/dist/typebox.modern.mjs +1 -1
  163. package/typebox/dist/typebox.modern.mjs.map +1 -1
  164. package/typebox/dist/typebox.module.js +1 -1
  165. package/typebox/dist/typebox.module.js.map +1 -1
  166. package/typebox/dist/typebox.umd.js +1 -1
  167. package/typebox/dist/typebox.umd.js.map +1 -1
  168. package/typebox/src/__tests__/Form-compiler.tsx +4 -8
  169. package/typebox/src/__tests__/Form.tsx +32 -6
  170. package/typebox/src/__tests__/__fixtures__/data.ts +1 -1
  171. package/typebox/src/index.ts +0 -1
  172. package/typebox/src/typebox.ts +33 -7
  173. package/typeschema/dist/typeschema.d.ts +17 -0
  174. package/typeschema/dist/typeschema.js.map +1 -1
  175. package/typeschema/dist/typeschema.modern.mjs.map +1 -1
  176. package/typeschema/dist/typeschema.module.js.map +1 -1
  177. package/typeschema/dist/typeschema.umd.js.map +1 -1
  178. package/typeschema/src/typeschema.ts +17 -0
  179. package/valibot/dist/index.d.ts +0 -1
  180. package/valibot/dist/valibot.d.ts +31 -2
  181. package/valibot/dist/valibot.js.map +1 -1
  182. package/valibot/dist/valibot.mjs +1 -1
  183. package/valibot/dist/valibot.modern.mjs +1 -1
  184. package/valibot/dist/valibot.modern.mjs.map +1 -1
  185. package/valibot/dist/valibot.module.js +1 -1
  186. package/valibot/dist/valibot.module.js.map +1 -1
  187. package/valibot/dist/valibot.umd.js.map +1 -1
  188. package/valibot/src/__tests__/Form.tsx +26 -0
  189. package/valibot/src/__tests__/__fixtures__/data.ts +7 -3
  190. package/valibot/src/index.ts +0 -1
  191. package/valibot/src/valibot.ts +52 -5
  192. package/vest/dist/vest.d.ts +18 -0
  193. package/vest/dist/vest.js.map +1 -1
  194. package/vest/dist/vest.modern.mjs.map +1 -1
  195. package/vest/dist/vest.module.js.map +1 -1
  196. package/vest/dist/vest.umd.js.map +1 -1
  197. package/vest/src/vest.ts +18 -0
  198. package/vine/dist/index.d.ts +0 -1
  199. package/vine/dist/vine.d.ts +25 -2
  200. package/vine/dist/vine.js +1 -1
  201. package/vine/dist/vine.js.map +1 -1
  202. package/vine/dist/vine.mjs +1 -1
  203. package/vine/dist/vine.modern.mjs +1 -1
  204. package/vine/dist/vine.modern.mjs.map +1 -1
  205. package/vine/dist/vine.module.js +1 -1
  206. package/vine/dist/vine.module.js.map +1 -1
  207. package/vine/dist/vine.umd.js +1 -1
  208. package/vine/dist/vine.umd.js.map +1 -1
  209. package/vine/src/__tests__/Form-native-validation.tsx +2 -4
  210. package/vine/src/__tests__/Form.tsx +30 -6
  211. package/vine/src/__tests__/__fixtures__/data.ts +1 -1
  212. package/vine/src/index.ts +0 -1
  213. package/vine/src/vine.ts +37 -9
  214. package/yup/dist/yup.d.ts +18 -7
  215. package/yup/dist/yup.js.map +1 -1
  216. package/yup/dist/yup.modern.mjs.map +1 -1
  217. package/yup/dist/yup.module.js.map +1 -1
  218. package/yup/dist/yup.umd.js.map +1 -1
  219. package/yup/src/yup.ts +21 -10
  220. package/zod/dist/index.d.ts +0 -1
  221. package/zod/dist/zod.d.ts +25 -2
  222. package/zod/dist/zod.js +1 -1
  223. package/zod/dist/zod.js.map +1 -1
  224. package/zod/dist/zod.mjs +1 -1
  225. package/zod/dist/zod.modern.mjs +1 -1
  226. package/zod/dist/zod.modern.mjs.map +1 -1
  227. package/zod/dist/zod.module.js +1 -1
  228. package/zod/dist/zod.module.js.map +1 -1
  229. package/zod/dist/zod.umd.js +1 -1
  230. package/zod/dist/zod.umd.js.map +1 -1
  231. package/zod/src/__tests__/Form.tsx +30 -6
  232. package/zod/src/__tests__/__fixtures__/data.ts +3 -3
  233. package/zod/src/index.ts +0 -1
  234. package/zod/src/zod.ts +39 -8
  235. package/arktype/dist/types.d.ts +0 -9
  236. package/arktype/src/types.ts +0 -18
  237. package/class-validator/dist/types.d.ts +0 -12
  238. package/class-validator/src/types.ts +0 -16
  239. package/computed-types/dist/types.d.ts +0 -2
  240. package/computed-types/src/types.ts +0 -13
  241. package/effect-ts/dist/types.d.ts +0 -4
  242. package/effect-ts/src/types.ts +0 -12
  243. package/io-ts/dist/types.d.ts +0 -7
  244. package/io-ts/src/types.ts +0 -18
  245. package/standard-schema/dist/types.d.ts +0 -3
  246. package/standard-schema/src/types.ts +0 -10
  247. package/superstruct/dist/types.d.ts +0 -11
  248. package/superstruct/src/types.ts +0 -20
  249. package/typanion/dist/types.d.ts +0 -9
  250. package/typanion/src/types.ts +0 -20
  251. package/typebox/dist/types.d.ts +0 -4
  252. package/typebox/src/types.ts +0 -11
  253. package/valibot/dist/types.d.ts +0 -13
  254. package/valibot/src/types.ts +0 -34
  255. package/vine/dist/types.d.ts +0 -6
  256. package/vine/src/types.ts +0 -13
  257. package/zod/dist/types.d.ts +0 -13
  258. package/zod/src/types.ts +0 -22
@@ -1,63 +1,274 @@
1
1
  // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
2
 
3
+ exports[`standardSchemaResolver > should correctly handle path segments that are objects 1`] = `
4
+ {
5
+ "errors": {
6
+ "like": [
7
+ {
8
+ "id": {
9
+ "message": "Custom error",
10
+ "ref": undefined,
11
+ "type": "",
12
+ },
13
+ },
14
+ ],
15
+ "username": {
16
+ "message": "Custom error",
17
+ "ref": {
18
+ "name": "username",
19
+ },
20
+ "type": "",
21
+ },
22
+ },
23
+ "values": {},
24
+ }
25
+ `;
26
+
3
27
  exports[`standardSchemaResolver > should return a single error from standardSchemaResolver when validation fails 1`] = `
4
28
  {
5
29
  "errors": {
6
30
  "accessToken": {
7
- "message": "accessToken must be a number or a string (was missing)",
31
+ "message": "Invalid input",
8
32
  "ref": undefined,
33
+ "type": "",
9
34
  },
10
35
  "birthYear": {
11
- "message": "birthYear must be a number (was a string)",
36
+ "message": "Expected number, received string",
12
37
  "ref": undefined,
38
+ "type": "",
13
39
  },
14
40
  "dateStr": {
15
- "message": "dateStr must be a Date (was missing)",
41
+ "message": "Required",
16
42
  "ref": undefined,
43
+ "type": "",
17
44
  },
18
45
  "email": {
19
- "message": "email must be an email address (was "")",
46
+ "message": "Invalid email",
20
47
  "ref": {
21
48
  "name": "email",
22
49
  },
50
+ "type": "",
23
51
  },
24
52
  "enabled": {
25
- "message": "enabled must be boolean (was missing)",
53
+ "message": "Required",
26
54
  "ref": undefined,
55
+ "type": "",
27
56
  },
28
57
  "like": [
29
58
  {
30
59
  "id": {
31
- "message": "like[0].id must be a number (was a string)",
60
+ "message": "Expected number, received string",
32
61
  "ref": undefined,
62
+ "type": "",
33
63
  },
34
64
  "name": {
35
- "message": "like[0].name must be a string (was missing)",
65
+ "message": "Required",
36
66
  "ref": undefined,
67
+ "type": "",
37
68
  },
38
69
  },
39
70
  ],
40
71
  "password": {
41
- "message": "password must be matched by .*[A-Za-z].* or matched by .*\\d.* (was "___")",
72
+ "message": "One uppercase character",
42
73
  "ref": {
43
74
  "name": "password",
44
75
  },
76
+ "type": "",
45
77
  },
46
78
  "repeatPassword": {
47
- "message": "repeatPassword must be a string (was missing)",
79
+ "message": "Required",
48
80
  "ref": undefined,
81
+ "type": "",
49
82
  },
50
83
  "tags": {
51
- "message": "tags must be an array (was missing)",
84
+ "message": "Required",
52
85
  "ref": undefined,
86
+ "type": "",
87
+ },
88
+ "url": {
89
+ "message": "Custom error url",
90
+ "ref": undefined,
91
+ "type": "",
53
92
  },
54
93
  "username": {
55
- "message": "username must be a string (was missing)",
94
+ "message": "Required",
56
95
  "ref": {
57
96
  "name": "username",
58
97
  },
98
+ "type": "",
59
99
  },
60
100
  },
61
101
  "values": {},
62
102
  }
63
103
  `;
104
+
105
+ exports[`standardSchemaResolver > should return all the errors from standardSchemaResolver when validation fails with \`validateAllFieldCriteria\` set to true 1`] = `
106
+ {
107
+ "errors": {
108
+ "accessToken": {
109
+ "message": "Invalid input",
110
+ "ref": undefined,
111
+ "type": "",
112
+ "types": {
113
+ "0": "Invalid input",
114
+ },
115
+ },
116
+ "birthYear": {
117
+ "message": "Expected number, received string",
118
+ "ref": undefined,
119
+ "type": "",
120
+ "types": {
121
+ "0": "Expected number, received string",
122
+ },
123
+ },
124
+ "dateStr": {
125
+ "message": "Required",
126
+ "ref": undefined,
127
+ "type": "",
128
+ "types": {
129
+ "0": "Required",
130
+ },
131
+ },
132
+ "email": {
133
+ "message": "Invalid email",
134
+ "ref": {
135
+ "name": "email",
136
+ },
137
+ "type": "",
138
+ "types": {
139
+ "0": "Invalid email",
140
+ },
141
+ },
142
+ "enabled": {
143
+ "message": "Required",
144
+ "ref": undefined,
145
+ "type": "",
146
+ "types": {
147
+ "0": "Required",
148
+ },
149
+ },
150
+ "like": [
151
+ {
152
+ "id": {
153
+ "message": "Expected number, received string",
154
+ "ref": undefined,
155
+ "type": "",
156
+ "types": {
157
+ "0": "Expected number, received string",
158
+ },
159
+ },
160
+ "name": {
161
+ "message": "Required",
162
+ "ref": undefined,
163
+ "type": "",
164
+ "types": {
165
+ "0": "Required",
166
+ },
167
+ },
168
+ },
169
+ ],
170
+ "password": {
171
+ "message": "One uppercase character",
172
+ "ref": {
173
+ "name": "password",
174
+ },
175
+ "type": "",
176
+ "types": {
177
+ "0": "One uppercase character",
178
+ "1": "One lowercase character",
179
+ "2": "One number",
180
+ "3": "Must be at least 8 characters in length",
181
+ },
182
+ },
183
+ "repeatPassword": {
184
+ "message": "Required",
185
+ "ref": undefined,
186
+ "type": "",
187
+ "types": {
188
+ "0": "Required",
189
+ },
190
+ },
191
+ "tags": {
192
+ "message": "Required",
193
+ "ref": undefined,
194
+ "type": "",
195
+ "types": {
196
+ "0": "Required",
197
+ },
198
+ },
199
+ "url": {
200
+ "message": "Custom error url",
201
+ "ref": undefined,
202
+ "type": "",
203
+ "types": {
204
+ "0": "Custom error url",
205
+ },
206
+ },
207
+ "username": {
208
+ "message": "Required",
209
+ "ref": {
210
+ "name": "username",
211
+ },
212
+ "type": "",
213
+ "types": {
214
+ "0": "Required",
215
+ },
216
+ },
217
+ },
218
+ "values": {},
219
+ }
220
+ `;
221
+
222
+ exports[`standardSchemaResolver > should return values from standardSchemaResolver when validation pass & raw=true 1`] = `
223
+ {
224
+ "errors": {},
225
+ "values": {
226
+ "accessToken": "accessToken",
227
+ "birthYear": 2000,
228
+ "dateStr": "2020-01-01T00:00:00.000Z",
229
+ "email": "john@doe.com",
230
+ "enabled": true,
231
+ "like": [
232
+ {
233
+ "id": 1,
234
+ "name": "name",
235
+ },
236
+ ],
237
+ "password": "Password123_",
238
+ "repeatPassword": "Password123_",
239
+ "tags": [
240
+ "tag1",
241
+ "tag2",
242
+ ],
243
+ "url": "https://react-hook-form.com/",
244
+ "username": "Doe",
245
+ },
246
+ }
247
+ `;
248
+
249
+ exports[`standardSchemaResolver > should return values from standardSchemaResolver when validation pass 1`] = `
250
+ {
251
+ "errors": {},
252
+ "values": {
253
+ "accessToken": "accessToken",
254
+ "birthYear": 2000,
255
+ "dateStr": 2020-01-01T00:00:00.000Z,
256
+ "email": "john@doe.com",
257
+ "enabled": true,
258
+ "like": [
259
+ {
260
+ "id": 1,
261
+ "name": "name",
262
+ },
263
+ ],
264
+ "password": "Password123_",
265
+ "repeatPassword": "Password123_",
266
+ "tags": [
267
+ "tag1",
268
+ "tag2",
269
+ ],
270
+ "url": "https://react-hook-form.com/",
271
+ "username": "Doe",
272
+ },
273
+ }
274
+ `;
@@ -1,16 +1,22 @@
1
1
  import { standardSchemaResolver } from '..';
2
- import { fields, invalidData, schema, validData } from './__fixtures__/data';
2
+ import {
3
+ customSchema,
4
+ fields,
5
+ invalidData,
6
+ schema,
7
+ validData,
8
+ } from './__fixtures__/data';
3
9
 
4
10
  const shouldUseNativeValidation = false;
5
11
 
6
12
  describe('standardSchemaResolver', () => {
7
- it('should return values from standardSchemaResolver when validation pass & raw=true', async () => {
13
+ it('should return values from standardSchemaResolver when validation pass', async () => {
8
14
  const result = await standardSchemaResolver(schema)(validData, undefined, {
9
15
  fields,
10
16
  shouldUseNativeValidation,
11
17
  });
12
18
 
13
- expect(result).toEqual({ errors: {}, values: validData });
19
+ expect(result).toMatchSnapshot();
14
20
  });
15
21
 
16
22
  it('should return a single error from standardSchemaResolver when validation fails', async () => {
@@ -25,4 +31,44 @@ describe('standardSchemaResolver', () => {
25
31
 
26
32
  expect(result).toMatchSnapshot();
27
33
  });
34
+
35
+ it('should return all the errors from standardSchemaResolver when validation fails with `validateAllFieldCriteria` set to true', async () => {
36
+ const result = await standardSchemaResolver(schema)(
37
+ invalidData,
38
+ undefined,
39
+ {
40
+ fields,
41
+ criteriaMode: 'all',
42
+ shouldUseNativeValidation,
43
+ },
44
+ );
45
+
46
+ expect(result).toMatchSnapshot();
47
+ });
48
+
49
+ it('should return values from standardSchemaResolver when validation pass & raw=true', async () => {
50
+ const validateSpy = vi.spyOn(schema['~standard'], 'validate');
51
+
52
+ const result = await standardSchemaResolver(schema, {
53
+ raw: true,
54
+ })(validData, undefined, {
55
+ fields,
56
+ shouldUseNativeValidation,
57
+ });
58
+
59
+ expect(validateSpy).toHaveBeenCalledTimes(1);
60
+ expect(result).toMatchSnapshot();
61
+ });
62
+ it('should correctly handle path segments that are objects', async () => {
63
+ const result = await standardSchemaResolver(customSchema)(
64
+ validData,
65
+ undefined,
66
+ {
67
+ fields,
68
+ shouldUseNativeValidation,
69
+ },
70
+ );
71
+
72
+ expect(result).toMatchSnapshot();
73
+ });
28
74
  });
@@ -1,2 +1 @@
1
1
  export * from './standard-schema';
2
- export * from './types';
@@ -1,34 +1,76 @@
1
1
  import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
2
2
  import { StandardSchemaV1 } from '@standard-schema/spec';
3
- import { FieldError } from 'react-hook-form';
4
- import type { Resolver } from './types';
3
+ import { getDotPath } from '@standard-schema/utils';
4
+ import { FieldError, FieldValues, Resolver } from 'react-hook-form';
5
5
 
6
- const parseIssues = (issues: readonly StandardSchemaV1.Issue[]) => {
6
+ function parseIssues(
7
+ issues: readonly StandardSchemaV1.Issue[],
8
+ validateAllFieldCriteria: boolean,
9
+ ) {
7
10
  const errors: Record<string, FieldError> = {};
8
11
 
9
12
  for (let i = 0; i < issues.length; i++) {
10
- const issue = issues[i];
11
- const path = issue.path?.join('.') ?? '';
13
+ const error = issues[i];
14
+ const path = getDotPath(error);
12
15
 
13
16
  if (path) {
14
17
  if (!errors[path]) {
15
- errors[path] = { message: issue.message } as FieldError;
18
+ errors[path] = { message: error.message, type: '' };
19
+ }
20
+
21
+ if (validateAllFieldCriteria) {
22
+ const types = errors[path].types || {};
23
+
24
+ errors[path].types = {
25
+ ...types,
26
+ [Object.keys(types).length]: error.message,
27
+ };
16
28
  }
17
29
  }
18
30
  }
19
31
 
20
32
  return errors;
21
- };
33
+ }
22
34
 
23
- export const standardSchemaResolver: Resolver =
24
- (schema) => async (values, _, options) => {
35
+ /**
36
+ * Creates a resolver for react-hook-form that validates data using a Standard Schema.
37
+ *
38
+ * @param {Schema} schema - The Standard Schema to validate against
39
+ * @param {Object} resolverOptions - Options for the resolver
40
+ * @param {boolean} [resolverOptions.raw=false] - Whether to return raw input values instead of parsed values
41
+ * @returns {Resolver} A resolver function compatible with react-hook-form
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * const schema = z.object({
46
+ * name: z.string().min(2),
47
+ * age: z.number().min(18)
48
+ * });
49
+ *
50
+ * useForm({
51
+ * resolver: standardSchemaResolver(schema)
52
+ * });
53
+ * ```
54
+ */
55
+ export function standardSchemaResolver<
56
+ Schema extends StandardSchemaV1<FieldValues>,
57
+ >(
58
+ schema: Schema,
59
+ resolverOptions: {
60
+ raw?: boolean;
61
+ } = {},
62
+ ): Resolver<StandardSchemaV1.InferOutput<Schema>> {
63
+ return async (values, _, options) => {
25
64
  let result = schema['~standard'].validate(values);
26
65
  if (result instanceof Promise) {
27
66
  result = await result;
28
67
  }
29
68
 
30
69
  if (result.issues) {
31
- const errors = parseIssues(result.issues);
70
+ const errors = parseIssues(
71
+ result.issues,
72
+ !options.shouldUseNativeValidation && options.criteriaMode === 'all',
73
+ );
32
74
 
33
75
  return {
34
76
  values: {},
@@ -39,7 +81,8 @@ export const standardSchemaResolver: Resolver =
39
81
  options.shouldUseNativeValidation && validateFieldsNatively({}, options);
40
82
 
41
83
  return {
42
- values: values,
84
+ values: resolverOptions.raw ? Object.assign({}, values) : result.value,
43
85
  errors: {},
44
86
  };
45
87
  };
88
+ }
@@ -1,2 +1 @@
1
1
  export * from './superstruct';
2
- export * from './types';
@@ -1,2 +1,22 @@
1
- import { Resolver } from './types';
2
- export declare const superstructResolver: Resolver;
1
+ import { FieldValues, Resolver } from 'react-hook-form';
2
+ import { Infer, Struct, validate } from 'superstruct';
3
+ /**
4
+ * Creates a resolver for react-hook-form using Superstruct schema validation
5
+ * @param {Struct<TFieldValues, any>} schema - The Superstruct schema to validate against
6
+ * @param {Parameters<typeof validate>[2]} [schemaOptions] - Optional Superstruct validation options
7
+ * @param {Object} resolverOptions - Additional resolver configuration
8
+ * @param {boolean} [resolverOptions.raw=false] - If true, returns raw values rather than validated results
9
+ * @returns {Resolver<Infer<typeof schema>>} A resolver function compatible with react-hook-form
10
+ * @example
11
+ * const schema = struct({
12
+ * name: string(),
13
+ * age: number()
14
+ * });
15
+ *
16
+ * useForm({
17
+ * resolver: superstructResolver(schema)
18
+ * });
19
+ */
20
+ export declare function superstructResolver<TFieldValues extends FieldValues>(schema: Struct<TFieldValues, any>, schemaOptions?: Parameters<typeof validate>[2], resolverOptions?: {
21
+ raw?: boolean;
22
+ }): Resolver<Infer<typeof schema>>;
@@ -1 +1 @@
1
- {"version":3,"file":"superstruct.js","sources":["../src/superstruct.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError } from 'react-hook-form';\n\nimport { StructError, validate } from 'superstruct';\nimport { Resolver } from './types';\n\nconst parseErrorSchema = (error: StructError) =>\n error.failures().reduce<Record<string, FieldError>>(\n (previous, error) =>\n (previous[error.path.join('.')] = {\n message: error.message,\n type: error.type,\n }) && previous,\n {},\n );\n\nexport const superstructResolver: Resolver =\n (schema, schemaOptions, resolverOptions = {}) =>\n (values, _, options) => {\n const result = validate(values, schema, schemaOptions);\n\n if (result[0]) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(result[0]), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? Object.assign({}, values) : result[1],\n errors: {},\n };\n };\n"],"names":["schema","schemaOptions","resolverOptions","values","_","options","error","result","validate","errors","toNestErrors","failures","reduce","previous","path","join","message","type","shouldUseNativeValidation","validateFieldsNatively","raw","Object","assign"],"mappings":"0FAiBE,SAACA,EAAQC,EAAeC,GAAoB,gBAApBA,IAAAA,EAAkB,CAAE,GAC3CC,SAAAA,EAAQC,EAAGC,GACV,IAbsBC,EAahBC,EAASC,EAAAA,SAASL,EAAQH,EAAQC,GAExC,OAAIM,EAAO,GACF,CACLJ,OAAQ,GACRM,OAAQC,EAAAA,cAlBUJ,EAkBoBC,EAAO,GAjBnDD,EAAMK,WAAWC,OACf,SAACC,EAAUP,GAAK,OACbO,EAASP,EAAMQ,KAAKC,KAAK,MAAQ,CAChCC,QAASV,EAAMU,QACfC,KAAMX,EAAMW,QACRJ,CAAQ,EAChB,KAWsDR,KAItDA,EAAQa,2BAA6BC,EAAsBA,uBAAC,CAAE,EAAEd,GAEzD,CACLF,OAAQD,EAAgBkB,IAAMC,OAAOC,OAAO,CAAE,EAAEnB,GAAUI,EAAO,GACjEE,OAAQ,CAAA,GAEZ,CAAC"}
1
+ {"version":3,"file":"superstruct.js","sources":["../src/superstruct.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError, FieldValues, Resolver } from 'react-hook-form';\nimport { Infer, Struct, StructError, validate } from 'superstruct';\n\nfunction parseErrorSchema(error: StructError) {\n return error.failures().reduce<Record<string, FieldError>>(\n (previous, error) =>\n (previous[error.path.join('.')] = {\n message: error.message,\n type: error.type,\n }) && previous,\n {},\n );\n}\n\n/**\n * Creates a resolver for react-hook-form using Superstruct schema validation\n * @param {Struct<TFieldValues, any>} schema - The Superstruct schema to validate against\n * @param {Parameters<typeof validate>[2]} [schemaOptions] - Optional Superstruct validation options\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {boolean} [resolverOptions.raw=false] - If true, returns raw values rather than validated results\n * @returns {Resolver<Infer<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = struct({\n * name: string(),\n * age: number()\n * });\n *\n * useForm({\n * resolver: superstructResolver(schema)\n * });\n */\nexport function superstructResolver<TFieldValues extends FieldValues>(\n schema: Struct<TFieldValues, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions: {\n raw?: boolean;\n } = {},\n): Resolver<Infer<typeof schema>> {\n return (values, _, options) => {\n const result = validate(values, schema, schemaOptions);\n\n if (result[0]) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(result[0]), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? Object.assign({}, values) : result[1],\n errors: {},\n };\n };\n}\n"],"names":["schema","schemaOptions","resolverOptions","values","_","options","error","result","validate","errors","toNestErrors","failures","reduce","previous","path","join","message","type","shouldUseNativeValidation","validateFieldsNatively","raw","Object","assign"],"mappings":"mGAiCEA,EACAC,EACAC,GAIA,gBAJAA,IAAAA,EAEI,CAAE,GAEEC,SAAAA,EAAQC,EAAGC,GACjB,IApCsBC,EAoChBC,EAASC,EAAAA,SAASL,EAAQH,EAAQC,GAExC,OAAIM,EAAO,GACF,CACLJ,OAAQ,CAAA,EACRM,OAAQC,EAAYA,cAzCFJ,EAyCoBC,EAAO,GAxC5CD,EAAMK,WAAWC,OACtB,SAACC,EAAUP,GACT,OAACO,EAASP,EAAMQ,KAAKC,KAAK,MAAQ,CAChCC,QAASV,EAAMU,QACfC,KAAMX,EAAMW,QACRJ,CAAQ,EAChB,CAAA,IAkCsDR,KAItDA,EAAQa,2BAA6BC,yBAAuB,CAAA,EAAId,GAEzD,CACLF,OAAQD,EAAgBkB,IAAMC,OAAOC,OAAO,CAAE,EAAEnB,GAAUI,EAAO,GACjEE,OAAQ,CAAA,GAEZ,CACF"}
@@ -1,2 +1,2 @@
1
- import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";import{validate as o}from"superstruct";var s=function(s,t,a){return void 0===a&&(a={}),function(u,i,n){var f,m=o(u,s,t);return m[0]?{values:{},errors:r((f=m[0],f.failures().reduce(function(r,e){return(r[e.path.join(".")]={message:e.message,type:e.type})&&r},{})),n)}:(n.shouldUseNativeValidation&&e({},n),{values:a.raw?Object.assign({},u):m[1],errors:{}})}};export{s as superstructResolver};
1
+ import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";import{validate as o}from"superstruct";function s(s,t,u){return void 0===u&&(u={}),function(a,i,n){var f,m=o(a,s,t);return m[0]?{values:{},errors:r((f=m[0],f.failures().reduce(function(r,e){return(r[e.path.join(".")]={message:e.message,type:e.type})&&r},{})),n)}:(n.shouldUseNativeValidation&&e({},n),{values:u.raw?Object.assign({},a):m[1],errors:{}})}}export{s as superstructResolver};
2
2
  //# sourceMappingURL=superstruct.module.js.map
@@ -1,2 +1,2 @@
1
- import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";import{validate as s}from"superstruct";const o=(o,t,a={})=>(i,u,m)=>{const p=s(i,o,t);return p[0]?{values:{},errors:r((l=p[0],l.failures().reduce((r,e)=>(r[e.path.join(".")]={message:e.message,type:e.type})&&r,{})),m)}:(m.shouldUseNativeValidation&&e({},m),{values:a.raw?Object.assign({},i):p[1],errors:{}});var l};export{o as superstructResolver};
1
+ import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";import{validate as s}from"superstruct";function o(o,t,a={}){return(u,i,n)=>{const m=s(u,o,t);return m[0]?{values:{},errors:r((p=m[0],p.failures().reduce((r,e)=>(r[e.path.join(".")]={message:e.message,type:e.type})&&r,{})),n)}:(n.shouldUseNativeValidation&&e({},n),{values:a.raw?Object.assign({},u):m[1],errors:{}});var p}}export{o as superstructResolver};
2
2
  //# sourceMappingURL=superstruct.modern.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"superstruct.modern.mjs","sources":["../src/superstruct.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError } from 'react-hook-form';\n\nimport { StructError, validate } from 'superstruct';\nimport { Resolver } from './types';\n\nconst parseErrorSchema = (error: StructError) =>\n error.failures().reduce<Record<string, FieldError>>(\n (previous, error) =>\n (previous[error.path.join('.')] = {\n message: error.message,\n type: error.type,\n }) && previous,\n {},\n );\n\nexport const superstructResolver: Resolver =\n (schema, schemaOptions, resolverOptions = {}) =>\n (values, _, options) => {\n const result = validate(values, schema, schemaOptions);\n\n if (result[0]) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(result[0]), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? Object.assign({}, values) : result[1],\n errors: {},\n };\n };\n"],"names":["superstructResolver","schema","schemaOptions","resolverOptions","values","_","options","result","validate","errors","toNestErrors","error","failures","reduce","previous","path","join","message","type","shouldUseNativeValidation","validateFieldsNatively","raw","Object","assign"],"mappings":"sHAMA,MAUaA,EACXA,CAACC,EAAQC,EAAeC,EAAkB,CAAA,IAC1C,CAACC,EAAQC,EAAGC,KACV,MAAMC,EAASC,EAASJ,EAAQH,EAAQC,GAExC,OAAIK,EAAO,GACF,CACLH,OAAQ,CAAA,EACRK,OAAQC,GAlBUC,EAkBoBJ,EAAO,GAjBnDI,EAAMC,WAAWC,OACf,CAACC,EAAUH,KACRG,EAASH,EAAMI,KAAKC,KAAK,MAAQ,CAChCC,QAASN,EAAMM,QACfC,KAAMP,EAAMO,QACRJ,EACR,CAAA,IAWsDR,KAItDA,EAAQa,2BAA6BC,EAAuB,GAAId,GAEzD,CACLF,OAAQD,EAAgBkB,IAAMC,OAAOC,OAAO,CAAE,EAAEnB,GAAUG,EAAO,GACjEE,OAAQ,KA1BYE"}
1
+ {"version":3,"file":"superstruct.modern.mjs","sources":["../src/superstruct.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError, FieldValues, Resolver } from 'react-hook-form';\nimport { Infer, Struct, StructError, validate } from 'superstruct';\n\nfunction parseErrorSchema(error: StructError) {\n return error.failures().reduce<Record<string, FieldError>>(\n (previous, error) =>\n (previous[error.path.join('.')] = {\n message: error.message,\n type: error.type,\n }) && previous,\n {},\n );\n}\n\n/**\n * Creates a resolver for react-hook-form using Superstruct schema validation\n * @param {Struct<TFieldValues, any>} schema - The Superstruct schema to validate against\n * @param {Parameters<typeof validate>[2]} [schemaOptions] - Optional Superstruct validation options\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {boolean} [resolverOptions.raw=false] - If true, returns raw values rather than validated results\n * @returns {Resolver<Infer<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = struct({\n * name: string(),\n * age: number()\n * });\n *\n * useForm({\n * resolver: superstructResolver(schema)\n * });\n */\nexport function superstructResolver<TFieldValues extends FieldValues>(\n schema: Struct<TFieldValues, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions: {\n raw?: boolean;\n } = {},\n): Resolver<Infer<typeof schema>> {\n return (values, _, options) => {\n const result = validate(values, schema, schemaOptions);\n\n if (result[0]) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(result[0]), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? Object.assign({}, values) : result[1],\n errors: {},\n };\n };\n}\n"],"names":["superstructResolver","schema","schemaOptions","resolverOptions","values","_","options","result","validate","errors","toNestErrors","error","failures","reduce","previous","path","join","message","type","shouldUseNativeValidation","validateFieldsNatively","raw","Object","assign"],"mappings":"sHAgCgB,SAAAA,EACdC,EACAC,EACAC,EAEI,CAAE,GAEN,MAAO,CAACC,EAAQC,EAAGC,KACjB,MAAMC,EAASC,EAASJ,EAAQH,EAAQC,GAExC,OAAIK,EAAO,GACF,CACLH,OAAQ,CAAA,EACRK,OAAQC,GAzCUC,EAyCoBJ,EAAO,GAxC5CI,EAAMC,WAAWC,OACtB,CAACC,EAAUH,KACRG,EAASH,EAAMI,KAAKC,KAAK,MAAQ,CAChCC,QAASN,EAAMM,QACfC,KAAMP,EAAMO,QACRJ,EACR,KAkCsDR,KAItDA,EAAQa,2BAA6BC,EAAuB,GAAId,GAEzD,CACLF,OAAQD,EAAgBkB,IAAMC,OAAOC,OAAO,CAAA,EAAInB,GAAUG,EAAO,GACjEE,OAAQ,CAAA,IAjDd,IAA0BE,EAoD1B"}
@@ -1,2 +1,2 @@
1
- import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";import{validate as o}from"superstruct";var s=function(s,t,a){return void 0===a&&(a={}),function(u,i,n){var f,m=o(u,s,t);return m[0]?{values:{},errors:r((f=m[0],f.failures().reduce(function(r,e){return(r[e.path.join(".")]={message:e.message,type:e.type})&&r},{})),n)}:(n.shouldUseNativeValidation&&e({},n),{values:a.raw?Object.assign({},u):m[1],errors:{}})}};export{s as superstructResolver};
1
+ import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";import{validate as o}from"superstruct";function s(s,t,u){return void 0===u&&(u={}),function(a,i,n){var f,m=o(a,s,t);return m[0]?{values:{},errors:r((f=m[0],f.failures().reduce(function(r,e){return(r[e.path.join(".")]={message:e.message,type:e.type})&&r},{})),n)}:(n.shouldUseNativeValidation&&e({},n),{values:u.raw?Object.assign({},a):m[1],errors:{}})}}export{s as superstructResolver};
2
2
  //# sourceMappingURL=superstruct.module.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"superstruct.module.js","sources":["../src/superstruct.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError } from 'react-hook-form';\n\nimport { StructError, validate } from 'superstruct';\nimport { Resolver } from './types';\n\nconst parseErrorSchema = (error: StructError) =>\n error.failures().reduce<Record<string, FieldError>>(\n (previous, error) =>\n (previous[error.path.join('.')] = {\n message: error.message,\n type: error.type,\n }) && previous,\n {},\n );\n\nexport const superstructResolver: Resolver =\n (schema, schemaOptions, resolverOptions = {}) =>\n (values, _, options) => {\n const result = validate(values, schema, schemaOptions);\n\n if (result[0]) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(result[0]), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? Object.assign({}, values) : result[1],\n errors: {},\n };\n };\n"],"names":["superstructResolver","schema","schemaOptions","resolverOptions","values","_","options","error","result","validate","errors","toNestErrors","failures","reduce","previous","path","join","message","type","shouldUseNativeValidation","validateFieldsNatively","raw","Object","assign"],"mappings":"sHAMA,IAUaA,EACX,SAACC,EAAQC,EAAeC,GAAoB,gBAApBA,IAAAA,EAAkB,CAAE,GAC3CC,SAAAA,EAAQC,EAAGC,GACV,IAbsBC,EAahBC,EAASC,EAASL,EAAQH,EAAQC,GAExC,OAAIM,EAAO,GACF,CACLJ,OAAQ,GACRM,OAAQC,GAlBUJ,EAkBoBC,EAAO,GAjBnDD,EAAMK,WAAWC,OACf,SAACC,EAAUP,GAAK,OACbO,EAASP,EAAMQ,KAAKC,KAAK,MAAQ,CAChCC,QAASV,EAAMU,QACfC,KAAMX,EAAMW,QACRJ,CAAQ,EAChB,KAWsDR,KAItDA,EAAQa,2BAA6BC,EAAuB,CAAE,EAAEd,GAEzD,CACLF,OAAQD,EAAgBkB,IAAMC,OAAOC,OAAO,CAAE,EAAEnB,GAAUI,EAAO,GACjEE,OAAQ,CAAA,GAEZ,CAAC"}
1
+ {"version":3,"file":"superstruct.module.js","sources":["../src/superstruct.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError, FieldValues, Resolver } from 'react-hook-form';\nimport { Infer, Struct, StructError, validate } from 'superstruct';\n\nfunction parseErrorSchema(error: StructError) {\n return error.failures().reduce<Record<string, FieldError>>(\n (previous, error) =>\n (previous[error.path.join('.')] = {\n message: error.message,\n type: error.type,\n }) && previous,\n {},\n );\n}\n\n/**\n * Creates a resolver for react-hook-form using Superstruct schema validation\n * @param {Struct<TFieldValues, any>} schema - The Superstruct schema to validate against\n * @param {Parameters<typeof validate>[2]} [schemaOptions] - Optional Superstruct validation options\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {boolean} [resolverOptions.raw=false] - If true, returns raw values rather than validated results\n * @returns {Resolver<Infer<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = struct({\n * name: string(),\n * age: number()\n * });\n *\n * useForm({\n * resolver: superstructResolver(schema)\n * });\n */\nexport function superstructResolver<TFieldValues extends FieldValues>(\n schema: Struct<TFieldValues, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions: {\n raw?: boolean;\n } = {},\n): Resolver<Infer<typeof schema>> {\n return (values, _, options) => {\n const result = validate(values, schema, schemaOptions);\n\n if (result[0]) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(result[0]), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? Object.assign({}, values) : result[1],\n errors: {},\n };\n };\n}\n"],"names":["superstructResolver","schema","schemaOptions","resolverOptions","values","_","options","error","result","validate","errors","toNestErrors","failures","reduce","previous","path","join","message","type","shouldUseNativeValidation","validateFieldsNatively","raw","Object","assign"],"mappings":"+HAgCgBA,EACdC,EACAC,EACAC,GAIA,gBAJAA,IAAAA,EAEI,CAAE,GAEEC,SAAAA,EAAQC,EAAGC,GACjB,IApCsBC,EAoChBC,EAASC,EAASL,EAAQH,EAAQC,GAExC,OAAIM,EAAO,GACF,CACLJ,OAAQ,CAAA,EACRM,OAAQC,GAzCUJ,EAyCoBC,EAAO,GAxC5CD,EAAMK,WAAWC,OACtB,SAACC,EAAUP,GACT,OAACO,EAASP,EAAMQ,KAAKC,KAAK,MAAQ,CAChCC,QAASV,EAAMU,QACfC,KAAMX,EAAMW,QACRJ,CAAQ,EAChB,CAAA,IAkCsDR,KAItDA,EAAQa,2BAA6BC,EAAuB,CAAA,EAAId,GAEzD,CACLF,OAAQD,EAAgBkB,IAAMC,OAAOC,OAAO,CAAE,EAAEnB,GAAUI,EAAO,GACjEE,OAAQ,CAAA,GAEZ,CACF"}
@@ -1 +1 @@
1
- {"version":3,"file":"superstruct.umd.js","sources":["../src/superstruct.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError } from 'react-hook-form';\n\nimport { StructError, validate } from 'superstruct';\nimport { Resolver } from './types';\n\nconst parseErrorSchema = (error: StructError) =>\n error.failures().reduce<Record<string, FieldError>>(\n (previous, error) =>\n (previous[error.path.join('.')] = {\n message: error.message,\n type: error.type,\n }) && previous,\n {},\n );\n\nexport const superstructResolver: Resolver =\n (schema, schemaOptions, resolverOptions = {}) =>\n (values, _, options) => {\n const result = validate(values, schema, schemaOptions);\n\n if (result[0]) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(result[0]), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? Object.assign({}, values) : result[1],\n errors: {},\n };\n };\n"],"names":["schema","schemaOptions","resolverOptions","values","_","options","error","result","validate","errors","toNestErrors","failures","reduce","previous","path","join","message","type","shouldUseNativeValidation","validateFieldsNatively","raw","Object","assign"],"mappings":"4YAiBE,SAACA,EAAQC,EAAeC,GAAoB,gBAApBA,IAAAA,EAAkB,CAAE,GAC3CC,SAAAA,EAAQC,EAAGC,GACV,IAbsBC,EAahBC,EAASC,EAAAA,SAASL,EAAQH,EAAQC,GAExC,OAAIM,EAAO,GACF,CACLJ,OAAQ,GACRM,OAAQC,EAAAA,cAlBUJ,EAkBoBC,EAAO,GAjBnDD,EAAMK,WAAWC,OACf,SAACC,EAAUP,GAAK,OACbO,EAASP,EAAMQ,KAAKC,KAAK,MAAQ,CAChCC,QAASV,EAAMU,QACfC,KAAMX,EAAMW,QACRJ,CAAQ,EAChB,KAWsDR,KAItDA,EAAQa,2BAA6BC,EAAsBA,uBAAC,CAAE,EAAEd,GAEzD,CACLF,OAAQD,EAAgBkB,IAAMC,OAAOC,OAAO,CAAE,EAAEnB,GAAUI,EAAO,GACjEE,OAAQ,CAAA,GAEZ,CAAC"}
1
+ {"version":3,"file":"superstruct.umd.js","sources":["../src/superstruct.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError, FieldValues, Resolver } from 'react-hook-form';\nimport { Infer, Struct, StructError, validate } from 'superstruct';\n\nfunction parseErrorSchema(error: StructError) {\n return error.failures().reduce<Record<string, FieldError>>(\n (previous, error) =>\n (previous[error.path.join('.')] = {\n message: error.message,\n type: error.type,\n }) && previous,\n {},\n );\n}\n\n/**\n * Creates a resolver for react-hook-form using Superstruct schema validation\n * @param {Struct<TFieldValues, any>} schema - The Superstruct schema to validate against\n * @param {Parameters<typeof validate>[2]} [schemaOptions] - Optional Superstruct validation options\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {boolean} [resolverOptions.raw=false] - If true, returns raw values rather than validated results\n * @returns {Resolver<Infer<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = struct({\n * name: string(),\n * age: number()\n * });\n *\n * useForm({\n * resolver: superstructResolver(schema)\n * });\n */\nexport function superstructResolver<TFieldValues extends FieldValues>(\n schema: Struct<TFieldValues, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions: {\n raw?: boolean;\n } = {},\n): Resolver<Infer<typeof schema>> {\n return (values, _, options) => {\n const result = validate(values, schema, schemaOptions);\n\n if (result[0]) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(result[0]), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? Object.assign({}, values) : result[1],\n errors: {},\n };\n };\n}\n"],"names":["schema","schemaOptions","resolverOptions","values","_","options","error","result","validate","errors","toNestErrors","failures","reduce","previous","path","join","message","type","shouldUseNativeValidation","validateFieldsNatively","raw","Object","assign"],"mappings":"qZAiCEA,EACAC,EACAC,GAIA,gBAJAA,IAAAA,EAEI,CAAE,GAEEC,SAAAA,EAAQC,EAAGC,GACjB,IApCsBC,EAoChBC,EAASC,EAAAA,SAASL,EAAQH,EAAQC,GAExC,OAAIM,EAAO,GACF,CACLJ,OAAQ,CAAA,EACRM,OAAQC,EAAYA,cAzCFJ,EAyCoBC,EAAO,GAxC5CD,EAAMK,WAAWC,OACtB,SAACC,EAAUP,GACT,OAACO,EAASP,EAAMQ,KAAKC,KAAK,MAAQ,CAChCC,QAASV,EAAMU,QACfC,KAAMX,EAAMW,QACRJ,CAAQ,EAChB,CAAA,IAkCsDR,KAItDA,EAAQa,2BAA6BC,yBAAuB,CAAA,EAAId,GAEzD,CACLF,OAAQD,EAAgBkB,IAAMC,OAAOC,OAAO,CAAE,EAAEnB,GAAUI,EAAO,GACjEE,OAAQ,CAAA,GAEZ,CACF"}
@@ -58,3 +58,29 @@ test("form's validation with Superstruct and TypeScript's integration", async ()
58
58
  ).toBeInTheDocument();
59
59
  expect(handleSubmit).not.toHaveBeenCalled();
60
60
  });
61
+
62
+ export function TestComponentManualType({
63
+ onSubmit,
64
+ }: {
65
+ onSubmit: (data: FormData) => void;
66
+ }) {
67
+ const {
68
+ register,
69
+ handleSubmit,
70
+ formState: { errors },
71
+ } = useForm<Infer<typeof schema>, undefined, FormData>({
72
+ resolver: superstructResolver(schema), // Useful to check TypeScript regressions
73
+ });
74
+
75
+ return (
76
+ <form onSubmit={handleSubmit(onSubmit)}>
77
+ <input {...register('username')} />
78
+ {errors.username && <span role="alert">{errors.username.message}</span>}
79
+
80
+ <input {...register('password')} />
81
+ {errors.password && <span role="alert">{errors.password.message}</span>}
82
+
83
+ <button type="submit">submit</button>
84
+ </form>
85
+ );
86
+ }
@@ -53,7 +53,7 @@ export const invalidData = {
53
53
  email: '',
54
54
  birthYear: 'birthYear',
55
55
  like: [{ id: 'z' }],
56
- };
56
+ } as any as Infer<typeof schema>;
57
57
 
58
58
  export const fields: Record<InternalFieldName, Field['_f']> = {
59
59
  username: {
@@ -1,2 +1 @@
1
1
  export * from './superstruct';
2
- export * from './types';