@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,68 @@
|
|
|
1
|
+
import { Field, InternalFieldName } from 'react-hook-form';
|
|
2
|
+
import * as z from 'zod';
|
|
3
|
+
|
|
4
|
+
export const schema = z
|
|
5
|
+
.object({
|
|
6
|
+
username: z.string().regex(/^\w+$/).min(3).max(30),
|
|
7
|
+
password: z.string().regex(/^[a-zA-Z0-9]{3,30}/),
|
|
8
|
+
repeatPassword: z.string(),
|
|
9
|
+
accessToken: z.union([z.string(), z.number()]).optional(),
|
|
10
|
+
birthYear: z.number().min(1900).max(2013).optional(),
|
|
11
|
+
email: z.string().email().optional(),
|
|
12
|
+
tags: z.array(z.string()),
|
|
13
|
+
enabled: z.boolean(),
|
|
14
|
+
like: z
|
|
15
|
+
.array(
|
|
16
|
+
z.object({
|
|
17
|
+
id: z.number(),
|
|
18
|
+
name: z.string().length(4),
|
|
19
|
+
}),
|
|
20
|
+
)
|
|
21
|
+
.optional(),
|
|
22
|
+
})
|
|
23
|
+
.refine((data) => data.password === data.repeatPassword, {
|
|
24
|
+
message: "Passwords don't match",
|
|
25
|
+
path: ['confirm'], // set path of error
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
export const validData: z.infer<typeof schema> = {
|
|
29
|
+
username: 'Doe',
|
|
30
|
+
password: 'Password123',
|
|
31
|
+
repeatPassword: 'Password123',
|
|
32
|
+
birthYear: 2000,
|
|
33
|
+
email: 'john@doe.com',
|
|
34
|
+
tags: ['tag1', 'tag2'],
|
|
35
|
+
enabled: true,
|
|
36
|
+
like: [
|
|
37
|
+
{
|
|
38
|
+
id: 1,
|
|
39
|
+
name: 'name',
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export const invalidData = {
|
|
45
|
+
password: '___',
|
|
46
|
+
email: '',
|
|
47
|
+
birthYear: 'birthYear',
|
|
48
|
+
like: [{ id: 'z' }],
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export const fields: Record<InternalFieldName, Field['_f']> = {
|
|
52
|
+
username: {
|
|
53
|
+
ref: { name: 'username' },
|
|
54
|
+
name: 'username',
|
|
55
|
+
},
|
|
56
|
+
password: {
|
|
57
|
+
ref: { name: 'password' },
|
|
58
|
+
name: 'password',
|
|
59
|
+
},
|
|
60
|
+
email: {
|
|
61
|
+
ref: { name: 'email' },
|
|
62
|
+
name: 'email',
|
|
63
|
+
},
|
|
64
|
+
birthday: {
|
|
65
|
+
ref: { name: 'birthday' },
|
|
66
|
+
name: 'birthday',
|
|
67
|
+
},
|
|
68
|
+
};
|
|
@@ -0,0 +1,355 @@
|
|
|
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
|
+
"birthYear": Object {
|
|
7
|
+
"message": "Invalid input",
|
|
8
|
+
"ref": undefined,
|
|
9
|
+
"type": "invalid_union",
|
|
10
|
+
},
|
|
11
|
+
"confirm": Object {
|
|
12
|
+
"message": "Passwords don't match",
|
|
13
|
+
"ref": undefined,
|
|
14
|
+
"type": "custom_error",
|
|
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": Object {
|
|
29
|
+
"0": 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
|
+
"message": "Invalid input",
|
|
42
|
+
"ref": undefined,
|
|
43
|
+
"type": "invalid_union",
|
|
44
|
+
},
|
|
45
|
+
"password": Object {
|
|
46
|
+
"message": "Invalid",
|
|
47
|
+
"ref": Object {
|
|
48
|
+
"name": "password",
|
|
49
|
+
},
|
|
50
|
+
"type": "invalid_string",
|
|
51
|
+
},
|
|
52
|
+
"repeatPassword": Object {
|
|
53
|
+
"message": "Required",
|
|
54
|
+
"ref": undefined,
|
|
55
|
+
"type": "invalid_type",
|
|
56
|
+
},
|
|
57
|
+
"tags": Object {
|
|
58
|
+
"message": "Required",
|
|
59
|
+
"ref": undefined,
|
|
60
|
+
"type": "invalid_type",
|
|
61
|
+
},
|
|
62
|
+
"username": Object {
|
|
63
|
+
"message": "Required",
|
|
64
|
+
"ref": Object {
|
|
65
|
+
"name": "username",
|
|
66
|
+
},
|
|
67
|
+
"type": "invalid_type",
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
"values": Object {},
|
|
71
|
+
}
|
|
72
|
+
`;
|
|
73
|
+
|
|
74
|
+
exports[`zodResolver should return a single error from zodResolver with \`mode: sync\` when validation fails 1`] = `
|
|
75
|
+
Object {
|
|
76
|
+
"errors": Object {
|
|
77
|
+
"birthYear": Object {
|
|
78
|
+
"message": "Invalid input",
|
|
79
|
+
"ref": undefined,
|
|
80
|
+
"type": "invalid_union",
|
|
81
|
+
},
|
|
82
|
+
"confirm": Object {
|
|
83
|
+
"message": "Passwords don't match",
|
|
84
|
+
"ref": undefined,
|
|
85
|
+
"type": "custom_error",
|
|
86
|
+
},
|
|
87
|
+
"email": Object {
|
|
88
|
+
"message": "Invalid email",
|
|
89
|
+
"ref": Object {
|
|
90
|
+
"name": "email",
|
|
91
|
+
},
|
|
92
|
+
"type": "invalid_string",
|
|
93
|
+
},
|
|
94
|
+
"enabled": Object {
|
|
95
|
+
"message": "Required",
|
|
96
|
+
"ref": undefined,
|
|
97
|
+
"type": "invalid_type",
|
|
98
|
+
},
|
|
99
|
+
"like": Object {
|
|
100
|
+
"0": Object {
|
|
101
|
+
"id": Object {
|
|
102
|
+
"message": "Expected number, received string",
|
|
103
|
+
"ref": undefined,
|
|
104
|
+
"type": "invalid_type",
|
|
105
|
+
},
|
|
106
|
+
"name": Object {
|
|
107
|
+
"message": "Required",
|
|
108
|
+
"ref": undefined,
|
|
109
|
+
"type": "invalid_type",
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
"message": "Invalid input",
|
|
113
|
+
"ref": undefined,
|
|
114
|
+
"type": "invalid_union",
|
|
115
|
+
},
|
|
116
|
+
"password": Object {
|
|
117
|
+
"message": "Invalid",
|
|
118
|
+
"ref": Object {
|
|
119
|
+
"name": "password",
|
|
120
|
+
},
|
|
121
|
+
"type": "invalid_string",
|
|
122
|
+
},
|
|
123
|
+
"repeatPassword": Object {
|
|
124
|
+
"message": "Required",
|
|
125
|
+
"ref": undefined,
|
|
126
|
+
"type": "invalid_type",
|
|
127
|
+
},
|
|
128
|
+
"tags": Object {
|
|
129
|
+
"message": "Required",
|
|
130
|
+
"ref": undefined,
|
|
131
|
+
"type": "invalid_type",
|
|
132
|
+
},
|
|
133
|
+
"username": Object {
|
|
134
|
+
"message": "Required",
|
|
135
|
+
"ref": Object {
|
|
136
|
+
"name": "username",
|
|
137
|
+
},
|
|
138
|
+
"type": "invalid_type",
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
"values": Object {},
|
|
142
|
+
}
|
|
143
|
+
`;
|
|
144
|
+
|
|
145
|
+
exports[`zodResolver should return all the errors from zodResolver when validation fails with \`validateAllFieldCriteria\` set to true 1`] = `
|
|
146
|
+
Object {
|
|
147
|
+
"errors": Object {
|
|
148
|
+
"birthYear": Object {
|
|
149
|
+
"message": "Invalid input",
|
|
150
|
+
"ref": undefined,
|
|
151
|
+
"type": "invalid_union",
|
|
152
|
+
"types": Object {
|
|
153
|
+
"invalid_type": "Expected undefined, received string",
|
|
154
|
+
"invalid_union": "Invalid input",
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
"confirm": Object {
|
|
158
|
+
"message": "Passwords don't match",
|
|
159
|
+
"ref": undefined,
|
|
160
|
+
"type": "custom_error",
|
|
161
|
+
"types": Object {
|
|
162
|
+
"custom_error": "Passwords don't match",
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
"email": Object {
|
|
166
|
+
"message": "Invalid email",
|
|
167
|
+
"ref": Object {
|
|
168
|
+
"name": "email",
|
|
169
|
+
},
|
|
170
|
+
"type": "invalid_string",
|
|
171
|
+
"types": Object {
|
|
172
|
+
"invalid_string": "Invalid email",
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
"enabled": Object {
|
|
176
|
+
"message": "Required",
|
|
177
|
+
"ref": undefined,
|
|
178
|
+
"type": "invalid_type",
|
|
179
|
+
"types": Object {
|
|
180
|
+
"invalid_type": "Required",
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
"like": Object {
|
|
184
|
+
"0": Object {
|
|
185
|
+
"id": Object {
|
|
186
|
+
"message": "Expected number, received string",
|
|
187
|
+
"ref": undefined,
|
|
188
|
+
"type": "invalid_type",
|
|
189
|
+
"types": Object {
|
|
190
|
+
"invalid_type": "Expected number, received string",
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
"name": Object {
|
|
194
|
+
"message": "Required",
|
|
195
|
+
"ref": undefined,
|
|
196
|
+
"type": "invalid_type",
|
|
197
|
+
"types": Object {
|
|
198
|
+
"invalid_type": "Required",
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
"message": "Invalid input",
|
|
203
|
+
"ref": undefined,
|
|
204
|
+
"type": "invalid_union",
|
|
205
|
+
"types": Object {
|
|
206
|
+
"invalid_type": "Expected undefined, received array",
|
|
207
|
+
"invalid_union": "Invalid input",
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
"password": Object {
|
|
211
|
+
"message": "Invalid",
|
|
212
|
+
"ref": Object {
|
|
213
|
+
"name": "password",
|
|
214
|
+
},
|
|
215
|
+
"type": "invalid_string",
|
|
216
|
+
"types": Object {
|
|
217
|
+
"invalid_string": "Invalid",
|
|
218
|
+
},
|
|
219
|
+
},
|
|
220
|
+
"repeatPassword": Object {
|
|
221
|
+
"message": "Required",
|
|
222
|
+
"ref": undefined,
|
|
223
|
+
"type": "invalid_type",
|
|
224
|
+
"types": Object {
|
|
225
|
+
"invalid_type": "Required",
|
|
226
|
+
},
|
|
227
|
+
},
|
|
228
|
+
"tags": Object {
|
|
229
|
+
"message": "Required",
|
|
230
|
+
"ref": undefined,
|
|
231
|
+
"type": "invalid_type",
|
|
232
|
+
"types": Object {
|
|
233
|
+
"invalid_type": "Required",
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
"username": Object {
|
|
237
|
+
"message": "Required",
|
|
238
|
+
"ref": Object {
|
|
239
|
+
"name": "username",
|
|
240
|
+
},
|
|
241
|
+
"type": "invalid_type",
|
|
242
|
+
"types": Object {
|
|
243
|
+
"invalid_type": "Required",
|
|
244
|
+
},
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
"values": Object {},
|
|
248
|
+
}
|
|
249
|
+
`;
|
|
250
|
+
|
|
251
|
+
exports[`zodResolver should return all the errors from zodResolver when validation fails with \`validateAllFieldCriteria\` set to true and \`mode: sync\` 1`] = `
|
|
252
|
+
Object {
|
|
253
|
+
"errors": Object {
|
|
254
|
+
"birthYear": Object {
|
|
255
|
+
"message": "Invalid input",
|
|
256
|
+
"ref": undefined,
|
|
257
|
+
"type": "invalid_union",
|
|
258
|
+
"types": Object {
|
|
259
|
+
"invalid_type": "Expected undefined, received string",
|
|
260
|
+
"invalid_union": "Invalid input",
|
|
261
|
+
},
|
|
262
|
+
},
|
|
263
|
+
"confirm": Object {
|
|
264
|
+
"message": "Passwords don't match",
|
|
265
|
+
"ref": undefined,
|
|
266
|
+
"type": "custom_error",
|
|
267
|
+
"types": Object {
|
|
268
|
+
"custom_error": "Passwords don't match",
|
|
269
|
+
},
|
|
270
|
+
},
|
|
271
|
+
"email": Object {
|
|
272
|
+
"message": "Invalid email",
|
|
273
|
+
"ref": Object {
|
|
274
|
+
"name": "email",
|
|
275
|
+
},
|
|
276
|
+
"type": "invalid_string",
|
|
277
|
+
"types": Object {
|
|
278
|
+
"invalid_string": "Invalid email",
|
|
279
|
+
},
|
|
280
|
+
},
|
|
281
|
+
"enabled": Object {
|
|
282
|
+
"message": "Required",
|
|
283
|
+
"ref": undefined,
|
|
284
|
+
"type": "invalid_type",
|
|
285
|
+
"types": Object {
|
|
286
|
+
"invalid_type": "Required",
|
|
287
|
+
},
|
|
288
|
+
},
|
|
289
|
+
"like": Object {
|
|
290
|
+
"0": Object {
|
|
291
|
+
"id": Object {
|
|
292
|
+
"message": "Expected number, received string",
|
|
293
|
+
"ref": undefined,
|
|
294
|
+
"type": "invalid_type",
|
|
295
|
+
"types": Object {
|
|
296
|
+
"invalid_type": "Expected number, received string",
|
|
297
|
+
},
|
|
298
|
+
},
|
|
299
|
+
"name": Object {
|
|
300
|
+
"message": "Required",
|
|
301
|
+
"ref": undefined,
|
|
302
|
+
"type": "invalid_type",
|
|
303
|
+
"types": Object {
|
|
304
|
+
"invalid_type": "Required",
|
|
305
|
+
},
|
|
306
|
+
},
|
|
307
|
+
},
|
|
308
|
+
"message": "Invalid input",
|
|
309
|
+
"ref": undefined,
|
|
310
|
+
"type": "invalid_union",
|
|
311
|
+
"types": Object {
|
|
312
|
+
"invalid_type": "Expected undefined, received array",
|
|
313
|
+
"invalid_union": "Invalid input",
|
|
314
|
+
},
|
|
315
|
+
},
|
|
316
|
+
"password": Object {
|
|
317
|
+
"message": "Invalid",
|
|
318
|
+
"ref": Object {
|
|
319
|
+
"name": "password",
|
|
320
|
+
},
|
|
321
|
+
"type": "invalid_string",
|
|
322
|
+
"types": Object {
|
|
323
|
+
"invalid_string": "Invalid",
|
|
324
|
+
},
|
|
325
|
+
},
|
|
326
|
+
"repeatPassword": Object {
|
|
327
|
+
"message": "Required",
|
|
328
|
+
"ref": undefined,
|
|
329
|
+
"type": "invalid_type",
|
|
330
|
+
"types": Object {
|
|
331
|
+
"invalid_type": "Required",
|
|
332
|
+
},
|
|
333
|
+
},
|
|
334
|
+
"tags": Object {
|
|
335
|
+
"message": "Required",
|
|
336
|
+
"ref": undefined,
|
|
337
|
+
"type": "invalid_type",
|
|
338
|
+
"types": Object {
|
|
339
|
+
"invalid_type": "Required",
|
|
340
|
+
},
|
|
341
|
+
},
|
|
342
|
+
"username": Object {
|
|
343
|
+
"message": "Required",
|
|
344
|
+
"ref": Object {
|
|
345
|
+
"name": "username",
|
|
346
|
+
},
|
|
347
|
+
"type": "invalid_type",
|
|
348
|
+
"types": Object {
|
|
349
|
+
"invalid_type": "Required",
|
|
350
|
+
},
|
|
351
|
+
},
|
|
352
|
+
},
|
|
353
|
+
"values": Object {},
|
|
354
|
+
}
|
|
355
|
+
`;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { zodResolver } from '..';
|
|
2
|
+
import { schema, validData, invalidData, fields } from './__fixtures__/data';
|
|
3
|
+
|
|
4
|
+
describe('zodResolver', () => {
|
|
5
|
+
it('should return values from zodResolver when validation pass', async () => {
|
|
6
|
+
const parseAsyncSpy = jest.spyOn(schema, 'parseAsync');
|
|
7
|
+
|
|
8
|
+
const result = await zodResolver(schema)(validData, undefined, {
|
|
9
|
+
fields,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
expect(parseAsyncSpy).toHaveBeenCalledTimes(1);
|
|
13
|
+
expect(result).toEqual({ errors: {}, values: validData });
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('should return values from zodResolver with `mode: sync` when validation pass', async () => {
|
|
17
|
+
const parseSpy = jest.spyOn(schema, 'parse');
|
|
18
|
+
const parseAsyncSpy = jest.spyOn(schema, 'parseAsync');
|
|
19
|
+
|
|
20
|
+
const result = await zodResolver(schema, undefined, {
|
|
21
|
+
mode: 'sync',
|
|
22
|
+
})(validData, undefined, { fields });
|
|
23
|
+
|
|
24
|
+
expect(parseSpy).toHaveBeenCalledTimes(1);
|
|
25
|
+
expect(parseAsyncSpy).not.toHaveBeenCalled();
|
|
26
|
+
expect(result).toEqual({ errors: {}, values: validData });
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should return a single error from zodResolver when validation fails', async () => {
|
|
30
|
+
const result = await zodResolver(schema)(invalidData, undefined, {
|
|
31
|
+
fields,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
expect(result).toMatchSnapshot();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('should return a single error from zodResolver with `mode: sync` when validation fails', async () => {
|
|
38
|
+
const parseSpy = jest.spyOn(schema, 'parse');
|
|
39
|
+
const parseAsyncSpy = jest.spyOn(schema, 'parseAsync');
|
|
40
|
+
|
|
41
|
+
const result = await zodResolver(schema, undefined, {
|
|
42
|
+
mode: 'sync',
|
|
43
|
+
})(invalidData, undefined, { fields });
|
|
44
|
+
|
|
45
|
+
expect(parseSpy).toHaveBeenCalledTimes(1);
|
|
46
|
+
expect(parseAsyncSpy).not.toHaveBeenCalled();
|
|
47
|
+
expect(result).toMatchSnapshot();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('should return all the errors from zodResolver when validation fails with `validateAllFieldCriteria` set to true', async () => {
|
|
51
|
+
const result = await zodResolver(schema)(invalidData, undefined, {
|
|
52
|
+
fields,
|
|
53
|
+
criteriaMode: 'all',
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
expect(result).toMatchSnapshot();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('should return all the errors from zodResolver when validation fails with `validateAllFieldCriteria` set to true and `mode: sync`', async () => {
|
|
60
|
+
const result = await zodResolver(schema, undefined, { mode: 'sync' })(
|
|
61
|
+
invalidData,
|
|
62
|
+
undefined,
|
|
63
|
+
{
|
|
64
|
+
fields,
|
|
65
|
+
criteriaMode: 'all',
|
|
66
|
+
},
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
expect(result).toMatchSnapshot();
|
|
70
|
+
});
|
|
71
|
+
});
|
package/zod/src/index.ts
ADDED
package/zod/src/types.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FieldValues,
|
|
3
|
+
ResolverResult,
|
|
4
|
+
UnpackNestedValue,
|
|
5
|
+
ResolverOptions,
|
|
6
|
+
} from 'react-hook-form';
|
|
7
|
+
import * as z from 'zod';
|
|
8
|
+
import type { ParseParams } from 'zod/lib/src/parser';
|
|
9
|
+
|
|
10
|
+
export type Resolver = <T extends z.ZodSchema<any, any>>(
|
|
11
|
+
schema: T,
|
|
12
|
+
schemaOptions?: ParseParams,
|
|
13
|
+
factoryOptions?: { mode?: 'async' | 'sync' },
|
|
14
|
+
) => <TFieldValues extends FieldValues, TContext>(
|
|
15
|
+
values: UnpackNestedValue<TFieldValues>,
|
|
16
|
+
context: TContext | undefined,
|
|
17
|
+
options: ResolverOptions<TFieldValues>,
|
|
18
|
+
) => Promise<ResolverResult<TFieldValues>>;
|
package/zod/src/zod.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { appendErrors, FieldError } from 'react-hook-form';
|
|
2
|
+
import * as z from 'zod';
|
|
3
|
+
import { toNestError } from '@hookform/resolvers';
|
|
4
|
+
import type { Resolver } from './types';
|
|
5
|
+
|
|
6
|
+
const parseErrorSchema = (
|
|
7
|
+
zodErrors: z.ZodSuberror[],
|
|
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
|
+
errors[_path] = { message, type: code };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if ('unionErrors' in error) {
|
|
21
|
+
error.unionErrors.forEach((unionError) =>
|
|
22
|
+
unionError.errors.forEach((e) => zodErrors.push(e)),
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (validateAllFieldCriteria) {
|
|
27
|
+
errors[_path] = appendErrors(
|
|
28
|
+
_path,
|
|
29
|
+
validateAllFieldCriteria,
|
|
30
|
+
errors,
|
|
31
|
+
code,
|
|
32
|
+
message,
|
|
33
|
+
) as FieldError;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
zodErrors.shift();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return errors;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const zodResolver: Resolver = (
|
|
43
|
+
schema,
|
|
44
|
+
schemaOptions,
|
|
45
|
+
resolverOptions = {},
|
|
46
|
+
) => async (values, _, options) => {
|
|
47
|
+
try {
|
|
48
|
+
return {
|
|
49
|
+
errors: {},
|
|
50
|
+
values: await schema[
|
|
51
|
+
resolverOptions.mode === 'sync' ? 'parse' : 'parseAsync'
|
|
52
|
+
](values, schemaOptions),
|
|
53
|
+
};
|
|
54
|
+
} catch (error) {
|
|
55
|
+
return {
|
|
56
|
+
values: {},
|
|
57
|
+
errors: error.isEmpty
|
|
58
|
+
? {}
|
|
59
|
+
: toNestError(
|
|
60
|
+
parseErrorSchema(error.errors, options.criteriaMode === 'all'),
|
|
61
|
+
options.fields,
|
|
62
|
+
),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
};
|
package/dist/ie11/joi.d.ts
DELETED
package/dist/ie11/joi.js
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (_) try {
|
|
29
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
-
exports.joiResolver = void 0;
|
|
51
|
-
var react_hook_form_1 = require("react-hook-form");
|
|
52
|
-
var convertArrayToPathName_1 = require("./utils/convertArrayToPathName");
|
|
53
|
-
var parseErrorSchema = function (error, validateAllFieldCriteria) {
|
|
54
|
-
return Array.isArray(error.details)
|
|
55
|
-
? error.details.reduce(function (previous, _a) {
|
|
56
|
-
var _b, _c, _d;
|
|
57
|
-
var path = _a.path, _e = _a.message, message = _e === void 0 ? '' : _e, type = _a.type;
|
|
58
|
-
var currentPath = convertArrayToPathName_1.default(path);
|
|
59
|
-
return __assign(__assign({}, previous), (path
|
|
60
|
-
? previous[currentPath] && validateAllFieldCriteria
|
|
61
|
-
? (_b = {},
|
|
62
|
-
_b[currentPath] = react_hook_form_1.appendErrors(currentPath, validateAllFieldCriteria, previous, type, message),
|
|
63
|
-
_b) : (_c = {},
|
|
64
|
-
_c[currentPath] = previous[currentPath] || __assign({ message: message,
|
|
65
|
-
type: type }, (validateAllFieldCriteria
|
|
66
|
-
? {
|
|
67
|
-
types: (_d = {}, _d[type] = message || true, _d),
|
|
68
|
-
}
|
|
69
|
-
: {})),
|
|
70
|
-
_c)
|
|
71
|
-
: {}));
|
|
72
|
-
}, {})
|
|
73
|
-
: [];
|
|
74
|
-
};
|
|
75
|
-
exports.joiResolver = function (schema, options) {
|
|
76
|
-
if (options === void 0) { options = {
|
|
77
|
-
abortEarly: false,
|
|
78
|
-
}; }
|
|
79
|
-
return function (values, _, validateAllFieldCriteria) {
|
|
80
|
-
if (validateAllFieldCriteria === void 0) { validateAllFieldCriteria = false; }
|
|
81
|
-
return __awaiter(void 0, void 0, void 0, function () {
|
|
82
|
-
var _a, e_1;
|
|
83
|
-
return __generator(this, function (_b) {
|
|
84
|
-
switch (_b.label) {
|
|
85
|
-
case 0:
|
|
86
|
-
_b.trys.push([0, 2, , 3]);
|
|
87
|
-
_a = {};
|
|
88
|
-
return [4 /*yield*/, schema.validateAsync(values, __assign({}, options))];
|
|
89
|
-
case 1: return [2 /*return*/, (_a.values = _b.sent(),
|
|
90
|
-
_a.errors = {},
|
|
91
|
-
_a)];
|
|
92
|
-
case 2:
|
|
93
|
-
e_1 = _b.sent();
|
|
94
|
-
return [2 /*return*/, {
|
|
95
|
-
values: {},
|
|
96
|
-
errors: react_hook_form_1.transformToNestObject(parseErrorSchema(e_1, validateAllFieldCriteria)),
|
|
97
|
-
}];
|
|
98
|
-
case 3: return [2 /*return*/];
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
});
|
|
102
|
-
};
|
|
103
|
-
};
|
|
104
|
-
//# sourceMappingURL=joi.js.map
|
package/dist/ie11/joi.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"joi.js","sourceRoot":"","sources":["../../src/joi.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mDAKyB;AAEzB,yEAAoE;AAEpE,IAAM,gBAAgB,GAAG,UACvB,KAA0B,EAC1B,wBAAiC;IAEjC,OAAA,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC;QAC1B,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAClB,UAAC,QAA6B,EAAE,EAA4B;;gBAA1B,IAAI,UAAA,EAAE,eAAY,EAAZ,OAAO,mBAAG,EAAE,KAAA,EAAE,IAAI,UAAA;YACxD,IAAM,WAAW,GAAG,gCAAsB,CAAC,IAAI,CAAC,CAAC;YAEjD,6BACK,QAAQ,GACR,CAAC,IAAI;gBACN,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,IAAI,wBAAwB;oBACjD,CAAC;wBACG,GAAC,WAAW,IAAG,8BAAY,CACzB,WAAW,EACX,wBAAwB,EACxB,QAAQ,EACR,IAAI,EACJ,OAAO,CACR;4BAEL,CAAC;oBACG,GAAC,WAAW,IAAG,QAAQ,CAAC,WAAW,CAAC,eAClC,OAAO,SAAA;wBACP,IAAI,MAAA,IACD,CAAC,wBAAwB;wBAC1B,CAAC,CAAC;4BACE,KAAK,YAAI,GAAC,IAAI,IAAG,OAAO,IAAI,IAAI,KAAE;yBACnC;wBACH,CAAC,CAAC,EAAE,CAAC,CACR;uBACF;gBACL,CAAC,CAAC,EAAE,CAAC,EACP;QACJ,CAAC,EACD,EAAE,CACH;QACH,CAAC,CAAC,EAAE;AAlCN,CAkCM,CAAC;AAEI,QAAA,WAAW,GAAG,UACzB,MAAkB,EAClB,OAEC;IAFD,wBAAA,EAAA;QACE,UAAU,EAAE,KAAK;KAClB;IAC0B,OAAA,UAC3B,MAAM,EACN,CAAC,EACD,wBAAgC;QAAhC,yCAAA,EAAA,gCAAgC;;;;;;;;wBAIpB,qBAAM,MAAM,CAAC,aAAa,CAAC,MAAM,eACpC,OAAO,EACV,EAAA;4BAHJ,uBACE,SAAM,GAAE,SAEN;4BACF,SAAM,GAAE,EAAE;iCACV;;;wBAEF,sBAAO;gCACL,MAAM,EAAE,EAAE;gCACV,MAAM,EAAE,uCAAqB,CAC3B,gBAAgB,CAAC,GAAC,EAAE,wBAAwB,CAAC,CAC9C;6BACF,EAAC;;;;;KAEL;AApB4B,CAoB5B,CAAC"}
|