@hookform/resolvers 2.0.0-beta.7 → 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/README.md +19 -10
- package/dist/index.d.ts +1 -1
- package/dist/resolvers.js +1 -1
- package/dist/resolvers.js.map +1 -1
- package/dist/resolvers.mjs +1 -1
- package/dist/resolvers.modern.js +1 -1
- package/dist/resolvers.modern.js.map +1 -1
- package/dist/resolvers.module.js +1 -1
- package/dist/resolvers.module.js.map +1 -1
- package/dist/resolvers.umd.js +1 -1
- package/dist/resolvers.umd.js.map +1 -1
- package/dist/toNestError.d.ts +2 -0
- package/joi/dist/joi.js +1 -1
- package/joi/dist/joi.js.map +1 -1
- package/joi/dist/joi.mjs +1 -1
- package/joi/dist/joi.modern.js +1 -1
- package/joi/dist/joi.modern.js.map +1 -1
- package/joi/dist/joi.module.js +1 -1
- package/joi/dist/joi.module.js.map +1 -1
- package/joi/dist/joi.umd.js +1 -1
- package/joi/dist/joi.umd.js.map +1 -1
- package/joi/dist/types.d.ts +1 -1
- package/joi/src/__tests__/__fixtures__/data.ts +76 -0
- package/joi/src/__tests__/__snapshots__/joi.ts.snap +118 -0
- package/joi/src/__tests__/joi.ts +35 -88
- package/joi/src/joi.ts +47 -64
- package/joi/src/types.ts +1 -1
- package/package.json +14 -15
- package/superstruct/dist/superstruct.js +1 -1
- package/superstruct/dist/superstruct.js.map +1 -1
- package/superstruct/dist/superstruct.mjs +1 -1
- package/superstruct/dist/superstruct.modern.js +1 -1
- package/superstruct/dist/superstruct.modern.js.map +1 -1
- package/superstruct/dist/superstruct.module.js +1 -1
- package/superstruct/dist/superstruct.module.js.map +1 -1
- package/superstruct/dist/superstruct.umd.js +1 -1
- package/superstruct/dist/superstruct.umd.js.map +1 -1
- package/superstruct/dist/types.d.ts +1 -1
- package/superstruct/package.json +2 -1
- package/superstruct/src/__tests__/__fixtures__/data.ts +73 -0
- package/superstruct/src/__tests__/__snapshots__/superstruct.ts.snap +26 -56
- package/superstruct/src/__tests__/superstruct.ts +6 -65
- package/superstruct/src/superstruct.ts +20 -53
- package/superstruct/src/types.ts +1 -1
- package/vest/dist/vest.js +1 -1
- package/vest/dist/vest.js.map +1 -1
- package/vest/dist/vest.mjs +1 -1
- package/vest/dist/vest.modern.js +1 -1
- package/vest/dist/vest.modern.js.map +1 -1
- package/vest/dist/vest.module.js +1 -1
- package/vest/dist/vest.module.js.map +1 -1
- package/vest/dist/vest.umd.js +1 -1
- package/vest/dist/vest.umd.js.map +1 -1
- package/vest/package.json +3 -1
- package/vest/src/__tests__/__fixtures__/data.ts +67 -0
- package/vest/src/__tests__/__snapshots__/vest.ts.snap +28 -0
- package/vest/src/__tests__/vest.ts +22 -95
- package/vest/src/types.ts +1 -1
- package/vest/src/vest.ts +31 -42
- package/yup/dist/types.d.ts +4 -3
- package/yup/dist/yup.js +1 -1
- package/yup/dist/yup.js.map +1 -1
- package/yup/dist/yup.mjs +1 -1
- package/yup/dist/yup.modern.js +1 -1
- package/yup/dist/yup.modern.js.map +1 -1
- package/yup/dist/yup.module.js +1 -1
- package/yup/dist/yup.module.js.map +1 -1
- package/yup/dist/yup.umd.js +1 -1
- package/yup/dist/yup.umd.js.map +1 -1
- package/yup/package.json +2 -1
- package/yup/src/__tests__/__fixtures__/data.ts +65 -0
- package/yup/src/__tests__/__snapshots__/yup.ts.snap +99 -13
- package/yup/src/__tests__/yup.ts +23 -97
- package/yup/src/types.ts +6 -3
- package/yup/src/yup.ts +30 -60
- package/zod/dist/types.d.ts +1 -1
- package/zod/dist/zod.js +1 -1
- package/zod/dist/zod.js.map +1 -1
- package/zod/dist/zod.mjs +1 -1
- package/zod/dist/zod.modern.js +1 -1
- package/zod/dist/zod.modern.js.map +1 -1
- package/zod/dist/zod.module.js +1 -1
- package/zod/dist/zod.module.js.map +1 -1
- package/zod/dist/zod.umd.js +1 -1
- package/zod/dist/zod.umd.js.map +1 -1
- package/zod/src/__tests__/__fixtures__/data.ts +68 -0
- package/zod/src/__tests__/__snapshots__/zod.ts.snap +146 -0
- package/zod/src/__tests__/zod.ts +19 -79
- package/zod/src/types.ts +1 -1
- package/zod/src/zod.ts +45 -50
- package/dist/convertArrayToPathName.d.ts +0 -1
|
@@ -5,14 +5,35 @@ Object {
|
|
|
5
5
|
"errors": Object {
|
|
6
6
|
"birthYear": Object {
|
|
7
7
|
"message": "birthYear must be a \`number\` type, but the final value was: \`NaN\` (cast from the value \`\\"birthYear\\"\`).",
|
|
8
|
+
"ref": undefined,
|
|
8
9
|
"type": "typeError",
|
|
9
10
|
},
|
|
11
|
+
"like": Array [
|
|
12
|
+
Object {
|
|
13
|
+
"id": Object {
|
|
14
|
+
"message": "like[0].id must be a \`number\` type, but the final value was: \`NaN\` (cast from the value \`\\"z\\"\`).",
|
|
15
|
+
"ref": undefined,
|
|
16
|
+
"type": "typeError",
|
|
17
|
+
},
|
|
18
|
+
"name": Object {
|
|
19
|
+
"message": "like[0].name is a required field",
|
|
20
|
+
"ref": undefined,
|
|
21
|
+
"type": "required",
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
],
|
|
10
25
|
"password": Object {
|
|
11
26
|
"message": "password must match the following: \\"/^[a-zA-Z0-9]{3,30}/\\"",
|
|
27
|
+
"ref": Object {
|
|
28
|
+
"name": "password",
|
|
29
|
+
},
|
|
12
30
|
"type": "matches",
|
|
13
31
|
},
|
|
14
32
|
"username": Object {
|
|
15
33
|
"message": "username is a required field",
|
|
34
|
+
"ref": Object {
|
|
35
|
+
"name": "username",
|
|
36
|
+
},
|
|
16
37
|
"type": "required",
|
|
17
38
|
},
|
|
18
39
|
},
|
|
@@ -25,14 +46,35 @@ Object {
|
|
|
25
46
|
"errors": Object {
|
|
26
47
|
"birthYear": Object {
|
|
27
48
|
"message": "birthYear must be a \`number\` type, but the final value was: \`NaN\` (cast from the value \`\\"birthYear\\"\`).",
|
|
49
|
+
"ref": undefined,
|
|
28
50
|
"type": "typeError",
|
|
29
51
|
},
|
|
52
|
+
"like": Array [
|
|
53
|
+
Object {
|
|
54
|
+
"id": Object {
|
|
55
|
+
"message": "like[0].id must be a \`number\` type, but the final value was: \`NaN\` (cast from the value \`\\"z\\"\`).",
|
|
56
|
+
"ref": undefined,
|
|
57
|
+
"type": "typeError",
|
|
58
|
+
},
|
|
59
|
+
"name": Object {
|
|
60
|
+
"message": "like[0].name is a required field",
|
|
61
|
+
"ref": undefined,
|
|
62
|
+
"type": "required",
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
],
|
|
30
66
|
"password": Object {
|
|
31
67
|
"message": "password must match the following: \\"/^[a-zA-Z0-9]{3,30}/\\"",
|
|
68
|
+
"ref": Object {
|
|
69
|
+
"name": "password",
|
|
70
|
+
},
|
|
32
71
|
"type": "matches",
|
|
33
72
|
},
|
|
34
73
|
"username": Object {
|
|
35
74
|
"message": "username is a required field",
|
|
75
|
+
"ref": Object {
|
|
76
|
+
"name": "username",
|
|
77
|
+
},
|
|
36
78
|
"type": "required",
|
|
37
79
|
},
|
|
38
80
|
},
|
|
@@ -45,13 +87,37 @@ Object {
|
|
|
45
87
|
"errors": Object {
|
|
46
88
|
"birthYear": Object {
|
|
47
89
|
"message": "birthYear must be a \`number\` type, but the final value was: \`NaN\` (cast from the value \`\\"birthYear\\"\`).",
|
|
90
|
+
"ref": undefined,
|
|
48
91
|
"type": "typeError",
|
|
49
92
|
"types": Object {
|
|
50
93
|
"typeError": "birthYear must be a \`number\` type, but the final value was: \`NaN\` (cast from the value \`\\"birthYear\\"\`).",
|
|
51
94
|
},
|
|
52
95
|
},
|
|
96
|
+
"like": Array [
|
|
97
|
+
Object {
|
|
98
|
+
"id": Object {
|
|
99
|
+
"message": "like[0].id must be a \`number\` type, but the final value was: \`NaN\` (cast from the value \`\\"z\\"\`).",
|
|
100
|
+
"ref": undefined,
|
|
101
|
+
"type": "typeError",
|
|
102
|
+
"types": Object {
|
|
103
|
+
"typeError": "like[0].id must be a \`number\` type, but the final value was: \`NaN\` (cast from the value \`\\"z\\"\`).",
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
"name": Object {
|
|
107
|
+
"message": "like[0].name is a required field",
|
|
108
|
+
"ref": undefined,
|
|
109
|
+
"type": "required",
|
|
110
|
+
"types": Object {
|
|
111
|
+
"required": "like[0].name is a required field",
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
],
|
|
53
116
|
"password": Object {
|
|
54
117
|
"message": "password must match the following: \\"/^[a-zA-Z0-9]{3,30}/\\"",
|
|
118
|
+
"ref": Object {
|
|
119
|
+
"name": "password",
|
|
120
|
+
},
|
|
55
121
|
"type": "matches",
|
|
56
122
|
"types": Object {
|
|
57
123
|
"matches": "password must match the following: \\"/^[a-zA-Z0-9]{3,30}/\\"",
|
|
@@ -59,6 +125,9 @@ Object {
|
|
|
59
125
|
},
|
|
60
126
|
"username": Object {
|
|
61
127
|
"message": "username is a required field",
|
|
128
|
+
"ref": Object {
|
|
129
|
+
"name": "username",
|
|
130
|
+
},
|
|
62
131
|
"type": "required",
|
|
63
132
|
"types": Object {
|
|
64
133
|
"required": "username is a required field",
|
|
@@ -74,13 +143,37 @@ Object {
|
|
|
74
143
|
"errors": Object {
|
|
75
144
|
"birthYear": Object {
|
|
76
145
|
"message": "birthYear must be a \`number\` type, but the final value was: \`NaN\` (cast from the value \`\\"birthYear\\"\`).",
|
|
146
|
+
"ref": undefined,
|
|
77
147
|
"type": "typeError",
|
|
78
148
|
"types": Object {
|
|
79
149
|
"typeError": "birthYear must be a \`number\` type, but the final value was: \`NaN\` (cast from the value \`\\"birthYear\\"\`).",
|
|
80
150
|
},
|
|
81
151
|
},
|
|
152
|
+
"like": Array [
|
|
153
|
+
Object {
|
|
154
|
+
"id": Object {
|
|
155
|
+
"message": "like[0].id must be a \`number\` type, but the final value was: \`NaN\` (cast from the value \`\\"z\\"\`).",
|
|
156
|
+
"ref": undefined,
|
|
157
|
+
"type": "typeError",
|
|
158
|
+
"types": Object {
|
|
159
|
+
"typeError": "like[0].id must be a \`number\` type, but the final value was: \`NaN\` (cast from the value \`\\"z\\"\`).",
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
"name": Object {
|
|
163
|
+
"message": "like[0].name is a required field",
|
|
164
|
+
"ref": undefined,
|
|
165
|
+
"type": "required",
|
|
166
|
+
"types": Object {
|
|
167
|
+
"required": "like[0].name is a required field",
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
],
|
|
82
172
|
"password": Object {
|
|
83
173
|
"message": "password must match the following: \\"/^[a-zA-Z0-9]{3,30}/\\"",
|
|
174
|
+
"ref": Object {
|
|
175
|
+
"name": "password",
|
|
176
|
+
},
|
|
84
177
|
"type": "matches",
|
|
85
178
|
"types": Object {
|
|
86
179
|
"matches": "password must match the following: \\"/^[a-zA-Z0-9]{3,30}/\\"",
|
|
@@ -88,6 +181,9 @@ Object {
|
|
|
88
181
|
},
|
|
89
182
|
"username": Object {
|
|
90
183
|
"message": "username is a required field",
|
|
184
|
+
"ref": Object {
|
|
185
|
+
"name": "username",
|
|
186
|
+
},
|
|
91
187
|
"type": "required",
|
|
92
188
|
"types": Object {
|
|
93
189
|
"required": "username is a required field",
|
|
@@ -103,6 +199,7 @@ Object {
|
|
|
103
199
|
"errors": Object {
|
|
104
200
|
"name": Object {
|
|
105
201
|
"message": "name must be at least 6 characters",
|
|
202
|
+
"ref": undefined,
|
|
106
203
|
"type": "min",
|
|
107
204
|
},
|
|
108
205
|
},
|
|
@@ -110,23 +207,12 @@ Object {
|
|
|
110
207
|
}
|
|
111
208
|
`;
|
|
112
209
|
|
|
113
|
-
exports[`yupResolver should return an error result if inner yup validation error has no path 1`] = `
|
|
114
|
-
Object {
|
|
115
|
-
"errors": Object {
|
|
116
|
-
"required": Object {
|
|
117
|
-
"message": "error1",
|
|
118
|
-
"type": "required",
|
|
119
|
-
},
|
|
120
|
-
},
|
|
121
|
-
"values": Object {},
|
|
122
|
-
}
|
|
123
|
-
`;
|
|
124
|
-
|
|
125
210
|
exports[`yupResolver should return correct error message with using yup.test 1`] = `
|
|
126
211
|
Object {
|
|
127
212
|
"errors": Object {
|
|
128
|
-
"
|
|
213
|
+
"": Object {
|
|
129
214
|
"message": "Email or name are required",
|
|
215
|
+
"ref": undefined,
|
|
130
216
|
"type": "name",
|
|
131
217
|
},
|
|
132
218
|
},
|
package/yup/src/__tests__/yup.ts
CHANGED
|
@@ -1,97 +1,50 @@
|
|
|
1
1
|
/* eslint-disable no-console, @typescript-eslint/ban-ts-comment */
|
|
2
2
|
import * as yup from 'yup';
|
|
3
3
|
import { yupResolver } from '..';
|
|
4
|
-
|
|
5
|
-
const schema = yup.object({
|
|
6
|
-
username: yup.string().matches(/^\w+$/).min(3).max(30).required(),
|
|
7
|
-
password: yup
|
|
8
|
-
.string()
|
|
9
|
-
.matches(/^[a-zA-Z0-9]{3,30}/)
|
|
10
|
-
.required(),
|
|
11
|
-
repeatPassword: yup.ref('password'),
|
|
12
|
-
accessToken: yup.string(),
|
|
13
|
-
birthYear: yup.number().min(1900).max(2013),
|
|
14
|
-
email: yup.string().email(),
|
|
15
|
-
tags: yup.array(yup.string()),
|
|
16
|
-
enabled: yup.boolean(),
|
|
17
|
-
});
|
|
4
|
+
import { schema, validData, fields, invalidData } from './__fixtures__/data';
|
|
18
5
|
|
|
19
6
|
describe('yupResolver', () => {
|
|
20
7
|
it('should return values from yupResolver when validation pass', async () => {
|
|
21
|
-
const data: yup.InferType<typeof schema> = {
|
|
22
|
-
username: 'Doe',
|
|
23
|
-
password: 'Password123',
|
|
24
|
-
repeatPassword: 'Password123',
|
|
25
|
-
birthYear: 2000,
|
|
26
|
-
email: 'john@doe.com',
|
|
27
|
-
tags: ['tag1', 'tag2'],
|
|
28
|
-
enabled: true,
|
|
29
|
-
accessToken: 'accessToken',
|
|
30
|
-
};
|
|
31
|
-
|
|
32
8
|
const schemaSpy = jest.spyOn(schema, 'validate');
|
|
33
9
|
const schemaSyncSpy = jest.spyOn(schema, 'validateSync');
|
|
34
10
|
|
|
35
|
-
const result = await yupResolver(schema)(
|
|
11
|
+
const result = await yupResolver(schema)(validData, undefined, {
|
|
12
|
+
fields,
|
|
13
|
+
});
|
|
36
14
|
|
|
37
15
|
expect(schemaSpy).toHaveBeenCalledTimes(1);
|
|
38
16
|
expect(schemaSyncSpy).not.toHaveBeenCalled();
|
|
39
|
-
expect(result).toEqual({ errors: {}, values:
|
|
17
|
+
expect(result).toEqual({ errors: {}, values: validData });
|
|
40
18
|
});
|
|
41
19
|
|
|
42
20
|
it('should return values from yupResolver with `mode: sync` when validation pass', async () => {
|
|
43
|
-
const data: yup.InferType<typeof schema> = {
|
|
44
|
-
username: 'Doe',
|
|
45
|
-
password: 'Password123',
|
|
46
|
-
repeatPassword: 'Password123',
|
|
47
|
-
birthYear: 2000,
|
|
48
|
-
email: 'john@doe.com',
|
|
49
|
-
tags: ['tag1', 'tag2'],
|
|
50
|
-
enabled: true,
|
|
51
|
-
accessToken: 'accessToken',
|
|
52
|
-
};
|
|
53
|
-
|
|
54
21
|
const validateSyncSpy = jest.spyOn(schema, 'validateSync');
|
|
55
22
|
const validateSpy = jest.spyOn(schema, 'validate');
|
|
56
23
|
|
|
57
|
-
const result = await yupResolver(schema, undefined, {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
{ fields: {} },
|
|
61
|
-
);
|
|
24
|
+
const result = await yupResolver(schema, undefined, {
|
|
25
|
+
mode: 'sync',
|
|
26
|
+
})(validData, undefined, { fields });
|
|
62
27
|
|
|
63
28
|
expect(validateSyncSpy).toHaveBeenCalledTimes(1);
|
|
64
29
|
expect(validateSpy).not.toHaveBeenCalled();
|
|
65
|
-
expect(result).toEqual({ errors: {}, values:
|
|
30
|
+
expect(result).toEqual({ errors: {}, values: validData });
|
|
66
31
|
});
|
|
67
32
|
|
|
68
33
|
it('should return a single error from yupResolver when validation fails', async () => {
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
birthYear: 'birthYear',
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
const result = await yupResolver(schema)(data, undefined, { fields: {} });
|
|
34
|
+
const result = await yupResolver(schema)(invalidData, undefined, {
|
|
35
|
+
fields,
|
|
36
|
+
});
|
|
76
37
|
|
|
77
38
|
expect(result).toMatchSnapshot();
|
|
78
39
|
});
|
|
79
40
|
|
|
80
41
|
it('should return a single error from yupResolver with `mode: sync` when validation fails', async () => {
|
|
81
|
-
const data = {
|
|
82
|
-
password: '___',
|
|
83
|
-
email: '',
|
|
84
|
-
birthYear: 'birthYear',
|
|
85
|
-
};
|
|
86
|
-
|
|
87
42
|
const validateSyncSpy = jest.spyOn(schema, 'validateSync');
|
|
88
43
|
const validateSpy = jest.spyOn(schema, 'validate');
|
|
89
44
|
|
|
90
|
-
const result = await yupResolver(schema, undefined, {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
{ fields: {} },
|
|
94
|
-
);
|
|
45
|
+
const result = await yupResolver(schema, undefined, {
|
|
46
|
+
mode: 'sync',
|
|
47
|
+
})(invalidData, undefined, { fields });
|
|
95
48
|
|
|
96
49
|
expect(validateSyncSpy).toHaveBeenCalledTimes(1);
|
|
97
50
|
expect(validateSpy).not.toHaveBeenCalled();
|
|
@@ -99,14 +52,8 @@ describe('yupResolver', () => {
|
|
|
99
52
|
});
|
|
100
53
|
|
|
101
54
|
it('should return all the errors from yupResolver when validation fails with `validateAllFieldCriteria` set to true', async () => {
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
email: '',
|
|
105
|
-
birthYear: 'birthYear',
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
const result = await yupResolver(schema)(data, undefined, {
|
|
109
|
-
fields: {},
|
|
55
|
+
const result = await yupResolver(schema)(invalidData, undefined, {
|
|
56
|
+
fields,
|
|
110
57
|
criteriaMode: 'all',
|
|
111
58
|
});
|
|
112
59
|
|
|
@@ -114,17 +61,11 @@ describe('yupResolver', () => {
|
|
|
114
61
|
});
|
|
115
62
|
|
|
116
63
|
it('should return all the errors from yupResolver when validation fails with `validateAllFieldCriteria` set to true and `mode: sync`', async () => {
|
|
117
|
-
const data = {
|
|
118
|
-
password: '___',
|
|
119
|
-
email: '',
|
|
120
|
-
birthYear: 'birthYear',
|
|
121
|
-
};
|
|
122
|
-
|
|
123
64
|
const result = await yupResolver(schema, undefined, { mode: 'sync' })(
|
|
124
|
-
|
|
65
|
+
invalidData,
|
|
125
66
|
undefined,
|
|
126
67
|
{
|
|
127
|
-
fields
|
|
68
|
+
fields,
|
|
128
69
|
criteriaMode: 'all',
|
|
129
70
|
},
|
|
130
71
|
);
|
|
@@ -147,7 +88,7 @@ describe('yupResolver', () => {
|
|
|
147
88
|
const validateSpy = jest.spyOn(schemaWithContext, 'validate');
|
|
148
89
|
|
|
149
90
|
const result = await yupResolver(schemaWithContext)(data, context, {
|
|
150
|
-
fields
|
|
91
|
+
fields,
|
|
151
92
|
});
|
|
152
93
|
expect(validateSpy).toHaveBeenCalledTimes(1);
|
|
153
94
|
expect(validateSpy).toHaveBeenCalledWith(
|
|
@@ -160,21 +101,6 @@ describe('yupResolver', () => {
|
|
|
160
101
|
expect(result).toMatchSnapshot();
|
|
161
102
|
});
|
|
162
103
|
|
|
163
|
-
it('should return an error result if inner yup validation error has no path', async () => {
|
|
164
|
-
const yupSchema = yup.object({
|
|
165
|
-
name: yup.string().required(),
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
jest.spyOn(yupSchema, 'validate').mockRejectedValueOnce({
|
|
169
|
-
inner: [{ message: 'error1', type: 'required' }],
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
const result = await yupResolver(yupSchema)({ name: '' }, undefined, {
|
|
173
|
-
fields: {},
|
|
174
|
-
});
|
|
175
|
-
expect(result).toMatchSnapshot();
|
|
176
|
-
});
|
|
177
|
-
|
|
178
104
|
it('should show a warning log if yup context is used instead only on dev environment', async () => {
|
|
179
105
|
jest.spyOn(console, 'warn').mockImplementation(jest.fn);
|
|
180
106
|
process.env.NODE_ENV = 'development';
|
|
@@ -183,7 +109,7 @@ describe('yupResolver', () => {
|
|
|
183
109
|
{},
|
|
184
110
|
undefined,
|
|
185
111
|
{
|
|
186
|
-
fields
|
|
112
|
+
fields,
|
|
187
113
|
},
|
|
188
114
|
);
|
|
189
115
|
expect(console.warn).toHaveBeenCalledWith(
|
|
@@ -199,7 +125,7 @@ describe('yupResolver', () => {
|
|
|
199
125
|
await yupResolver(yup.object(), { context: { noContext: true } })(
|
|
200
126
|
{},
|
|
201
127
|
undefined,
|
|
202
|
-
{ fields
|
|
128
|
+
{ fields },
|
|
203
129
|
);
|
|
204
130
|
expect(console.warn).not.toHaveBeenCalled();
|
|
205
131
|
process.env.NODE_ENV = 'test';
|
|
@@ -217,7 +143,7 @@ describe('yupResolver', () => {
|
|
|
217
143
|
'Email or name are required',
|
|
218
144
|
(value) => !!(value && (value.name || value.email)),
|
|
219
145
|
),
|
|
220
|
-
)({ name: '', email: '' }, undefined, { fields
|
|
146
|
+
)({ name: '', email: '' }, undefined, { fields });
|
|
221
147
|
|
|
222
148
|
expect(result).toMatchSnapshot();
|
|
223
149
|
});
|
package/yup/src/types.ts
CHANGED
|
@@ -5,13 +5,16 @@ import {
|
|
|
5
5
|
UnpackNestedValue,
|
|
6
6
|
} from 'react-hook-form';
|
|
7
7
|
import * as Yup from 'yup';
|
|
8
|
+
import type Lazy from 'yup/lib/Lazy';
|
|
8
9
|
|
|
9
|
-
type Options<T extends Yup.AnyObjectSchema
|
|
10
|
+
type Options<T extends Yup.AnyObjectSchema | Lazy<any>> = Parameters<
|
|
11
|
+
T['validate']
|
|
12
|
+
>[1];
|
|
10
13
|
|
|
11
|
-
export type Resolver = <T extends Yup.AnyObjectSchema
|
|
14
|
+
export type Resolver = <T extends Yup.AnyObjectSchema | Lazy<any>>(
|
|
12
15
|
schema: T,
|
|
13
16
|
schemaOptions?: Options<T>,
|
|
14
|
-
factoryOptions?: { mode
|
|
17
|
+
factoryOptions?: { mode?: 'async' | 'sync' },
|
|
15
18
|
) => <TFieldValues extends FieldValues, TContext>(
|
|
16
19
|
values: UnpackNestedValue<TFieldValues>,
|
|
17
20
|
context: TContext | undefined,
|
package/yup/src/yup.ts
CHANGED
|
@@ -1,95 +1,65 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
2
|
-
import { transformToNestObject } from 'react-hook-form';
|
|
3
1
|
import Yup from 'yup';
|
|
2
|
+
import { toNestError } from '@hookform/resolvers';
|
|
3
|
+
import { appendErrors, FieldError } from 'react-hook-form';
|
|
4
4
|
import { Resolver } from './types';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
* FYI: `path`: a string, indicating where there error was thrown. `path` is empty at the root level.
|
|
9
|
-
* react-hook-form's values are object so path is defined
|
|
7
|
+
* Why `path!` ? because it could be `undefined` in some case
|
|
10
8
|
* https://github.com/jquense/yup#validationerrorerrors-string--arraystring-value-any-path-string
|
|
11
9
|
*/
|
|
12
10
|
const parseErrorSchema = (
|
|
13
11
|
error: Yup.ValidationError,
|
|
14
12
|
validateAllFieldCriteria: boolean,
|
|
15
13
|
) => {
|
|
16
|
-
return
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
14
|
+
return error.inner.reduce<Record<string, FieldError>>((previous, error) => {
|
|
15
|
+
if (!previous[error.path!]) {
|
|
16
|
+
previous[error.path!] = { message: error.message, type: error.type! };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (validateAllFieldCriteria) {
|
|
20
|
+
previous[error.path!] = appendErrors(
|
|
21
|
+
error.path!,
|
|
22
|
+
validateAllFieldCriteria,
|
|
23
|
+
previous,
|
|
24
|
+
error.type!,
|
|
25
|
+
error.message,
|
|
26
|
+
) as FieldError;
|
|
27
|
+
}
|
|
22
28
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
...(key
|
|
26
|
-
? {
|
|
27
|
-
[key]: {
|
|
28
|
-
...(previous[key] || {
|
|
29
|
-
message,
|
|
30
|
-
type,
|
|
31
|
-
}),
|
|
32
|
-
...(validateAllFieldCriteria
|
|
33
|
-
? {
|
|
34
|
-
types: {
|
|
35
|
-
...previousTypes,
|
|
36
|
-
// @ts-expect-error
|
|
37
|
-
[type]: previousTypes[type]
|
|
38
|
-
? // @ts-expect-error
|
|
39
|
-
[...[].concat(previousTypes[type]), message]
|
|
40
|
-
: message,
|
|
41
|
-
},
|
|
42
|
-
}
|
|
43
|
-
: {}),
|
|
44
|
-
},
|
|
45
|
-
}
|
|
46
|
-
: {}),
|
|
47
|
-
};
|
|
48
|
-
},
|
|
49
|
-
{},
|
|
50
|
-
)
|
|
51
|
-
: {
|
|
52
|
-
// @ts-expect-error
|
|
53
|
-
[error.path]: { message: error.message, type: error.type },
|
|
54
|
-
};
|
|
29
|
+
return previous;
|
|
30
|
+
}, {});
|
|
55
31
|
};
|
|
56
32
|
|
|
57
33
|
export const yupResolver: Resolver = (
|
|
58
34
|
schema,
|
|
59
|
-
|
|
35
|
+
schemaOptions = {
|
|
60
36
|
abortEarly: false,
|
|
61
37
|
},
|
|
62
|
-
|
|
63
|
-
) => async (values, context,
|
|
38
|
+
resolverOptions = {},
|
|
39
|
+
) => async (values, context, options) => {
|
|
64
40
|
try {
|
|
65
|
-
if (
|
|
41
|
+
if (schemaOptions.context && process.env.NODE_ENV === 'development') {
|
|
66
42
|
// eslint-disable-next-line no-console
|
|
67
43
|
console.warn(
|
|
68
44
|
"You should not used the yup options context. Please, use the 'useForm' context object instead",
|
|
69
45
|
);
|
|
70
46
|
}
|
|
71
47
|
|
|
72
|
-
const result =
|
|
73
|
-
mode === '
|
|
74
|
-
|
|
75
|
-
...options,
|
|
76
|
-
context,
|
|
77
|
-
})
|
|
78
|
-
: schema.validateSync(values, {
|
|
79
|
-
...options,
|
|
80
|
-
context,
|
|
81
|
-
});
|
|
48
|
+
const result = await schema[
|
|
49
|
+
resolverOptions.mode === 'sync' ? 'validateSync' : 'validate'
|
|
50
|
+
](values, Object.assign({}, schemaOptions, { context }));
|
|
82
51
|
|
|
83
52
|
return {
|
|
84
53
|
values: result,
|
|
85
54
|
errors: {},
|
|
86
55
|
};
|
|
87
56
|
} catch (e) {
|
|
88
|
-
const parsedErrors = parseErrorSchema(e, criteriaMode === 'all');
|
|
89
|
-
|
|
90
57
|
return {
|
|
91
58
|
values: {},
|
|
92
|
-
errors:
|
|
59
|
+
errors: toNestError(
|
|
60
|
+
parseErrorSchema(e, options.criteriaMode === 'all'),
|
|
61
|
+
options.fields,
|
|
62
|
+
),
|
|
93
63
|
};
|
|
94
64
|
}
|
|
95
65
|
};
|
package/zod/dist/types.d.ts
CHANGED
|
@@ -2,5 +2,5 @@ import { FieldValues, ResolverResult, UnpackNestedValue, ResolverOptions } from
|
|
|
2
2
|
import * as z from 'zod';
|
|
3
3
|
import type { ParseParams } from 'zod/lib/src/parser';
|
|
4
4
|
export declare type Resolver = <T extends z.ZodSchema<any, any>>(schema: T, schemaOptions?: ParseParams, factoryOptions?: {
|
|
5
|
-
mode
|
|
5
|
+
mode?: 'async' | 'sync';
|
|
6
6
|
}) => <TFieldValues extends FieldValues, TContext>(values: UnpackNestedValue<TFieldValues>, context: TContext | undefined, options: ResolverOptions<TFieldValues>) => Promise<ResolverResult<TFieldValues>>;
|
package/zod/dist/zod.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var r=require("react-hook-form"),e=require("@hookform/resolvers")
|
|
1
|
+
var r=require("react-hook-form"),e=require("@hookform/resolvers"),o=function(e,o){for(var n={};e.length;){var t=e[0],s=t.code,u=t.message,i=t.path.join(".");n[i]||(n[i]={message:u,type:s}),"unionErrors"in t&&t.unionErrors.forEach(function(r){return r.errors.forEach(function(r){return e.push(r)})}),o&&(n[i]=r.appendErrors(i,o,n,s,u)),e.shift()}return n};exports.zodResolver=function(r,n,t){return void 0===t&&(t={}),function(s,u,i){try{return Promise.resolve(function(e,o){try{var u=Promise.resolve(r["sync"===t.mode?"parse":"parseAsync"](s,n)).then(function(r){return{errors:{},values:r}})}catch(r){return o(r)}return u&&u.then?u.then(void 0,o):u}(0,function(r){return{values:{},errors:r.isEmpty?{}:e.toNestError(o(r.errors,"all"===i.criteriaMode),i.fields)}}))}catch(r){return Promise.reject(r)}}};
|
|
2
2
|
//# sourceMappingURL=zod.js.map
|
package/zod/dist/zod.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zod.js","sources":["../src/zod.ts"],"sourcesContent":["import { appendErrors,
|
|
1
|
+
{"version":3,"file":"zod.js","sources":["../src/zod.ts"],"sourcesContent":["import { appendErrors, FieldError } from 'react-hook-form';\nimport * as z from 'zod';\nimport { toNestError } from '@hookform/resolvers';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (\n zodErrors: z.ZodSuberror[],\n validateAllFieldCriteria: boolean,\n) => {\n const errors: Record<string, FieldError> = {};\n for (; zodErrors.length; ) {\n const error = zodErrors[0];\n const { code, message, path } = error;\n const _path = path.join('.');\n\n if (!errors[_path]) {\n errors[_path] = { message, type: code };\n }\n\n if ('unionErrors' in error) {\n error.unionErrors.forEach((unionError) =>\n unionError.errors.forEach((e) => zodErrors.push(e)),\n );\n }\n\n if (validateAllFieldCriteria) {\n errors[_path] = appendErrors(\n _path,\n validateAllFieldCriteria,\n errors,\n code,\n message,\n ) as FieldError;\n }\n\n zodErrors.shift();\n }\n\n return errors;\n};\n\nexport const zodResolver: Resolver = (\n schema,\n schemaOptions,\n resolverOptions = {},\n) => async (values, _, options) => {\n try {\n return {\n errors: {},\n values: await schema[\n resolverOptions.mode === 'sync' ? 'parse' : 'parseAsync'\n ](values, schemaOptions),\n };\n } catch (error) {\n return {\n values: {},\n errors: error.isEmpty\n ? {}\n : toNestError(\n parseErrorSchema(error.errors, options.criteriaMode === 'all'),\n options.fields,\n ),\n };\n }\n};\n"],"names":["parseErrorSchema","zodErrors","validateAllFieldCriteria","errors","length","error","code","message","_path","path","join","type","unionErrors","forEach","unionError","e","push","appendErrors","shift","schema","schemaOptions","resolverOptions","values","_","options","mode","isEmpty","toNestError","criteriaMode","fields"],"mappings":"kEAKMA,EAAmB,SACvBC,EACAC,GAGA,IADA,IAAMC,EAAqC,GACpCF,EAAUG,QAAU,CACzB,IAAMC,EAAQJ,EAAU,GAChBK,EAAwBD,EAAxBC,KAAMC,EAAkBF,EAAlBE,QACRC,EAD0BH,EAATI,KACJC,KAAK,KAEnBP,EAAOK,KACVL,EAAOK,GAAS,CAAED,QAAAA,EAASI,KAAML,IAG/B,gBAAiBD,GACnBA,EAAMO,YAAYC,QAAQ,SAACC,UACzBA,EAAWX,OAAOU,QAAQ,SAACE,UAAMd,EAAUe,KAAKD,OAIhDb,IACFC,EAAOK,GAASS,eACdT,EACAN,EACAC,EACAG,EACAC,IAIJN,EAAUiB,QAGZ,OAAOf,uBAG4B,SACnCgB,EACAC,EACAC,mBAAAA,IAAAA,EAAkB,aACRC,EAAQC,EAAGC,sEAIHL,EACa,SAAzBE,EAAgBI,KAAkB,QAAU,cAC5CH,EAAQF,qBAJZ,MAAO,CACLjB,OAAQ,GACRmB,iFAIKjB,GACP,MAAO,CACLiB,OAAQ,GACRnB,OAAQE,EAAMqB,QACV,GACAC,cACE3B,EAAiBK,EAAMF,OAAiC,QAAzBqB,EAAQI,cACvCJ,EAAQK,YAnBiB"}
|
package/zod/dist/zod.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{appendErrors as r}from"react-hook-form";import{toNestError as e}from"@hookform/resolvers";var o=function(e,o){for(var n={};e.length;){var t=e[0],s=t.code,i=t.message,u=t.path.join(".");n[u]||(n[u]={message:i,type:s}),"unionErrors"in t&&t.unionErrors.forEach(function(r){return r.errors.forEach(function(r){return e.push(r)})}),o&&(n[u]=r(u,o,n,s,i)),e.shift()}return n},n=function(r,n,t){return void 0===t&&(t={}),function(s,i,u){try{return Promise.resolve(function(e,o){try{var i=Promise.resolve(r["sync"===t.mode?"parse":"parseAsync"](s,n)).then(function(r){return{errors:{},values:r}})}catch(r){return o(r)}return i&&i.then?i.then(void 0,o):i}(0,function(r){return{values:{},errors:r.isEmpty?{}:e(o(r.errors,"all"===u.criteriaMode),u.fields)}}))}catch(r){return Promise.reject(r)}}};export{n as zodResolver};
|
|
2
2
|
//# sourceMappingURL=zod.module.js.map
|
package/zod/dist/zod.modern.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{appendErrors as r}from"react-hook-form";import{toNestError as o}from"@hookform/resolvers";const e=(o,e)=>{const s={};for(;o.length;){const t=o[0],{code:a,message:n,path:c}=t,i=c.join(".");s[i]||(s[i]={message:n,type:a}),"unionErrors"in t&&t.unionErrors.forEach(r=>r.errors.forEach(r=>o.push(r))),e&&(s[i]=r(i,e,s,a,n)),o.shift()}return s},s=(r,s,t={})=>async(a,n,c)=>{try{return{errors:{},values:await r["sync"===t.mode?"parse":"parseAsync"](a,s)}}catch(r){return{values:{},errors:r.isEmpty?{}:o(e(r.errors,"all"===c.criteriaMode),c.fields)}}};export{s as zodResolver};
|
|
2
2
|
//# sourceMappingURL=zod.modern.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zod.modern.js","sources":["../src/zod.ts"],"sourcesContent":["import { appendErrors,
|
|
1
|
+
{"version":3,"file":"zod.modern.js","sources":["../src/zod.ts"],"sourcesContent":["import { appendErrors, FieldError } from 'react-hook-form';\nimport * as z from 'zod';\nimport { toNestError } from '@hookform/resolvers';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (\n zodErrors: z.ZodSuberror[],\n validateAllFieldCriteria: boolean,\n) => {\n const errors: Record<string, FieldError> = {};\n for (; zodErrors.length; ) {\n const error = zodErrors[0];\n const { code, message, path } = error;\n const _path = path.join('.');\n\n if (!errors[_path]) {\n errors[_path] = { message, type: code };\n }\n\n if ('unionErrors' in error) {\n error.unionErrors.forEach((unionError) =>\n unionError.errors.forEach((e) => zodErrors.push(e)),\n );\n }\n\n if (validateAllFieldCriteria) {\n errors[_path] = appendErrors(\n _path,\n validateAllFieldCriteria,\n errors,\n code,\n message,\n ) as FieldError;\n }\n\n zodErrors.shift();\n }\n\n return errors;\n};\n\nexport const zodResolver: Resolver = (\n schema,\n schemaOptions,\n resolverOptions = {},\n) => async (values, _, options) => {\n try {\n return {\n errors: {},\n values: await schema[\n resolverOptions.mode === 'sync' ? 'parse' : 'parseAsync'\n ](values, schemaOptions),\n };\n } catch (error) {\n return {\n values: {},\n errors: error.isEmpty\n ? {}\n : toNestError(\n parseErrorSchema(error.errors, options.criteriaMode === 'all'),\n options.fields,\n ),\n };\n }\n};\n"],"names":["parseErrorSchema","zodErrors","validateAllFieldCriteria","errors","length","error","code","message","path","_path","join","type","unionErrors","forEach","unionError","e","push","appendErrors","shift","zodResolver","schema","schemaOptions","resolverOptions","async","values","_","options","mode","isEmpty","toNestError","criteriaMode","fields"],"mappings":"iGAKA,MAAMA,EAAmB,CACvBC,EACAC,KAEA,MAAMC,EAAqC,GAC3C,KAAOF,EAAUG,QAAU,CACzB,MAAMC,EAAQJ,EAAU,IAClBK,KAAEA,EAAFC,QAAQA,EAARC,KAAiBA,GAASH,EAC1BI,EAAQD,EAAKE,KAAK,KAEnBP,EAAOM,KACVN,EAAOM,GAAS,CAAEF,QAAAA,EAASI,KAAML,IAG/B,gBAAiBD,GACnBA,EAAMO,YAAYC,QAASC,GACzBA,EAAWX,OAAOU,QAASE,GAAMd,EAAUe,KAAKD,KAIhDb,IACFC,EAAOM,GAASQ,EACdR,EACAP,EACAC,EACAG,EACAC,IAIJN,EAAUiB,QAGZ,OAAOf,GAGIgB,EAAwB,CACnCC,EACAC,EACAC,EAAkB,KACfC,MAAOC,EAAQC,EAAGC,KACrB,IACE,MAAO,CACLvB,OAAQ,GACRqB,aAAcJ,EACa,SAAzBE,EAAgBK,KAAkB,QAAU,cAC5CH,EAAQH,IAEZ,MAAOhB,GACP,MAAO,CACLmB,OAAQ,GACRrB,OAAQE,EAAMuB,QACV,GACAC,EACE7B,EAAiBK,EAAMF,OAAiC,QAAzBuB,EAAQI,cACvCJ,EAAQK"}
|
package/zod/dist/zod.module.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{appendErrors as r}from"react-hook-form";import{toNestError as e}from"@hookform/resolvers";var o=function(e,o){for(var n={};e.length;){var t=e[0],s=t.code,i=t.message,u=t.path.join(".");n[u]||(n[u]={message:i,type:s}),"unionErrors"in t&&t.unionErrors.forEach(function(r){return r.errors.forEach(function(r){return e.push(r)})}),o&&(n[u]=r(u,o,n,s,i)),e.shift()}return n},n=function(r,n,t){return void 0===t&&(t={}),function(s,i,u){try{return Promise.resolve(function(e,o){try{var i=Promise.resolve(r["sync"===t.mode?"parse":"parseAsync"](s,n)).then(function(r){return{errors:{},values:r}})}catch(r){return o(r)}return i&&i.then?i.then(void 0,o):i}(0,function(r){return{values:{},errors:r.isEmpty?{}:e(o(r.errors,"all"===u.criteriaMode),u.fields)}}))}catch(r){return Promise.reject(r)}}};export{n as zodResolver};
|
|
2
2
|
//# sourceMappingURL=zod.module.js.map
|