@hookform/resolvers 1.3.4 → 1.3.8
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 +13 -5
- package/dist/convertArrayToPathName.d.ts +1 -0
- package/dist/ie11/joi/index.d.ts +1 -0
- package/dist/ie11/{joi.d.ts → joi/joi.d.ts} +0 -0
- package/dist/ie11/joi/joi.js +2 -0
- package/dist/ie11/joi/joi.js.map +1 -0
- package/dist/ie11/superstruct/index.d.ts +1 -0
- package/dist/ie11/{superstruct.d.ts → superstruct/superstruct.d.ts} +0 -0
- package/dist/ie11/superstruct/superstruct.js +2 -0
- package/dist/ie11/superstruct/superstruct.js.map +1 -0
- package/dist/ie11/vest/index.d.ts +1 -0
- package/dist/ie11/{vest.d.ts → vest/vest.d.ts} +0 -0
- package/dist/ie11/vest/vest.js +2 -0
- package/dist/ie11/vest/vest.js.map +1 -0
- package/dist/ie11/yup/index.d.ts +1 -0
- package/dist/{yup.d.ts → ie11/yup/yup.d.ts} +6 -5
- package/dist/ie11/yup/yup.js +2 -0
- package/dist/ie11/yup/yup.js.map +1 -0
- package/dist/ie11/zod/index.d.ts +1 -0
- package/dist/ie11/{zod.d.ts → zod/zod.d.ts} +0 -0
- package/dist/ie11/zod/zod.js +2 -0
- package/dist/ie11/zod/zod.js.map +1 -0
- 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/joi/dist/index.d.ts +1 -0
- package/{dist → joi/dist}/joi.d.ts +3 -3
- 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/package.json +17 -0
- package/joi/src/__tests__/__snapshots__/joi.ts.snap +13 -0
- package/joi/src/__tests__/joi.ts +49 -0
- package/joi/src/index.ts +1 -0
- package/joi/src/joi.ts +76 -0
- package/package.json +83 -39
- package/superstruct/dist/index.d.ts +1 -0
- package/{dist → superstruct/dist}/superstruct.d.ts +5 -5
- 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/package.json +17 -0
- package/superstruct/src/__tests__/__snapshots__/superstruct.ts.snap +33 -0
- package/superstruct/src/__tests__/superstruct.ts +45 -0
- package/superstruct/src/index.ts +1 -0
- package/superstruct/src/superstruct.ts +68 -0
- package/vest/dist/index.d.ts +1 -0
- package/{dist → vest/dist}/vest.d.ts +5 -5
- 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 +17 -0
- package/vest/src/__tests__/vest.ts +120 -0
- package/vest/src/index.ts +1 -0
- package/vest/src/vest.ts +59 -0
- package/yup/dist/index.d.ts +1 -0
- package/{dist/ie11 → yup/dist}/yup.d.ts +2 -1
- 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__/__snapshots__/yup.ts.snap +119 -0
- package/yup/src/__tests__/yup.ts +311 -0
- package/yup/src/index.ts +1 -0
- package/yup/src/yup.ts +96 -0
- package/zod/dist/index.d.ts +1 -0
- package/{dist → zod/dist}/zod.d.ts +4 -4
- 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__/__snapshots__/zod.ts.snap +194 -0
- package/zod/src/__tests__/zod.ts +131 -0
- package/zod/src/index.ts +1 -0
- package/zod/src/zod.ts +84 -0
- package/CHANGELOG.md +0 -7
- package/dist/ie11/_virtual/_tslib.js +0 -2
- package/dist/ie11/_virtual/_tslib.js.map +0 -1
- package/dist/ie11/joi.js +0 -2
- package/dist/ie11/joi.js.map +0 -1
- package/dist/ie11/superstruct.js +0 -2
- package/dist/ie11/superstruct.js.map +0 -1
- package/dist/ie11/utils/convertArrayToPathName.d.ts +0 -2
- package/dist/ie11/utils/convertArrayToPathName.js +0 -2
- package/dist/ie11/utils/convertArrayToPathName.js.map +0 -1
- package/dist/ie11/vest.js +0 -2
- package/dist/ie11/vest.js.map +0 -1
- package/dist/ie11/yup.js +0 -2
- package/dist/ie11/yup.js.map +0 -1
- package/dist/ie11/zod.js +0 -2
- package/dist/ie11/zod.js.map +0 -1
- package/dist/joi.js +0 -38
- package/dist/joi.js.map +0 -1
- package/dist/superstruct.js +0 -36
- 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 -6
- package/dist/utils/convertArrayToPathName.js.map +0 -1
- package/dist/vest.js +0 -26
- package/dist/vest.js.map +0 -1
- package/dist/yup.js +0 -59
- package/dist/yup.js.map +0 -1
- package/dist/zod.js +0 -44
- package/dist/zod.js.map +0 -1
- package/joi.d.ts +0 -1
- package/joi.js +0 -1
- package/superstruct.d.ts +0 -1
- package/superstruct.js +0 -1
- package/vest.d.ts +0 -1
- package/vest.js +0 -1
- package/yup.d.ts +0 -1
- package/yup.js +0 -1
- package/zod.d.ts +0 -1
- package/zod.js +0 -1
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
/* eslint-disable no-console, @typescript-eslint/ban-ts-comment */
|
|
2
|
+
import * as yup from 'yup';
|
|
3
|
+
import { yupResolver } from '..';
|
|
4
|
+
|
|
5
|
+
const errors = {
|
|
6
|
+
name: 'ValidationError',
|
|
7
|
+
value: { createdOn: '2019-03-27T04:05:51.503Z' },
|
|
8
|
+
path: undefined,
|
|
9
|
+
type: undefined,
|
|
10
|
+
errors: ['name is a required field', 'age is a required field'],
|
|
11
|
+
inner: [
|
|
12
|
+
{
|
|
13
|
+
name: 'ValidationError',
|
|
14
|
+
value: undefined,
|
|
15
|
+
path: 'name',
|
|
16
|
+
type: 'required',
|
|
17
|
+
errors: [],
|
|
18
|
+
inner: [],
|
|
19
|
+
message: 'name is a required field',
|
|
20
|
+
params: [],
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
name: 'ValidationError',
|
|
24
|
+
value: undefined,
|
|
25
|
+
path: 'name',
|
|
26
|
+
type: 'min',
|
|
27
|
+
errors: [],
|
|
28
|
+
inner: [],
|
|
29
|
+
message: 'name is a min field',
|
|
30
|
+
params: [],
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
name: 'ValidationError',
|
|
34
|
+
value: undefined,
|
|
35
|
+
path: 'age',
|
|
36
|
+
type: 'required',
|
|
37
|
+
errors: [],
|
|
38
|
+
inner: [],
|
|
39
|
+
message: 'age is a required field',
|
|
40
|
+
params: [],
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const schema = yup.object({
|
|
46
|
+
name: yup.string().required(),
|
|
47
|
+
age: yup.number().required().positive().integer(),
|
|
48
|
+
email: yup.string().email(),
|
|
49
|
+
password: yup
|
|
50
|
+
.string()
|
|
51
|
+
.required()
|
|
52
|
+
.min(8)
|
|
53
|
+
.matches(RegExp('(.*[a-z].*)'), 'Lowercase')
|
|
54
|
+
.matches(RegExp('(.*[A-Z].*)'), 'Uppercase')
|
|
55
|
+
.matches(RegExp('(.*\\d.*)'), 'Number')
|
|
56
|
+
.matches(RegExp('[!@#$%^&*(),.?":{}|<>]'), 'Special'),
|
|
57
|
+
website: yup.string().url(),
|
|
58
|
+
createdOn: yup.date().default(function () {
|
|
59
|
+
return new Date();
|
|
60
|
+
}),
|
|
61
|
+
foo: yup
|
|
62
|
+
.array()
|
|
63
|
+
.required()
|
|
64
|
+
.of(
|
|
65
|
+
yup.object({
|
|
66
|
+
loose: yup.boolean(),
|
|
67
|
+
}),
|
|
68
|
+
),
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe('yupResolver', () => {
|
|
72
|
+
it('should get values', async () => {
|
|
73
|
+
const data = {
|
|
74
|
+
name: 'jimmy',
|
|
75
|
+
age: 24,
|
|
76
|
+
email: 'jimmy@mail.com',
|
|
77
|
+
password: '[}tehk6Uor',
|
|
78
|
+
website: 'https://react-hook-form.com/',
|
|
79
|
+
createdOn: new Date('2014-09-23T19:25:25Z'),
|
|
80
|
+
foo: [{ loose: true }],
|
|
81
|
+
};
|
|
82
|
+
expect(await yupResolver(schema)(data)).toEqual({
|
|
83
|
+
errors: {},
|
|
84
|
+
values: data,
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('should pass down the yup context', async () => {
|
|
89
|
+
const data = { name: 'eric' };
|
|
90
|
+
const context = { min: true };
|
|
91
|
+
const schemaWithContext = yup.object({
|
|
92
|
+
name: yup
|
|
93
|
+
.string()
|
|
94
|
+
.required()
|
|
95
|
+
.when('$min', (min: boolean, schema: yup.StringSchema) => {
|
|
96
|
+
return min ? schema.min(6) : schema;
|
|
97
|
+
}),
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
const schemaSpyValidate = jest.spyOn(schemaWithContext, 'validate');
|
|
101
|
+
|
|
102
|
+
const output = await yupResolver(schemaWithContext)(data, context);
|
|
103
|
+
expect(schemaSpyValidate).toHaveBeenCalledTimes(1);
|
|
104
|
+
expect(schemaSpyValidate).toHaveBeenCalledWith(
|
|
105
|
+
data,
|
|
106
|
+
expect.objectContaining({
|
|
107
|
+
abortEarly: false,
|
|
108
|
+
context,
|
|
109
|
+
}),
|
|
110
|
+
);
|
|
111
|
+
expect(output).toMatchSnapshot();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe('errors', () => {
|
|
115
|
+
it('should get errors with validate all criteria fields', async () => {
|
|
116
|
+
const data = {
|
|
117
|
+
name: 2,
|
|
118
|
+
age: 'test',
|
|
119
|
+
password: '',
|
|
120
|
+
createdOn: null,
|
|
121
|
+
foo: [{ loose: null }],
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const output = await yupResolver(schema)(data, {}, true);
|
|
125
|
+
expect(output).toMatchSnapshot();
|
|
126
|
+
expect(output.errors['foo']?.[0]?.['loose']).toBeDefined();
|
|
127
|
+
expect(output.errors['foo']?.[0]?.['loose']?.types)
|
|
128
|
+
.toMatchInlineSnapshot(`
|
|
129
|
+
Object {
|
|
130
|
+
"typeError": "foo[0].loose must be a \`boolean\` type, but the final value was: \`null\`.
|
|
131
|
+
If \\"null\\" is intended as an empty value be sure to mark the schema as \`.nullable()\`",
|
|
132
|
+
}
|
|
133
|
+
`);
|
|
134
|
+
expect(output.errors.age?.types).toMatchInlineSnapshot(`
|
|
135
|
+
Object {
|
|
136
|
+
"typeError": "age must be a \`number\` type, but the final value was: \`NaN\` (cast from the value \`\\"test\\"\`).",
|
|
137
|
+
}
|
|
138
|
+
`);
|
|
139
|
+
expect(output.errors.createdOn?.types).toMatchInlineSnapshot(`
|
|
140
|
+
Object {
|
|
141
|
+
"typeError": "createdOn must be a \`date\` type, but the final value was: \`Invalid Date\`.",
|
|
142
|
+
}
|
|
143
|
+
`);
|
|
144
|
+
expect(output.errors.password?.types).toMatchInlineSnapshot(`
|
|
145
|
+
Object {
|
|
146
|
+
"matches": Array [
|
|
147
|
+
"Lowercase",
|
|
148
|
+
"Uppercase",
|
|
149
|
+
"Number",
|
|
150
|
+
"Special",
|
|
151
|
+
],
|
|
152
|
+
"min": "password must be at least 8 characters",
|
|
153
|
+
"required": "password is a required field",
|
|
154
|
+
}
|
|
155
|
+
`);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('should get errors without validate all criteria fields', async () => {
|
|
159
|
+
const data = {
|
|
160
|
+
name: 2,
|
|
161
|
+
age: 'test',
|
|
162
|
+
createdOn: null,
|
|
163
|
+
foo: [{ loose: null }],
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const output = await yupResolver(schema)(data);
|
|
167
|
+
expect(output).toMatchSnapshot();
|
|
168
|
+
expect(output.errors.age?.types).toBeUndefined();
|
|
169
|
+
expect(output.errors.createdOn?.types).toBeUndefined();
|
|
170
|
+
expect(output.errors.password?.types).toBeUndefined();
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('should get error if yup errors has no inner errors', async () => {
|
|
174
|
+
const data = {
|
|
175
|
+
name: 2,
|
|
176
|
+
age: 'test',
|
|
177
|
+
createdOn: null,
|
|
178
|
+
foo: [{ loose: null }],
|
|
179
|
+
};
|
|
180
|
+
const output = await yupResolver(schema, {
|
|
181
|
+
abortEarly: true,
|
|
182
|
+
})(data, undefined, true);
|
|
183
|
+
|
|
184
|
+
expect(output.errors).toMatchInlineSnapshot(`
|
|
185
|
+
Object {
|
|
186
|
+
"createdOn": Object {
|
|
187
|
+
"message": "createdOn must be a \`date\` type, but the final value was: \`Invalid Date\`.",
|
|
188
|
+
"type": "typeError",
|
|
189
|
+
},
|
|
190
|
+
}
|
|
191
|
+
`);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it('should return an error result if inner yup validation error has no path', async () => {
|
|
195
|
+
const data = { name: '' };
|
|
196
|
+
const schemaWithContext = yup.object().shape({
|
|
197
|
+
name: yup.string().required(),
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
jest.spyOn(schemaWithContext, 'validate').mockRejectedValueOnce({
|
|
201
|
+
inner: [{ path: '', message: 'error1', type: 'required' }],
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
const output = await yupResolver(schemaWithContext)(data);
|
|
205
|
+
expect(output).toMatchSnapshot();
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
describe('validateWithSchema', () => {
|
|
211
|
+
it('should return undefined when no error reported', async () => {
|
|
212
|
+
const schema = yup.object();
|
|
213
|
+
jest.spyOn(schema, 'validate').mockRejectedValueOnce(errors);
|
|
214
|
+
|
|
215
|
+
expect(await yupResolver(schema)({})).toMatchSnapshot();
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('should return empty object when validate pass', async () => {
|
|
219
|
+
const schema = yup.object();
|
|
220
|
+
|
|
221
|
+
expect(await yupResolver(schema)({})).toMatchInlineSnapshot(`
|
|
222
|
+
Object {
|
|
223
|
+
"errors": Object {},
|
|
224
|
+
"values": Object {},
|
|
225
|
+
}
|
|
226
|
+
`);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('should return an error based on the user context', async () => {
|
|
230
|
+
const data = { name: 'eric' };
|
|
231
|
+
const schemaWithContext = yup.object().shape({
|
|
232
|
+
name: yup
|
|
233
|
+
.string()
|
|
234
|
+
.required()
|
|
235
|
+
.when('$min', (min: boolean, schema: yup.StringSchema) => {
|
|
236
|
+
return min ? schema.min(6) : schema;
|
|
237
|
+
}),
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
expect(await yupResolver(schemaWithContext)(data, { min: true }))
|
|
241
|
+
.toMatchInlineSnapshot(`
|
|
242
|
+
Object {
|
|
243
|
+
"errors": Object {
|
|
244
|
+
"name": Object {
|
|
245
|
+
"message": "name must be at least 6 characters",
|
|
246
|
+
"type": "min",
|
|
247
|
+
},
|
|
248
|
+
},
|
|
249
|
+
"values": Object {},
|
|
250
|
+
}
|
|
251
|
+
`);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it('should show a warning log if yup context is used instead only on dev environment', async () => {
|
|
255
|
+
jest.spyOn(console, 'warn').mockImplementation(jest.fn);
|
|
256
|
+
process.env.NODE_ENV = 'development';
|
|
257
|
+
|
|
258
|
+
await yupResolver(yup.object(), { context: { noContext: true } })({});
|
|
259
|
+
expect(console.warn).toHaveBeenCalledWith(
|
|
260
|
+
"You should not used the yup options context. Please, use the 'useForm' context object instead",
|
|
261
|
+
);
|
|
262
|
+
process.env.NODE_ENV = 'test';
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it('should not show warning log if yup context is used instead only on production environment', async () => {
|
|
266
|
+
jest.spyOn(console, 'warn').mockImplementation(jest.fn);
|
|
267
|
+
process.env.NODE_ENV = 'production';
|
|
268
|
+
|
|
269
|
+
await yupResolver(yup.object(), { context: { noContext: true } })({});
|
|
270
|
+
expect(console.warn).not.toHaveBeenCalled();
|
|
271
|
+
process.env.NODE_ENV = 'test';
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it('should return correct error message with using yup.test', async () => {
|
|
275
|
+
const output = await yupResolver(
|
|
276
|
+
yup
|
|
277
|
+
.object({
|
|
278
|
+
name: yup.string(),
|
|
279
|
+
email: yup.string(),
|
|
280
|
+
})
|
|
281
|
+
.test(
|
|
282
|
+
'name',
|
|
283
|
+
'Email or name are required',
|
|
284
|
+
(value) => !!(value && (value.name || value.email)),
|
|
285
|
+
),
|
|
286
|
+
)({ name: '', email: '' });
|
|
287
|
+
|
|
288
|
+
expect(output).toEqual({
|
|
289
|
+
values: {},
|
|
290
|
+
errors: { name: { message: 'Email or name are required', type: 'name' } },
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it('should throw an error without inner property', (done) => {
|
|
295
|
+
const schemaWithWhen = yup.object({
|
|
296
|
+
name: yup.string().required(),
|
|
297
|
+
value: yup.string().when('name', {
|
|
298
|
+
is: 'test',
|
|
299
|
+
then: yup.number().required(),
|
|
300
|
+
}),
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
// @ts-expect-error
|
|
304
|
+
yupResolver(schemaWithWhen)({ name: 'test', value: '' }).catch((e) => {
|
|
305
|
+
expect(e).toMatchInlineSnapshot(
|
|
306
|
+
`[TypeError: You cannot \`concat()\` schema's of different types: string and number]`,
|
|
307
|
+
);
|
|
308
|
+
done();
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
});
|
package/yup/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './yup';
|
package/yup/src/yup.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
2
|
+
import { Resolver, transformToNestObject, FieldValues } from 'react-hook-form';
|
|
3
|
+
import Yup from 'yup';
|
|
4
|
+
import Lazy from 'yup/lib/Lazy';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* From 0.32.0, Yup add TypeScript support and `path` typing is optional that's why we have `@ts-expect-error`
|
|
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
|
|
10
|
+
* https://github.com/jquense/yup#validationerrorerrors-string--arraystring-value-any-path-string
|
|
11
|
+
*/
|
|
12
|
+
const parseErrorSchema = (
|
|
13
|
+
error: Yup.ValidationError,
|
|
14
|
+
validateAllFieldCriteria: boolean,
|
|
15
|
+
) => {
|
|
16
|
+
return Array.isArray(error.inner) && error.inner.length
|
|
17
|
+
? error.inner.reduce(
|
|
18
|
+
(previous: Record<string, any>, { path, message, type }) => {
|
|
19
|
+
// @ts-expect-error
|
|
20
|
+
const previousTypes = (previous[path] && previous[path].types) || {};
|
|
21
|
+
const key = path || type;
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
...previous,
|
|
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
|
+
};
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
type ValidateOptions<T extends Yup.AnyObjectSchema> = Parameters<
|
|
58
|
+
T['validate']
|
|
59
|
+
>[1];
|
|
60
|
+
|
|
61
|
+
export const yupResolver = <TFieldValues extends FieldValues>(
|
|
62
|
+
schema: Yup.AnyObjectSchema | Lazy<any, any>,
|
|
63
|
+
options: ValidateOptions<Yup.AnyObjectSchema> = {
|
|
64
|
+
abortEarly: false,
|
|
65
|
+
},
|
|
66
|
+
): Resolver<TFieldValues> => async (
|
|
67
|
+
values,
|
|
68
|
+
context,
|
|
69
|
+
validateAllFieldCriteria = false,
|
|
70
|
+
) => {
|
|
71
|
+
try {
|
|
72
|
+
if (options.context && process.env.NODE_ENV === 'development') {
|
|
73
|
+
// eslint-disable-next-line no-console
|
|
74
|
+
console.warn(
|
|
75
|
+
"You should not used the yup options context. Please, use the 'useForm' context object instead",
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
values: await schema.validate(values, {
|
|
80
|
+
...options,
|
|
81
|
+
context,
|
|
82
|
+
}),
|
|
83
|
+
errors: {},
|
|
84
|
+
};
|
|
85
|
+
} catch (e: any) {
|
|
86
|
+
if (!e.inner) {
|
|
87
|
+
throw e;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const parsedErrors = parseErrorSchema(e, validateAllFieldCriteria);
|
|
91
|
+
return {
|
|
92
|
+
values: {},
|
|
93
|
+
errors: transformToNestObject(parsedErrors),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './zod';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Resolver } from 'react-hook-form';
|
|
2
|
-
import * as z from 'zod';
|
|
3
|
-
import { ParseParams } from 'zod/lib/src/parser';
|
|
4
|
-
export declare const zodResolver: <T extends z.ZodType<any, any>>(schema: T, options?: ParseParams | undefined) => Resolver<z.TypeOf<T>, object>;
|
|
1
|
+
import { Resolver } from 'react-hook-form';
|
|
2
|
+
import * as z from 'zod';
|
|
3
|
+
import { ParseParams } from 'zod/lib/src/parser';
|
|
4
|
+
export declare const zodResolver: <T extends z.ZodType<any, any>>(schema: T, options?: ParseParams | undefined) => Resolver<z.TypeOf<T>, object>;
|
package/zod/dist/zod.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var r=require("react-hook-form"),e=require("@hookform/resolvers");function t(){return(t=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n])}return r}).apply(this,arguments)}function n(r,e){(null==e||e>r.length)&&(e=r.length);for(var t=0,n=new Array(e);t<e;t++)n[t]=r[t];return n}function o(r,e){var t;if("undefined"==typeof Symbol||null==r[Symbol.iterator]){if(Array.isArray(r)||(t=function(r,e){if(r){if("string"==typeof r)return n(r,e);var t=Object.prototype.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(r):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?n(r,e):void 0}}(r))||e&&r&&"number"==typeof r.length){t&&(r=t);var o=0;return function(){return o>=r.length?{done:!0}:{done:!1,value:r[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(t=r[Symbol.iterator]()).next.bind(t)}var a=function(n,a){if(n.isEmpty)return{};for(var i,s=[].concat(n.errors),u={},c=o(s);!(i=c()).done;){var l,f,v,p=i.value,y=p.path,m=p.message,d=p.code,h=e.convertArrayToPathName(y);if("unionErrors"in p)for(var b,g=o(p.unionErrors.map(function(r){return r.errors}));!(b=g()).done;)s.push.apply(s,b.value);u=t({},u,y?u[h]&&a?((l={})[h]=r.appendErrors(h,a,u,d,m),l):((v={})[h]=u[h]||t({message:m,type:d},a?{types:(f={},f[d]=m||!0,f)}:{}),v):{})}return u};exports.zodResolver=function(e,t){return function(n,o,i){void 0===i&&(i=!1);try{var s=e.safeParse(n,t);return Promise.resolve(s.success?{values:s.data,errors:{}}:{values:{},errors:r.transformToNestObject(a(s.error,i))})}catch(r){return Promise.reject(r)}}};
|
|
2
|
+
//# sourceMappingURL=zod.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod.js","sources":["../src/zod.ts"],"sourcesContent":["import {\n appendErrors,\n Resolver,\n ResolverError,\n ResolverSuccess,\n transformToNestObject,\n} from 'react-hook-form';\nimport * as z from 'zod';\nimport { ParseParams } from 'zod/lib/src/parser';\nimport { convertArrayToPathName } from '@hookform/resolvers';\n\nconst parseErrorSchema = (\n zodError: z.ZodError,\n validateAllFieldCriteria: boolean,\n) => {\n if (zodError.isEmpty) {\n return {};\n }\n\n const errors = [...zodError.errors];\n let previous: Record<string, any> = {};\n\n for (const error of errors) {\n const { path, message, code: type } = error;\n const currentPath = convertArrayToPathName(path);\n\n if ('unionErrors' in error) {\n for (const subErrors of error.unionErrors.map((e) => e.errors)) {\n errors.push(...subErrors);\n }\n }\n\n previous = {\n ...previous,\n ...(path\n ? previous[currentPath] && validateAllFieldCriteria\n ? {\n [currentPath]: appendErrors(\n currentPath,\n validateAllFieldCriteria,\n previous,\n type,\n message,\n ),\n }\n : {\n [currentPath]: previous[currentPath] || {\n message,\n type,\n ...(validateAllFieldCriteria\n ? {\n types: { [type]: message || true },\n }\n : {}),\n },\n }\n : {}),\n };\n }\n\n return previous;\n};\n\nexport const zodResolver = <T extends z.ZodSchema<any, any>>(\n schema: T,\n options?: ParseParams,\n): Resolver<z.infer<T>> => async (\n values,\n _,\n validateAllFieldCriteria = false,\n) => {\n const result = schema.safeParse(values, options);\n\n if (result.success) {\n return { values: result.data, errors: {} } as ResolverSuccess<z.infer<T>>;\n }\n\n return {\n values: {},\n errors: transformToNestObject(\n parseErrorSchema(result.error, validateAllFieldCriteria),\n ),\n } as ResolverError<z.infer<T>>;\n};\n"],"names":["parseErrorSchema","zodError","validateAllFieldCriteria","isEmpty","errors","previous","error","path","message","type","code","currentPath","convertArrayToPathName","unionErrors","map","e","push","appendErrors","types","schema","options","values","_","result","safeParse","success","data","transformToNestObject"],"mappings":"skCAWA,IAAMA,EAAmB,SACvBC,EACAC,GAEA,GAAID,EAASE,QACX,MAAO,GAMT,IAHA,MAAMC,YAAaH,EAASG,QACxBC,EAAgC,OAEhBD,kBAAQ,WAAjBE,UACDC,EAA8BD,EAA9BC,KAAMC,EAAwBF,EAAxBE,QAAeC,EAASH,EAAfI,KACjBC,EAAcC,yBAAuBL,GAE3C,GAAI,gBAAiBD,EACnB,cAAwBA,EAAMO,YAAYC,IAAI,SAACC,UAAMA,EAAEX,yBACrDA,EAAOY,WAAPZ,WAIJC,OACKA,EACCE,EACAF,EAASM,IAAgBT,UAEpBS,GAAcM,eACbN,EACAT,EACAG,EACAI,EACAD,cAIDG,GAAcN,EAASM,OACtBH,QAAAA,EACAC,KAAAA,GACIP,EACA,CACEgB,cAAUT,GAAOD,IAAW,MAE9B,OAGV,IAIR,OAAOH,uBAGkB,SACzBc,EACAC,mBAEAC,EACAC,EACApB,YAAAA,IAAAA,GAA2B,OAE3B,IAAMqB,EAASJ,EAAOK,UAAUH,EAAQD,GAExC,uBAAIG,EAAOE,QACF,CAAEJ,OAAQE,EAAOG,KAAMtB,OAAQ,IAGjC,CACLiB,OAAQ,GACRjB,OAAQuB,wBACN3B,EAAiBuB,EAAOjB,MAAOJ,MAjBV"}
|
package/zod/dist/zod.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{transformToNestObject as r,appendErrors as e}from"react-hook-form";import{convertArrayToPathName as t}from"@hookform/resolvers";function n(){return(n=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n])}return r}).apply(this,arguments)}function o(r,e){(null==e||e>r.length)&&(e=r.length);for(var t=0,n=new Array(e);t<e;t++)n[t]=r[t];return n}function a(r,e){var t;if("undefined"==typeof Symbol||null==r[Symbol.iterator]){if(Array.isArray(r)||(t=function(r,e){if(r){if("string"==typeof r)return o(r,e);var t=Object.prototype.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(r):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?o(r,e):void 0}}(r))||e&&r&&"number"==typeof r.length){t&&(r=t);var n=0;return function(){return n>=r.length?{done:!0}:{done:!1,value:r[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(t=r[Symbol.iterator]()).next.bind(t)}var i=function(r,o){if(r.isEmpty)return{};for(var i,u=[].concat(r.errors),s={},c=a(u);!(i=c()).done;){var f,l,p,m=i.value,v=m.path,y=m.message,d=m.code,h=t(v);if("unionErrors"in m)for(var b,g=a(m.unionErrors.map(function(r){return r.errors}));!(b=g()).done;)u.push.apply(u,b.value);s=n({},s,v?s[h]&&o?((f={})[h]=e(h,o,s,d,y),f):((p={})[h]=s[h]||n({message:y,type:d},o?{types:(l={},l[d]=y||!0,l)}:{}),p):{})}return s},u=function(e,t){return function(n,o,a){void 0===a&&(a=!1);try{var u=e.safeParse(n,t);return Promise.resolve(u.success?{values:u.data,errors:{}}:{values:{},errors:r(i(u.error,a))})}catch(r){return Promise.reject(r)}}};export{u as zodResolver};
|
|
2
|
+
//# sourceMappingURL=zod.module.js.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{transformToNestObject as r,appendErrors as o}from"react-hook-form";import{convertArrayToPathName as s}from"@hookform/resolvers";function e(){return(e=Object.assign||function(r){for(var o=1;o<arguments.length;o++){var s=arguments[o];for(var e in s)Object.prototype.hasOwnProperty.call(s,e)&&(r[e]=s[e])}return r}).apply(this,arguments)}const t=(r,t)=>{if(r.isEmpty)return{};const n=[...r.errors];let a={};for(const r of n){const{path:c,message:f,code:p}=r,i=s(c);if("unionErrors"in r)for(const o of r.unionErrors.map(r=>r.errors))n.push(...o);a=e({},a,c?a[i]&&t?{[i]:o(i,t,a,p,f)}:{[i]:a[i]||e({message:f,type:p},t?{types:{[p]:f||!0}}:{})}:{})}return a},n=(o,s)=>async(e,n,a=!1)=>{const c=o.safeParse(e,s);return c.success?{values:c.data,errors:{}}:{values:{},errors:r(t(c.error,a))}};export{n as zodResolver};
|
|
2
|
+
//# sourceMappingURL=zod.modern.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod.modern.js","sources":["../src/zod.ts"],"sourcesContent":["import {\n appendErrors,\n Resolver,\n ResolverError,\n ResolverSuccess,\n transformToNestObject,\n} from 'react-hook-form';\nimport * as z from 'zod';\nimport { ParseParams } from 'zod/lib/src/parser';\nimport { convertArrayToPathName } from '@hookform/resolvers';\n\nconst parseErrorSchema = (\n zodError: z.ZodError,\n validateAllFieldCriteria: boolean,\n) => {\n if (zodError.isEmpty) {\n return {};\n }\n\n const errors = [...zodError.errors];\n let previous: Record<string, any> = {};\n\n for (const error of errors) {\n const { path, message, code: type } = error;\n const currentPath = convertArrayToPathName(path);\n\n if ('unionErrors' in error) {\n for (const subErrors of error.unionErrors.map((e) => e.errors)) {\n errors.push(...subErrors);\n }\n }\n\n previous = {\n ...previous,\n ...(path\n ? previous[currentPath] && validateAllFieldCriteria\n ? {\n [currentPath]: appendErrors(\n currentPath,\n validateAllFieldCriteria,\n previous,\n type,\n message,\n ),\n }\n : {\n [currentPath]: previous[currentPath] || {\n message,\n type,\n ...(validateAllFieldCriteria\n ? {\n types: { [type]: message || true },\n }\n : {}),\n },\n }\n : {}),\n };\n }\n\n return previous;\n};\n\nexport const zodResolver = <T extends z.ZodSchema<any, any>>(\n schema: T,\n options?: ParseParams,\n): Resolver<z.infer<T>> => async (\n values,\n _,\n validateAllFieldCriteria = false,\n) => {\n const result = schema.safeParse(values, options);\n\n if (result.success) {\n return { values: result.data, errors: {} } as ResolverSuccess<z.infer<T>>;\n }\n\n return {\n values: {},\n errors: transformToNestObject(\n parseErrorSchema(result.error, validateAllFieldCriteria),\n ),\n } as ResolverError<z.infer<T>>;\n};\n"],"names":["parseErrorSchema","zodError","validateAllFieldCriteria","isEmpty","errors","previous","error","path","message","code","type","currentPath","convertArrayToPathName","subErrors","unionErrors","map","e","push","[object Object]","appendErrors","types","zodResolver","schema","options","async","values","_","result","safeParse","success","data","transformToNestObject"],"mappings":"sVAWA,MAAMA,EAAmB,CACvBC,EACAC,KAEA,GAAID,EAASE,QACX,MAAO,GAGT,MAAMC,EAAS,IAAIH,EAASG,QAC5B,IAAIC,EAAgC,GAEpC,IAAK,MAAMC,KAASF,EAAQ,CAC1B,MAAMG,KAAEA,EAAFC,QAAQA,EAASC,KAAMC,GAASJ,EAChCK,EAAcC,EAAuBL,GAE3C,GAAI,gBAAiBD,EACnB,IAAK,MAAMO,KAAaP,EAAMQ,YAAYC,IAAKC,GAAMA,EAAEZ,QACrDA,EAAOa,QAAQJ,GAInBR,OACKA,EACCE,EACAF,EAASM,IAAgBT,EACvB,CACEgB,CAACP,GAAcQ,EACbR,EACAT,EACAG,EACAK,EACAF,IAGJ,CACEU,CAACP,GAAcN,EAASM,OACtBH,QAAAA,EACAE,KAAAA,GACIR,EACA,CACEkB,MAAO,CAAEF,CAACR,GAAOF,IAAW,IAE9B,KAGV,IAIR,OAAOH,GAGIgB,EAAc,CACzBC,EACAC,IACyBC,MACzBC,EACAC,EACAxB,GAA2B,KAE3B,MAAMyB,EAASL,EAAOM,UAAUH,EAAQF,GAExC,OAAII,EAAOE,QACF,CAAEJ,OAAQE,EAAOG,KAAM1B,OAAQ,IAGjC,CACLqB,OAAQ,GACRrB,OAAQ2B,EACN/B,EAAiB2B,EAAOrB,MAAOJ"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{transformToNestObject as r,appendErrors as e}from"react-hook-form";import{convertArrayToPathName as t}from"@hookform/resolvers";function n(){return(n=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n])}return r}).apply(this,arguments)}function o(r,e){(null==e||e>r.length)&&(e=r.length);for(var t=0,n=new Array(e);t<e;t++)n[t]=r[t];return n}function a(r,e){var t;if("undefined"==typeof Symbol||null==r[Symbol.iterator]){if(Array.isArray(r)||(t=function(r,e){if(r){if("string"==typeof r)return o(r,e);var t=Object.prototype.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(r):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?o(r,e):void 0}}(r))||e&&r&&"number"==typeof r.length){t&&(r=t);var n=0;return function(){return n>=r.length?{done:!0}:{done:!1,value:r[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(t=r[Symbol.iterator]()).next.bind(t)}var i=function(r,o){if(r.isEmpty)return{};for(var i,u=[].concat(r.errors),s={},c=a(u);!(i=c()).done;){var f,l,p,m=i.value,v=m.path,y=m.message,d=m.code,h=t(v);if("unionErrors"in m)for(var b,g=a(m.unionErrors.map(function(r){return r.errors}));!(b=g()).done;)u.push.apply(u,b.value);s=n({},s,v?s[h]&&o?((f={})[h]=e(h,o,s,d,y),f):((p={})[h]=s[h]||n({message:y,type:d},o?{types:(l={},l[d]=y||!0,l)}:{}),p):{})}return s},u=function(e,t){return function(n,o,a){void 0===a&&(a=!1);try{var u=e.safeParse(n,t);return Promise.resolve(u.success?{values:u.data,errors:{}}:{values:{},errors:r(i(u.error,a))})}catch(r){return Promise.reject(r)}}};export{u as zodResolver};
|
|
2
|
+
//# sourceMappingURL=zod.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod.module.js","sources":["../src/zod.ts"],"sourcesContent":["import {\n appendErrors,\n Resolver,\n ResolverError,\n ResolverSuccess,\n transformToNestObject,\n} from 'react-hook-form';\nimport * as z from 'zod';\nimport { ParseParams } from 'zod/lib/src/parser';\nimport { convertArrayToPathName } from '@hookform/resolvers';\n\nconst parseErrorSchema = (\n zodError: z.ZodError,\n validateAllFieldCriteria: boolean,\n) => {\n if (zodError.isEmpty) {\n return {};\n }\n\n const errors = [...zodError.errors];\n let previous: Record<string, any> = {};\n\n for (const error of errors) {\n const { path, message, code: type } = error;\n const currentPath = convertArrayToPathName(path);\n\n if ('unionErrors' in error) {\n for (const subErrors of error.unionErrors.map((e) => e.errors)) {\n errors.push(...subErrors);\n }\n }\n\n previous = {\n ...previous,\n ...(path\n ? previous[currentPath] && validateAllFieldCriteria\n ? {\n [currentPath]: appendErrors(\n currentPath,\n validateAllFieldCriteria,\n previous,\n type,\n message,\n ),\n }\n : {\n [currentPath]: previous[currentPath] || {\n message,\n type,\n ...(validateAllFieldCriteria\n ? {\n types: { [type]: message || true },\n }\n : {}),\n },\n }\n : {}),\n };\n }\n\n return previous;\n};\n\nexport const zodResolver = <T extends z.ZodSchema<any, any>>(\n schema: T,\n options?: ParseParams,\n): Resolver<z.infer<T>> => async (\n values,\n _,\n validateAllFieldCriteria = false,\n) => {\n const result = schema.safeParse(values, options);\n\n if (result.success) {\n return { values: result.data, errors: {} } as ResolverSuccess<z.infer<T>>;\n }\n\n return {\n values: {},\n errors: transformToNestObject(\n parseErrorSchema(result.error, validateAllFieldCriteria),\n ),\n } as ResolverError<z.infer<T>>;\n};\n"],"names":["parseErrorSchema","zodError","validateAllFieldCriteria","isEmpty","errors","previous","error","path","message","type","code","currentPath","convertArrayToPathName","unionErrors","map","e","push","appendErrors","types","zodResolver","schema","options","values","_","result","safeParse","success","data","transformToNestObject"],"mappings":"2oCAWA,IAAMA,EAAmB,SACvBC,EACAC,GAEA,GAAID,EAASE,QACX,MAAO,GAMT,IAHA,MAAMC,YAAaH,EAASG,QACxBC,EAAgC,OAEhBD,kBAAQ,WAAjBE,UACDC,EAA8BD,EAA9BC,KAAMC,EAAwBF,EAAxBE,QAAeC,EAASH,EAAfI,KACjBC,EAAcC,EAAuBL,GAE3C,GAAI,gBAAiBD,EACnB,cAAwBA,EAAMO,YAAYC,IAAI,SAACC,UAAMA,EAAEX,yBACrDA,EAAOY,WAAPZ,WAIJC,OACKA,EACCE,EACAF,EAASM,IAAgBT,UAEpBS,GAAcM,EACbN,EACAT,EACAG,EACAI,EACAD,cAIDG,GAAcN,EAASM,OACtBH,QAAAA,EACAC,KAAAA,GACIP,EACA,CACEgB,cAAUT,GAAOD,IAAW,MAE9B,OAGV,IAIR,OAAOH,GAGIc,EAAc,SACzBC,EACAC,mBAEAC,EACAC,EACArB,YAAAA,IAAAA,GAA2B,OAE3B,IAAMsB,EAASJ,EAAOK,UAAUH,EAAQD,GAExC,uBAAIG,EAAOE,QACF,CAAEJ,OAAQE,EAAOG,KAAMvB,OAAQ,IAGjC,CACLkB,OAAQ,GACRlB,OAAQwB,EACN5B,EAAiBwB,EAAOlB,MAAOJ,MAjBV"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react-hook-form"),require("@hookform/resolvers")):"function"==typeof define&&define.amd?define(["exports","react-hook-form","@hookform/resolvers"],e):e((r||self).hookformResolversZod={},r.reactHookForm,r.hookformResolvers)}(this,function(r,e,o){function t(){return(t=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var o=arguments[e];for(var t in o)Object.prototype.hasOwnProperty.call(o,t)&&(r[t]=o[t])}return r}).apply(this,arguments)}function n(r,e){(null==e||e>r.length)&&(e=r.length);for(var o=0,t=new Array(e);o<e;o++)t[o]=r[o];return t}function a(r,e){var o;if("undefined"==typeof Symbol||null==r[Symbol.iterator]){if(Array.isArray(r)||(o=function(r,e){if(r){if("string"==typeof r)return n(r,e);var o=Object.prototype.toString.call(r).slice(8,-1);return"Object"===o&&r.constructor&&(o=r.constructor.name),"Map"===o||"Set"===o?Array.from(r):"Arguments"===o||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(o)?n(r,e):void 0}}(r))||e&&r&&"number"==typeof r.length){o&&(r=o);var t=0;return function(){return t>=r.length?{done:!0}:{done:!1,value:r[t++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(o=r[Symbol.iterator]()).next.bind(o)}var s=function(r,n){if(r.isEmpty)return{};for(var s,i=[].concat(r.errors),u={},f=a(i);!(s=f()).done;){var l,c,p,m=s.value,v=m.path,y=m.message,d=m.code,h=o.convertArrayToPathName(v);if("unionErrors"in m)for(var b,g=a(m.unionErrors.map(function(r){return r.errors}));!(b=g()).done;)i.push.apply(i,b.value);u=t({},u,v?u[h]&&n?((l={})[h]=e.appendErrors(h,n,u,d,y),l):((p={})[h]=u[h]||t({message:y,type:d},n?{types:(c={},c[d]=y||!0,c)}:{}),p):{})}return u};r.zodResolver=function(r,o){return function(t,n,a){void 0===a&&(a=!1);try{var i=r.safeParse(t,o);return Promise.resolve(i.success?{values:i.data,errors:{}}:{values:{},errors:e.transformToNestObject(s(i.error,a))})}catch(r){return Promise.reject(r)}}}});
|
|
2
|
+
//# sourceMappingURL=zod.umd.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod.umd.js","sources":["../src/zod.ts"],"sourcesContent":["import {\n appendErrors,\n Resolver,\n ResolverError,\n ResolverSuccess,\n transformToNestObject,\n} from 'react-hook-form';\nimport * as z from 'zod';\nimport { ParseParams } from 'zod/lib/src/parser';\nimport { convertArrayToPathName } from '@hookform/resolvers';\n\nconst parseErrorSchema = (\n zodError: z.ZodError,\n validateAllFieldCriteria: boolean,\n) => {\n if (zodError.isEmpty) {\n return {};\n }\n\n const errors = [...zodError.errors];\n let previous: Record<string, any> = {};\n\n for (const error of errors) {\n const { path, message, code: type } = error;\n const currentPath = convertArrayToPathName(path);\n\n if ('unionErrors' in error) {\n for (const subErrors of error.unionErrors.map((e) => e.errors)) {\n errors.push(...subErrors);\n }\n }\n\n previous = {\n ...previous,\n ...(path\n ? previous[currentPath] && validateAllFieldCriteria\n ? {\n [currentPath]: appendErrors(\n currentPath,\n validateAllFieldCriteria,\n previous,\n type,\n message,\n ),\n }\n : {\n [currentPath]: previous[currentPath] || {\n message,\n type,\n ...(validateAllFieldCriteria\n ? {\n types: { [type]: message || true },\n }\n : {}),\n },\n }\n : {}),\n };\n }\n\n return previous;\n};\n\nexport const zodResolver = <T extends z.ZodSchema<any, any>>(\n schema: T,\n options?: ParseParams,\n): Resolver<z.infer<T>> => async (\n values,\n _,\n validateAllFieldCriteria = false,\n) => {\n const result = schema.safeParse(values, options);\n\n if (result.success) {\n return { values: result.data, errors: {} } as ResolverSuccess<z.infer<T>>;\n }\n\n return {\n values: {},\n errors: transformToNestObject(\n parseErrorSchema(result.error, validateAllFieldCriteria),\n ),\n } as ResolverError<z.infer<T>>;\n};\n"],"names":["parseErrorSchema","zodError","validateAllFieldCriteria","isEmpty","errors","previous","error","path","message","type","code","currentPath","convertArrayToPathName","unionErrors","map","e","push","appendErrors","types","schema","options","values","_","result","safeParse","success","data","transformToNestObject"],"mappings":"43CAWA,IAAMA,EAAmB,SACvBC,EACAC,GAEA,GAAID,EAASE,QACX,MAAO,GAMT,IAHA,MAAMC,YAAaH,EAASG,QACxBC,EAAgC,OAEhBD,kBAAQ,WAAjBE,UACDC,EAA8BD,EAA9BC,KAAMC,EAAwBF,EAAxBE,QAAeC,EAASH,EAAfI,KACjBC,EAAcC,yBAAuBL,GAE3C,GAAI,gBAAiBD,EACnB,cAAwBA,EAAMO,YAAYC,IAAI,SAACC,UAAMA,EAAEX,yBACrDA,EAAOY,WAAPZ,WAIJC,OACKA,EACCE,EACAF,EAASM,IAAgBT,UAEpBS,GAAcM,eACbN,EACAT,EACAG,EACAI,EACAD,cAIDG,GAAcN,EAASM,OACtBH,QAAAA,EACAC,KAAAA,GACIP,EACA,CACEgB,cAAUT,GAAOD,IAAW,MAE9B,OAGV,IAIR,OAAOH,iBAGkB,SACzBc,EACAC,mBAEAC,EACAC,EACApB,YAAAA,IAAAA,GAA2B,OAE3B,IAAMqB,EAASJ,EAAOK,UAAUH,EAAQD,GAExC,uBAAIG,EAAOE,QACF,CAAEJ,OAAQE,EAAOG,KAAMtB,OAAQ,IAGjC,CACLiB,OAAQ,GACRjB,OAAQuB,wBACN3B,EAAiBuB,EAAOjB,MAAOJ,MAjBV"}
|
package/zod/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "zod",
|
|
3
|
+
"amdName": "hookformResolversZod",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"private": true,
|
|
6
|
+
"description": "React Hook Form validation resolver: zod",
|
|
7
|
+
"main": "dist/zod.js",
|
|
8
|
+
"module": "dist/zod.module.js",
|
|
9
|
+
"umd:main": "dist/zod.umd.js",
|
|
10
|
+
"source": "src/index.ts",
|
|
11
|
+
"types": "dist/index.d.ts",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"peerDependencies": {
|
|
14
|
+
"react-hook-form": ">=6.6.0",
|
|
15
|
+
"@hookform/resolvers": "^1.0.0"
|
|
16
|
+
}
|
|
17
|
+
}
|