@hookform/resolvers 2.0.0-rc.1 → 2.0.0
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 +115 -18
- package/dist/index.d.ts +1 -0
- package/dist/resolvers.js +2 -0
- package/dist/resolvers.js.map +1 -0
- package/dist/resolvers.mjs +2 -0
- package/dist/resolvers.modern.js +2 -0
- package/dist/resolvers.modern.js.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/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.tsx +61 -0
- package/joi/src/__tests__/__fixtures__/data.ts +76 -0
- package/joi/src/__tests__/__snapshots__/joi.ts.snap +283 -0
- package/joi/src/__tests__/joi.ts +89 -0
- package/joi/src/index.ts +2 -0
- package/joi/src/joi.ts +63 -0
- package/joi/src/types.ts +17 -0
- package/package.json +111 -55
- 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.tsx +62 -0
- package/superstruct/src/__tests__/__fixtures__/data.ts +73 -0
- package/superstruct/src/__tests__/__snapshots__/superstruct.ts.snap +64 -0
- package/superstruct/src/__tests__/superstruct.ts +20 -0
- package/superstruct/src/index.ts +2 -0
- package/superstruct/src/superstruct.ts +30 -0
- package/superstruct/src/types.ts +18 -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.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 +65 -0
- package/vest/src/index.ts +1 -0
- package/vest/src/types.ts +21 -0
- package/vest/src/vest.ts +45 -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.tsx +54 -0
- package/yup/src/__tests__/__fixtures__/data.ts +65 -0
- package/yup/src/__tests__/__snapshots__/yup.ts.snap +221 -0
- package/yup/src/__tests__/yup.ts +150 -0
- package/yup/src/index.ts +2 -0
- package/yup/src/types.ts +22 -0
- package/yup/src/yup.ts +65 -0
- package/zod/dist/index.d.ts +2 -0
- package/zod/dist/types.d.ts +6 -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.tsx +54 -0
- package/zod/src/__tests__/__fixtures__/data.ts +68 -0
- package/zod/src/__tests__/__snapshots__/zod.ts.snap +355 -0
- package/zod/src/__tests__/zod.ts +71 -0
- package/zod/src/index.ts +2 -0
- package/zod/src/types.ts +18 -0
- package/zod/src/zod.ts +65 -0
- package/dist/ie11/joi.d.ts +0 -3
- package/dist/ie11/joi.js +0 -104
- package/dist/ie11/joi.js.map +0 -1
- package/dist/ie11/superstruct.d.ts +0 -3
- package/dist/ie11/superstruct.js +0 -95
- package/dist/ie11/superstruct.js.map +0 -1
- package/dist/ie11/utils/convertArrayToPathName.d.ts +0 -2
- package/dist/ie11/utils/convertArrayToPathName.js +0 -12
- package/dist/ie11/utils/convertArrayToPathName.js.map +0 -1
- package/dist/ie11/yup.d.ts +0 -3
- package/dist/ie11/yup.js +0 -132
- package/dist/ie11/yup.js.map +0 -1
- package/dist/joi.d.ts +0 -3
- package/dist/joi.js +0 -41
- package/dist/joi.js.map +0 -1
- package/dist/superstruct.d.ts +0 -3
- package/dist/superstruct.js +0 -39
- package/dist/superstruct.js.map +0 -1
- package/dist/umd/index.js +0 -2
- package/dist/umd/index.js.map +0 -1
- package/dist/utils/convertArrayToPathName.d.ts +0 -2
- package/dist/utils/convertArrayToPathName.js +0 -8
- package/dist/utils/convertArrayToPathName.js.map +0 -1
- package/dist/yup.d.ts +0 -3
- package/dist/yup.js +0 -49
- package/dist/yup.js.map +0 -1
- package/joi.d.ts +0 -1
- package/joi.js +0 -1
- package/superstruct.js +0 -1
- package/yup.d.ts +0 -1
- package/yup.js +0 -1
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
+
|
|
3
|
+
exports[`joiResolver should return a single error from joiResolver when validation fails 1`] = `
|
|
4
|
+
Object {
|
|
5
|
+
"errors": Object {
|
|
6
|
+
"birthYear": Object {
|
|
7
|
+
"message": "\\"birthYear\\" must be a number",
|
|
8
|
+
"ref": undefined,
|
|
9
|
+
"type": "number.base",
|
|
10
|
+
},
|
|
11
|
+
"email": Object {
|
|
12
|
+
"message": "\\"email\\" is not allowed to be empty",
|
|
13
|
+
"ref": Object {
|
|
14
|
+
"name": "email",
|
|
15
|
+
},
|
|
16
|
+
"type": "string.empty",
|
|
17
|
+
},
|
|
18
|
+
"enabled": Object {
|
|
19
|
+
"message": "\\"enabled\\" is required",
|
|
20
|
+
"ref": undefined,
|
|
21
|
+
"type": "any.required",
|
|
22
|
+
},
|
|
23
|
+
"like": Array [
|
|
24
|
+
Object {
|
|
25
|
+
"id": Object {
|
|
26
|
+
"message": "\\"like[0].id\\" must be a number",
|
|
27
|
+
"ref": undefined,
|
|
28
|
+
"type": "number.base",
|
|
29
|
+
},
|
|
30
|
+
"name": Object {
|
|
31
|
+
"message": "\\"like[0].name\\" length must be 4 characters long",
|
|
32
|
+
"ref": undefined,
|
|
33
|
+
"type": "string.length",
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
"password": Object {
|
|
38
|
+
"message": "\\"password\\" with value \\"___\\" fails to match the required pattern: /^[a-zA-Z0-9]{3,30}$/",
|
|
39
|
+
"ref": Object {
|
|
40
|
+
"name": "password",
|
|
41
|
+
},
|
|
42
|
+
"type": "string.pattern.base",
|
|
43
|
+
},
|
|
44
|
+
"tags": Object {
|
|
45
|
+
"message": "\\"tags\\" is required",
|
|
46
|
+
"ref": undefined,
|
|
47
|
+
"type": "any.required",
|
|
48
|
+
},
|
|
49
|
+
"username": Object {
|
|
50
|
+
"message": "\\"username\\" is required",
|
|
51
|
+
"ref": Object {
|
|
52
|
+
"name": "username",
|
|
53
|
+
},
|
|
54
|
+
"type": "any.required",
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
"values": Object {},
|
|
58
|
+
}
|
|
59
|
+
`;
|
|
60
|
+
|
|
61
|
+
exports[`joiResolver should return a single error from joiResolver with \`mode: sync\` when validation fails 1`] = `
|
|
62
|
+
Object {
|
|
63
|
+
"errors": Object {
|
|
64
|
+
"birthYear": Object {
|
|
65
|
+
"message": "\\"birthYear\\" must be a number",
|
|
66
|
+
"ref": undefined,
|
|
67
|
+
"type": "number.base",
|
|
68
|
+
},
|
|
69
|
+
"email": Object {
|
|
70
|
+
"message": "\\"email\\" is not allowed to be empty",
|
|
71
|
+
"ref": Object {
|
|
72
|
+
"name": "email",
|
|
73
|
+
},
|
|
74
|
+
"type": "string.empty",
|
|
75
|
+
},
|
|
76
|
+
"enabled": Object {
|
|
77
|
+
"message": "\\"enabled\\" is required",
|
|
78
|
+
"ref": undefined,
|
|
79
|
+
"type": "any.required",
|
|
80
|
+
},
|
|
81
|
+
"like": Array [
|
|
82
|
+
Object {
|
|
83
|
+
"id": Object {
|
|
84
|
+
"message": "\\"like[0].id\\" must be a number",
|
|
85
|
+
"ref": undefined,
|
|
86
|
+
"type": "number.base",
|
|
87
|
+
},
|
|
88
|
+
"name": Object {
|
|
89
|
+
"message": "\\"like[0].name\\" length must be 4 characters long",
|
|
90
|
+
"ref": undefined,
|
|
91
|
+
"type": "string.length",
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
],
|
|
95
|
+
"password": Object {
|
|
96
|
+
"message": "\\"password\\" with value \\"___\\" fails to match the required pattern: /^[a-zA-Z0-9]{3,30}$/",
|
|
97
|
+
"ref": Object {
|
|
98
|
+
"name": "password",
|
|
99
|
+
},
|
|
100
|
+
"type": "string.pattern.base",
|
|
101
|
+
},
|
|
102
|
+
"tags": Object {
|
|
103
|
+
"message": "\\"tags\\" is required",
|
|
104
|
+
"ref": undefined,
|
|
105
|
+
"type": "any.required",
|
|
106
|
+
},
|
|
107
|
+
"username": Object {
|
|
108
|
+
"message": "\\"username\\" is required",
|
|
109
|
+
"ref": Object {
|
|
110
|
+
"name": "username",
|
|
111
|
+
},
|
|
112
|
+
"type": "any.required",
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
"values": Object {},
|
|
116
|
+
}
|
|
117
|
+
`;
|
|
118
|
+
|
|
119
|
+
exports[`joiResolver should return all the errors from joiResolver when validation fails with \`validateAllFieldCriteria\` set to true 1`] = `
|
|
120
|
+
Object {
|
|
121
|
+
"errors": Object {
|
|
122
|
+
"birthYear": Object {
|
|
123
|
+
"message": "\\"birthYear\\" must be a number",
|
|
124
|
+
"ref": undefined,
|
|
125
|
+
"type": "number.base",
|
|
126
|
+
"types": Object {
|
|
127
|
+
"number.base": "\\"birthYear\\" must be a number",
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
"email": Object {
|
|
131
|
+
"message": "\\"email\\" is not allowed to be empty",
|
|
132
|
+
"ref": Object {
|
|
133
|
+
"name": "email",
|
|
134
|
+
},
|
|
135
|
+
"type": "string.empty",
|
|
136
|
+
"types": Object {
|
|
137
|
+
"string.empty": "\\"email\\" is not allowed to be empty",
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
"enabled": Object {
|
|
141
|
+
"message": "\\"enabled\\" is required",
|
|
142
|
+
"ref": undefined,
|
|
143
|
+
"type": "any.required",
|
|
144
|
+
"types": Object {
|
|
145
|
+
"any.required": "\\"enabled\\" is required",
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
"like": Array [
|
|
149
|
+
Object {
|
|
150
|
+
"id": Object {
|
|
151
|
+
"message": "\\"like[0].id\\" must be a number",
|
|
152
|
+
"ref": undefined,
|
|
153
|
+
"type": "number.base",
|
|
154
|
+
"types": Object {
|
|
155
|
+
"number.base": "\\"like[0].id\\" must be a number",
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
"name": Object {
|
|
159
|
+
"message": "\\"like[0].name\\" length must be 4 characters long",
|
|
160
|
+
"ref": undefined,
|
|
161
|
+
"type": "string.length",
|
|
162
|
+
"types": Object {
|
|
163
|
+
"string.length": "\\"like[0].name\\" length must be 4 characters long",
|
|
164
|
+
"string.pattern.base": "\\"like[0].name\\" with value \\"r\\" fails to match the required pattern: /a/",
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
],
|
|
169
|
+
"password": Object {
|
|
170
|
+
"message": "\\"password\\" with value \\"___\\" fails to match the required pattern: /^[a-zA-Z0-9]{3,30}$/",
|
|
171
|
+
"ref": Object {
|
|
172
|
+
"name": "password",
|
|
173
|
+
},
|
|
174
|
+
"type": "string.pattern.base",
|
|
175
|
+
"types": Object {
|
|
176
|
+
"string.pattern.base": "\\"password\\" with value \\"___\\" fails to match the required pattern: /^[a-zA-Z0-9]{3,30}$/",
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
"tags": Object {
|
|
180
|
+
"message": "\\"tags\\" is required",
|
|
181
|
+
"ref": undefined,
|
|
182
|
+
"type": "any.required",
|
|
183
|
+
"types": Object {
|
|
184
|
+
"any.required": "\\"tags\\" is required",
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
"username": Object {
|
|
188
|
+
"message": "\\"username\\" is required",
|
|
189
|
+
"ref": Object {
|
|
190
|
+
"name": "username",
|
|
191
|
+
},
|
|
192
|
+
"type": "any.required",
|
|
193
|
+
"types": Object {
|
|
194
|
+
"any.required": "\\"username\\" is required",
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
},
|
|
198
|
+
"values": Object {},
|
|
199
|
+
}
|
|
200
|
+
`;
|
|
201
|
+
|
|
202
|
+
exports[`joiResolver should return all the errors from joiResolver when validation fails with \`validateAllFieldCriteria\` set to true and \`mode: sync\` 1`] = `
|
|
203
|
+
Object {
|
|
204
|
+
"errors": Object {
|
|
205
|
+
"birthYear": Object {
|
|
206
|
+
"message": "\\"birthYear\\" must be a number",
|
|
207
|
+
"ref": undefined,
|
|
208
|
+
"type": "number.base",
|
|
209
|
+
"types": Object {
|
|
210
|
+
"number.base": "\\"birthYear\\" must be a number",
|
|
211
|
+
},
|
|
212
|
+
},
|
|
213
|
+
"email": Object {
|
|
214
|
+
"message": "\\"email\\" is not allowed to be empty",
|
|
215
|
+
"ref": Object {
|
|
216
|
+
"name": "email",
|
|
217
|
+
},
|
|
218
|
+
"type": "string.empty",
|
|
219
|
+
"types": Object {
|
|
220
|
+
"string.empty": "\\"email\\" is not allowed to be empty",
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
"enabled": Object {
|
|
224
|
+
"message": "\\"enabled\\" is required",
|
|
225
|
+
"ref": undefined,
|
|
226
|
+
"type": "any.required",
|
|
227
|
+
"types": Object {
|
|
228
|
+
"any.required": "\\"enabled\\" is required",
|
|
229
|
+
},
|
|
230
|
+
},
|
|
231
|
+
"like": Array [
|
|
232
|
+
Object {
|
|
233
|
+
"id": Object {
|
|
234
|
+
"message": "\\"like[0].id\\" must be a number",
|
|
235
|
+
"ref": undefined,
|
|
236
|
+
"type": "number.base",
|
|
237
|
+
"types": Object {
|
|
238
|
+
"number.base": "\\"like[0].id\\" must be a number",
|
|
239
|
+
},
|
|
240
|
+
},
|
|
241
|
+
"name": Object {
|
|
242
|
+
"message": "\\"like[0].name\\" length must be 4 characters long",
|
|
243
|
+
"ref": undefined,
|
|
244
|
+
"type": "string.length",
|
|
245
|
+
"types": Object {
|
|
246
|
+
"string.length": "\\"like[0].name\\" length must be 4 characters long",
|
|
247
|
+
"string.pattern.base": "\\"like[0].name\\" with value \\"r\\" fails to match the required pattern: /a/",
|
|
248
|
+
},
|
|
249
|
+
},
|
|
250
|
+
},
|
|
251
|
+
],
|
|
252
|
+
"password": Object {
|
|
253
|
+
"message": "\\"password\\" with value \\"___\\" fails to match the required pattern: /^[a-zA-Z0-9]{3,30}$/",
|
|
254
|
+
"ref": Object {
|
|
255
|
+
"name": "password",
|
|
256
|
+
},
|
|
257
|
+
"type": "string.pattern.base",
|
|
258
|
+
"types": Object {
|
|
259
|
+
"string.pattern.base": "\\"password\\" with value \\"___\\" fails to match the required pattern: /^[a-zA-Z0-9]{3,30}$/",
|
|
260
|
+
},
|
|
261
|
+
},
|
|
262
|
+
"tags": Object {
|
|
263
|
+
"message": "\\"tags\\" is required",
|
|
264
|
+
"ref": undefined,
|
|
265
|
+
"type": "any.required",
|
|
266
|
+
"types": Object {
|
|
267
|
+
"any.required": "\\"tags\\" is required",
|
|
268
|
+
},
|
|
269
|
+
},
|
|
270
|
+
"username": Object {
|
|
271
|
+
"message": "\\"username\\" is required",
|
|
272
|
+
"ref": Object {
|
|
273
|
+
"name": "username",
|
|
274
|
+
},
|
|
275
|
+
"type": "any.required",
|
|
276
|
+
"types": Object {
|
|
277
|
+
"any.required": "\\"username\\" is required",
|
|
278
|
+
},
|
|
279
|
+
},
|
|
280
|
+
},
|
|
281
|
+
"values": Object {},
|
|
282
|
+
}
|
|
283
|
+
`;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { joiResolver } from '..';
|
|
2
|
+
import { schema, validData, fields, invalidData } from './__fixtures__/data';
|
|
3
|
+
|
|
4
|
+
describe('joiResolver', () => {
|
|
5
|
+
it('should return values from joiResolver when validation pass', async () => {
|
|
6
|
+
const validateAsyncSpy = jest.spyOn(schema, 'validateAsync');
|
|
7
|
+
const validateSpy = jest.spyOn(schema, 'validate');
|
|
8
|
+
|
|
9
|
+
const result = await joiResolver(schema)(validData, undefined, {
|
|
10
|
+
fields,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
expect(validateSpy).not.toHaveBeenCalled();
|
|
14
|
+
expect(validateAsyncSpy).toHaveBeenCalledTimes(1);
|
|
15
|
+
expect(result).toEqual({ errors: {}, values: validData });
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('should return values from joiResolver with `mode: sync` when validation pass', async () => {
|
|
19
|
+
const validateAsyncSpy = jest.spyOn(schema, 'validateAsync');
|
|
20
|
+
const validateSpy = jest.spyOn(schema, 'validate');
|
|
21
|
+
|
|
22
|
+
const result = await joiResolver(schema, undefined, {
|
|
23
|
+
mode: 'sync',
|
|
24
|
+
})(validData, undefined, { fields });
|
|
25
|
+
|
|
26
|
+
expect(validateAsyncSpy).not.toHaveBeenCalled();
|
|
27
|
+
expect(validateSpy).toHaveBeenCalledTimes(1);
|
|
28
|
+
expect(result).toEqual({ errors: {}, values: validData });
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('should return a single error from joiResolver when validation fails', async () => {
|
|
32
|
+
const result = await joiResolver(schema)(invalidData, undefined, {
|
|
33
|
+
fields,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
expect(result).toMatchSnapshot();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('should return a single error from joiResolver with `mode: sync` when validation fails', async () => {
|
|
40
|
+
const validateAsyncSpy = jest.spyOn(schema, 'validateAsync');
|
|
41
|
+
const validateSpy = jest.spyOn(schema, 'validate');
|
|
42
|
+
|
|
43
|
+
const result = await joiResolver(schema, undefined, {
|
|
44
|
+
mode: 'sync',
|
|
45
|
+
})(invalidData, undefined, { fields });
|
|
46
|
+
|
|
47
|
+
expect(validateAsyncSpy).not.toHaveBeenCalled();
|
|
48
|
+
expect(validateSpy).toHaveBeenCalledTimes(1);
|
|
49
|
+
expect(result).toMatchSnapshot();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('should return all the errors from joiResolver when validation fails with `validateAllFieldCriteria` set to true', async () => {
|
|
53
|
+
const result = await joiResolver(schema)(invalidData, undefined, {
|
|
54
|
+
fields,
|
|
55
|
+
criteriaMode: 'all',
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
expect(result).toMatchSnapshot();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('should return all the errors from joiResolver when validation fails with `validateAllFieldCriteria` set to true and `mode: sync`', async () => {
|
|
62
|
+
const result = await joiResolver(schema, undefined, { mode: 'sync' })(
|
|
63
|
+
invalidData,
|
|
64
|
+
undefined,
|
|
65
|
+
{
|
|
66
|
+
fields,
|
|
67
|
+
criteriaMode: 'all',
|
|
68
|
+
},
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
expect(result).toMatchSnapshot();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('should return values from joiResolver when validation pass and pass down the Joi context', async () => {
|
|
75
|
+
const context = { value: 'context' };
|
|
76
|
+
const validateAsyncSpy = jest.spyOn(schema, 'validateAsync');
|
|
77
|
+
const validateSpy = jest.spyOn(schema, 'validate');
|
|
78
|
+
|
|
79
|
+
const result = await joiResolver(schema)(validData, context, { fields });
|
|
80
|
+
|
|
81
|
+
expect(validateSpy).not.toHaveBeenCalled();
|
|
82
|
+
expect(validateAsyncSpy).toHaveBeenCalledTimes(1);
|
|
83
|
+
expect(validateAsyncSpy).toHaveBeenCalledWith(validData, {
|
|
84
|
+
abortEarly: false,
|
|
85
|
+
context,
|
|
86
|
+
});
|
|
87
|
+
expect(result).toEqual({ errors: {}, values: validData });
|
|
88
|
+
});
|
|
89
|
+
});
|
package/joi/src/index.ts
ADDED
package/joi/src/joi.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { appendErrors, FieldError } from 'react-hook-form';
|
|
2
|
+
import { toNestError } from '@hookform/resolvers';
|
|
3
|
+
import type { ValidationError } from 'joi';
|
|
4
|
+
import { Resolver } from './types';
|
|
5
|
+
|
|
6
|
+
const parseErrorSchema = (
|
|
7
|
+
error: ValidationError,
|
|
8
|
+
validateAllFieldCriteria: boolean,
|
|
9
|
+
) =>
|
|
10
|
+
error.details.length
|
|
11
|
+
? error.details.reduce<Record<string, FieldError>>((previous, error) => {
|
|
12
|
+
const _path = error.path.join('.');
|
|
13
|
+
|
|
14
|
+
if (!previous[_path]) {
|
|
15
|
+
previous[_path] = { message: error.message, type: error.type };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (validateAllFieldCriteria) {
|
|
19
|
+
previous[_path] = appendErrors(
|
|
20
|
+
_path,
|
|
21
|
+
validateAllFieldCriteria,
|
|
22
|
+
previous,
|
|
23
|
+
error.type,
|
|
24
|
+
error.message,
|
|
25
|
+
) as FieldError;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return previous;
|
|
29
|
+
}, {})
|
|
30
|
+
: {};
|
|
31
|
+
|
|
32
|
+
export const joiResolver: Resolver = (
|
|
33
|
+
schema,
|
|
34
|
+
schemaOptions = {
|
|
35
|
+
abortEarly: false,
|
|
36
|
+
},
|
|
37
|
+
resolverOptions = {},
|
|
38
|
+
) => async (values, context, options) => {
|
|
39
|
+
const _schemaOptions = Object.assign({}, schemaOptions, {
|
|
40
|
+
context,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
let result: Record<string, any> = {};
|
|
44
|
+
if (resolverOptions.mode === 'sync') {
|
|
45
|
+
result = schema.validate(values, _schemaOptions);
|
|
46
|
+
} else {
|
|
47
|
+
try {
|
|
48
|
+
result.value = await schema.validateAsync(values, _schemaOptions);
|
|
49
|
+
} catch (e) {
|
|
50
|
+
result.error = e;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
values: result.error ? {} : result.value,
|
|
56
|
+
errors: result.error
|
|
57
|
+
? toNestError(
|
|
58
|
+
parseErrorSchema(result.error, options.criteriaMode === 'all'),
|
|
59
|
+
options.fields,
|
|
60
|
+
)
|
|
61
|
+
: {},
|
|
62
|
+
};
|
|
63
|
+
};
|
package/joi/src/types.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FieldValues,
|
|
3
|
+
ResolverOptions,
|
|
4
|
+
ResolverResult,
|
|
5
|
+
UnpackNestedValue,
|
|
6
|
+
} from 'react-hook-form';
|
|
7
|
+
import type { AsyncValidationOptions, Schema } from 'joi';
|
|
8
|
+
|
|
9
|
+
export type Resolver = <T extends Schema>(
|
|
10
|
+
schema: T,
|
|
11
|
+
schemaOptions?: AsyncValidationOptions,
|
|
12
|
+
factoryOptions?: { mode?: 'async' | 'sync' },
|
|
13
|
+
) => <TFieldValues extends FieldValues, TContext>(
|
|
14
|
+
values: UnpackNestedValue<TFieldValues>,
|
|
15
|
+
context: TContext | undefined,
|
|
16
|
+
options: ResolverOptions<TFieldValues>,
|
|
17
|
+
) => Promise<ResolverResult<TFieldValues>>;
|
package/package.json
CHANGED
|
@@ -1,37 +1,89 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hookform/resolvers",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
3
|
+
"amdName": "hookformResolvers",
|
|
4
|
+
"version": "2.0.0",
|
|
5
|
+
"description": "React Hook Form validation resolvers: Yup, Joi, Superstruct, Zod, Vest and etc.",
|
|
6
|
+
"main": "dist/resolvers.js",
|
|
7
|
+
"module": "dist/resolvers.module.js",
|
|
8
|
+
"umd:main": "dist/resolvers.umd.js",
|
|
9
|
+
"source": "src/index.ts",
|
|
10
|
+
"types": "dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"browser": "./dist/resolvers.module.js",
|
|
14
|
+
"umd": "./dist/resolvers.umd.js",
|
|
15
|
+
"import": "./dist/resolvers.mjs",
|
|
16
|
+
"require": "./dist/resolvers.js"
|
|
17
|
+
},
|
|
18
|
+
"./zod": {
|
|
19
|
+
"browser": "./zod/dist/zod.module.js",
|
|
20
|
+
"umd": "./zod/dist/zod.umd.js",
|
|
21
|
+
"import": "./zod/dist/zod.mjs",
|
|
22
|
+
"require": "./zod/dist/zod.js"
|
|
23
|
+
},
|
|
24
|
+
"./yup": {
|
|
25
|
+
"browser": "./yup/dist/yup.module.js",
|
|
26
|
+
"umd": "./yup/dist/yup.umd.js",
|
|
27
|
+
"import": "./yup/dist/yup.mjs",
|
|
28
|
+
"require": "./yup/dist/yup.js"
|
|
29
|
+
},
|
|
30
|
+
"./joi": {
|
|
31
|
+
"browser": "./joi/dist/joi.module.js",
|
|
32
|
+
"umd": "./joi/dist/joi.umd.js",
|
|
33
|
+
"import": "./joi/dist/joi.mjs",
|
|
34
|
+
"require": "./joi/dist/joi.js"
|
|
35
|
+
},
|
|
36
|
+
"./vest": {
|
|
37
|
+
"browser": "./vest/dist/vest.module.js",
|
|
38
|
+
"umd": "./vest/dist/vest.umd.js",
|
|
39
|
+
"import": "./vest/dist/vest.mjs",
|
|
40
|
+
"require": "./vest/dist/vest.js"
|
|
41
|
+
},
|
|
42
|
+
"./superstruct": {
|
|
43
|
+
"browser": "./superstruct/dist/superstruct.module.js",
|
|
44
|
+
"umd": "./superstruct/dist/superstruct.umd.js",
|
|
45
|
+
"import": "./superstruct/dist/superstruct.mjs",
|
|
46
|
+
"require": "./superstruct/dist/superstruct.js"
|
|
47
|
+
},
|
|
48
|
+
"./package.json": "./package.json",
|
|
49
|
+
"./": "./"
|
|
50
|
+
},
|
|
9
51
|
"files": [
|
|
10
52
|
"dist",
|
|
11
|
-
"yup.
|
|
12
|
-
"yup
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
53
|
+
"yup/package.json",
|
|
54
|
+
"yup/src",
|
|
55
|
+
"yup/dist",
|
|
56
|
+
"zod/package.json",
|
|
57
|
+
"zod/src",
|
|
58
|
+
"zod/dist",
|
|
59
|
+
"vest/package.json",
|
|
60
|
+
"vest/src",
|
|
61
|
+
"vest/dist",
|
|
62
|
+
"joi/package.json",
|
|
63
|
+
"joi/src",
|
|
64
|
+
"joi/dist",
|
|
65
|
+
"superstruct/package.json",
|
|
66
|
+
"superstruct/src",
|
|
67
|
+
"superstruct/dist"
|
|
17
68
|
],
|
|
18
69
|
"publishConfig": {
|
|
19
70
|
"access": "public"
|
|
20
71
|
},
|
|
21
72
|
"scripts": {
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"build": "
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
73
|
+
"prepare": "run-s build:src build && check-export-map",
|
|
74
|
+
"build": "npm-run-all --parallel build:*",
|
|
75
|
+
"build:src": "microbundle build",
|
|
76
|
+
"build:zod": "microbundle --cwd zod --globals '@hookform/resolvers=hookformResolvers'",
|
|
77
|
+
"build:yup": "microbundle --cwd yup --globals '@hookform/resolvers=hookformResolvers'",
|
|
78
|
+
"build:joi": "microbundle --cwd joi --globals '@hookform/resolvers=hookformResolvers'",
|
|
79
|
+
"build:superstruct": "microbundle --cwd superstruct --globals '@hookform/resolvers=hookformResolvers'",
|
|
80
|
+
"build:vest": "microbundle --cwd vest --globals '@hookform/resolvers=hookformResolvers'",
|
|
81
|
+
"postbuild": "node ./config/node-13-exports.js",
|
|
82
|
+
"lint": "eslint . --ext .ts,.js --ignore-path .gitignore",
|
|
83
|
+
"lint:types": "tsc",
|
|
84
|
+
"test": "jest",
|
|
85
|
+
"test:watch": "yarn test --watchAll --coverage --runInBand",
|
|
86
|
+
"check:all": "npm-run-all --parallel lint:* test"
|
|
35
87
|
},
|
|
36
88
|
"keywords": [
|
|
37
89
|
"scheme",
|
|
@@ -41,8 +93,10 @@
|
|
|
41
93
|
"react-hook-form",
|
|
42
94
|
"yup",
|
|
43
95
|
"joi",
|
|
44
|
-
"
|
|
45
|
-
"typescript"
|
|
96
|
+
"superstruct",
|
|
97
|
+
"typescript",
|
|
98
|
+
"zod",
|
|
99
|
+
"vest"
|
|
46
100
|
],
|
|
47
101
|
"repository": {
|
|
48
102
|
"type": "git",
|
|
@@ -55,44 +109,46 @@
|
|
|
55
109
|
},
|
|
56
110
|
"homepage": "https://react-hook-form.com",
|
|
57
111
|
"devDependencies": {
|
|
58
|
-
"@
|
|
59
|
-
"@
|
|
60
|
-
"@
|
|
61
|
-
"@types/
|
|
62
|
-
"@types/
|
|
63
|
-
"@
|
|
64
|
-
"@typescript-eslint/
|
|
65
|
-
"
|
|
66
|
-
"eslint": "^7.
|
|
67
|
-
"eslint-config-prettier": "^
|
|
68
|
-
"eslint-plugin-prettier": "^3.1
|
|
69
|
-
"husky": "^4.
|
|
70
|
-
"jest": "^26.
|
|
71
|
-
"
|
|
112
|
+
"@testing-library/jest-dom": "^5.11.9",
|
|
113
|
+
"@testing-library/react": "^11.2.3",
|
|
114
|
+
"@testing-library/user-event": "^12.6.3",
|
|
115
|
+
"@types/jest": "^26.0.20",
|
|
116
|
+
"@types/react": "^17.0.0",
|
|
117
|
+
"@typescript-eslint/eslint-plugin": "^4.14.2",
|
|
118
|
+
"@typescript-eslint/parser": "^4.14.2",
|
|
119
|
+
"check-export-map": "^1.0.1",
|
|
120
|
+
"eslint": "^7.19.0",
|
|
121
|
+
"eslint-config-prettier": "^7.2.0",
|
|
122
|
+
"eslint-plugin-prettier": "^3.3.1",
|
|
123
|
+
"husky": "^4.3.8",
|
|
124
|
+
"jest": "^26.6.3",
|
|
125
|
+
"joi": "^17.3.0",
|
|
126
|
+
"lint-staged": "^10.5.3",
|
|
127
|
+
"microbundle": "^0.13.0",
|
|
72
128
|
"npm-run-all": "^4.1.5",
|
|
73
|
-
"prettier": "^2.
|
|
74
|
-
"react": "^
|
|
75
|
-
"react-
|
|
76
|
-
"
|
|
77
|
-
"
|
|
78
|
-
"
|
|
79
|
-
"
|
|
80
|
-
"
|
|
81
|
-
"
|
|
82
|
-
"
|
|
83
|
-
"
|
|
129
|
+
"prettier": "^2.2.1",
|
|
130
|
+
"react": "^17.0.1",
|
|
131
|
+
"react-dom": "^17.0.1",
|
|
132
|
+
"react-hook-form": "^7.0.0-alpha.2",
|
|
133
|
+
"semantic-release": "^17.3.7",
|
|
134
|
+
"superstruct": "^0.14.0",
|
|
135
|
+
"ts-jest": "^26.5.0",
|
|
136
|
+
"typescript": "^4.1.3",
|
|
137
|
+
"vest": "^3.1.2",
|
|
138
|
+
"yup": "^0.32.8",
|
|
139
|
+
"zod": "^1.11.11"
|
|
84
140
|
},
|
|
85
141
|
"peerDependencies": {
|
|
86
|
-
"react-hook-form": "
|
|
142
|
+
"react-hook-form": "^7.0.0"
|
|
87
143
|
},
|
|
88
144
|
"husky": {
|
|
89
145
|
"hooks": {
|
|
90
|
-
"pre-commit": "
|
|
146
|
+
"pre-commit": "yarn lint:types && lint-staged"
|
|
91
147
|
}
|
|
92
148
|
},
|
|
93
149
|
"lint-staged": {
|
|
94
150
|
"*.{js,ts}": [
|
|
95
|
-
"
|
|
151
|
+
"yarn lint --fix"
|
|
96
152
|
],
|
|
97
153
|
"*.{md,json,yml}": [
|
|
98
154
|
"prettier --write"
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var r=require("@hookform/resolvers"),e=require("superstruct");exports.superstructResolver=function(s,t){return function(u,o,n){var a,i=e.validate(u,s,t);return{values:i[1]||{},errors:i[0]?r.toNestError((a=i[0],a.failures().reduce(function(r,e){return(r[e.path.join(".")]={message:e.message,type:e.type})&&r},{})),n.fields):{}}}};
|
|
2
|
+
//# sourceMappingURL=superstruct.js.map
|