@hookform/resolvers 2.9.10 → 3.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 +33 -2
- package/ajv/dist/ajv.d.ts +2 -2
- package/ajv/dist/ajv.js +1 -7
- package/ajv/dist/ajv.js.map +1 -1
- package/ajv/dist/ajv.mjs +1 -7
- package/ajv/dist/ajv.modern.mjs +2 -0
- package/ajv/dist/ajv.modern.mjs.map +1 -0
- package/ajv/dist/ajv.module.js +1 -7
- package/ajv/dist/ajv.module.js.map +1 -1
- package/ajv/dist/ajv.umd.js +1 -7
- package/ajv/dist/ajv.umd.js.map +1 -1
- package/ajv/dist/index.d.ts +2 -2
- package/ajv/dist/types.d.ts +5 -5
- package/ajv/package.json +3 -1
- package/ajv/src/__tests__/Form-native-validation.tsx +5 -9
- package/ajv/src/__tests__/Form.tsx +4 -6
- package/ajv/src/__tests__/__fixtures__/data.ts +3 -4
- package/ajv/src/__tests__/__snapshots__/ajv.ts.snap +83 -83
- package/ajv/src/ajv.ts +28 -24
- package/class-validator/dist/class-validator.d.ts +2 -2
- package/class-validator/dist/class-validator.js +1 -1
- package/class-validator/dist/class-validator.js.map +1 -1
- package/class-validator/dist/class-validator.mjs +1 -1
- package/class-validator/dist/class-validator.modern.mjs +2 -0
- package/class-validator/dist/class-validator.modern.mjs.map +1 -0
- package/class-validator/dist/class-validator.module.js +1 -1
- package/class-validator/dist/class-validator.module.js.map +1 -1
- package/class-validator/dist/class-validator.umd.js +1 -1
- package/class-validator/dist/class-validator.umd.js.map +1 -1
- package/class-validator/dist/index.d.ts +2 -2
- package/class-validator/dist/types.d.ts +12 -9
- package/class-validator/src/__tests__/Form-native-validation.tsx +5 -9
- package/class-validator/src/__tests__/Form.tsx +4 -6
- package/class-validator/src/__tests__/__snapshots__/class-validator.ts.snap +102 -67
- package/class-validator/src/__tests__/class-validator.ts +66 -6
- package/class-validator/src/class-validator.ts +6 -4
- package/class-validator/src/types.ts +7 -8
- package/computed-types/dist/computed-types.d.ts +2 -2
- package/computed-types/dist/computed-types.js +1 -1
- package/computed-types/dist/computed-types.js.map +1 -1
- package/computed-types/dist/computed-types.mjs +1 -1
- package/computed-types/dist/computed-types.modern.mjs +2 -0
- package/computed-types/dist/computed-types.modern.mjs.map +1 -0
- package/computed-types/dist/computed-types.module.js +1 -1
- package/computed-types/dist/computed-types.module.js.map +1 -1
- package/computed-types/dist/computed-types.umd.js +1 -1
- package/computed-types/dist/computed-types.umd.js.map +1 -1
- package/computed-types/dist/index.d.ts +2 -2
- package/computed-types/dist/types.d.ts +2 -2
- package/computed-types/src/__tests__/Form-native-validation.tsx +5 -9
- package/computed-types/src/__tests__/Form.tsx +4 -6
- package/computed-types/src/__tests__/__snapshots__/computed-types.ts.snap +21 -21
- package/computed-types/src/__tests__/computed-types.ts +16 -0
- package/computed-types/src/computed-types.ts +12 -5
- package/dist/index.d.ts +2 -2
- package/dist/resolvers.js +1 -1
- package/dist/resolvers.js.map +1 -1
- package/dist/resolvers.mjs +1 -1
- package/dist/resolvers.mjs.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 -2
- package/dist/validateFieldsNatively.d.ts +2 -2
- package/io-ts/dist/arrayToPath.d.ts +3 -3
- package/io-ts/dist/errorsToRecord.d.ts +5 -5
- package/io-ts/dist/index.d.ts +2 -2
- package/io-ts/dist/io-ts.d.ts +2 -2
- package/io-ts/dist/io-ts.js +1 -1
- package/io-ts/dist/io-ts.js.map +1 -1
- package/io-ts/dist/io-ts.mjs +1 -1
- package/io-ts/dist/io-ts.modern.mjs +2 -0
- package/io-ts/dist/io-ts.modern.mjs.map +1 -0
- package/io-ts/dist/io-ts.module.js +1 -1
- package/io-ts/dist/io-ts.module.js.map +1 -1
- package/io-ts/dist/io-ts.umd.js +1 -1
- package/io-ts/dist/io-ts.umd.js.map +1 -1
- package/io-ts/dist/types.d.ts +7 -7
- package/io-ts/src/__tests__/Form-native-validation.tsx +5 -9
- package/io-ts/src/__tests__/Form.tsx +4 -6
- package/io-ts/src/__tests__/__snapshots__/io-ts.ts.snap +28 -28
- package/io-ts/src/__tests__/errorsToRecord.ts +47 -0
- package/io-ts/src/__tests__/io-ts.ts +1 -1
- package/io-ts/src/errorsToRecord.ts +7 -6
- package/joi/dist/index.d.ts +2 -2
- package/joi/dist/joi.d.ts +2 -2
- package/joi/dist/joi.js.map +1 -1
- package/joi/dist/{joi.modern.js → joi.modern.mjs} +1 -1
- package/joi/dist/joi.modern.mjs.map +1 -0
- package/joi/dist/joi.module.js.map +1 -1
- package/joi/dist/joi.umd.js.map +1 -1
- package/joi/dist/types.d.ts +5 -5
- package/joi/src/__tests__/Form-native-validation.tsx +5 -9
- package/joi/src/__tests__/Form.tsx +4 -6
- package/joi/src/__tests__/__snapshots__/joi.ts.snap +87 -87
- package/joi/src/__tests__/joi.ts +8 -8
- package/nope/dist/index.d.ts +2 -2
- package/nope/dist/nope.d.ts +2 -2
- package/nope/dist/nope.js.map +1 -1
- package/nope/dist/{nope.modern.js → nope.modern.mjs} +1 -1
- package/nope/dist/nope.modern.mjs.map +1 -0
- package/nope/dist/nope.module.js.map +1 -1
- package/nope/dist/nope.umd.js.map +1 -1
- package/nope/dist/types.d.ts +9 -9
- package/nope/src/__tests__/Form-native-validation.tsx +5 -9
- package/nope/src/__tests__/Form.tsx +4 -6
- package/nope/src/__tests__/__snapshots__/nope.ts.snap +15 -15
- package/nope/src/__tests__/nope.ts +1 -1
- package/package.json +74 -44
- package/superstruct/dist/index.d.ts +2 -2
- package/superstruct/dist/superstruct.d.ts +2 -2
- 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.mjs +2 -0
- package/superstruct/dist/superstruct.modern.mjs.map +1 -0
- 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 +11 -5
- package/superstruct/src/__tests__/Form-native-validation.tsx +5 -9
- package/superstruct/src/__tests__/Form.tsx +4 -6
- package/superstruct/src/__tests__/__snapshots__/superstruct.ts.snap +19 -19
- package/superstruct/src/__tests__/superstruct.ts +13 -0
- package/superstruct/src/superstruct.ts +4 -3
- package/superstruct/src/types.ts +8 -5
- package/typanion/dist/index.d.ts +2 -2
- package/typanion/dist/typanion.d.ts +2 -2
- package/typanion/dist/typanion.js.map +1 -1
- package/typanion/dist/{typanion.modern.js → typanion.modern.mjs} +1 -1
- package/typanion/dist/typanion.modern.mjs.map +1 -0
- package/typanion/dist/typanion.module.js.map +1 -1
- package/typanion/dist/typanion.umd.js.map +1 -1
- package/typanion/dist/types.d.ts +9 -9
- package/typanion/src/__tests__/Form-native-validation.tsx +7 -10
- package/typanion/src/__tests__/Form.tsx +9 -7
- package/typanion/src/__tests__/__snapshots__/typanion.ts.snap +22 -22
- package/typanion/src/__tests__/typanion.ts +11 -7
- package/typanion/src/typanion.ts +1 -1
- package/typebox/dist/index.d.ts +2 -0
- package/typebox/dist/typebox.d.ts +2 -0
- package/typebox/dist/typebox.js +2 -0
- package/typebox/dist/typebox.js.map +1 -0
- package/typebox/dist/typebox.mjs +2 -0
- package/typebox/dist/typebox.modern.mjs +2 -0
- package/typebox/dist/typebox.modern.mjs.map +1 -0
- package/typebox/dist/typebox.module.js +2 -0
- package/typebox/dist/typebox.module.js.map +1 -0
- package/typebox/dist/typebox.umd.js +2 -0
- package/typebox/dist/typebox.umd.js.map +1 -0
- package/typebox/dist/types.d.ts +3 -0
- package/typebox/package.json +18 -0
- package/typebox/src/__tests__/Form-native-validation.tsx +83 -0
- package/typebox/src/__tests__/Form.tsx +54 -0
- package/typebox/src/__tests__/__fixtures__/data.ts +84 -0
- package/typebox/src/__tests__/__snapshots__/typebox.ts.snap +190 -0
- package/typebox/src/__tests__/typebox.ts +34 -0
- package/typebox/src/index.ts +2 -0
- package/typebox/src/typebox.ts +64 -0
- package/typebox/src/types.ts +10 -0
- package/vest/dist/index.d.ts +2 -1
- package/vest/dist/types.d.ts +8 -0
- package/vest/dist/vest.d.ts +2 -2
- package/vest/dist/vest.js.map +1 -1
- package/vest/dist/{vest.modern.js → vest.modern.mjs} +1 -1
- package/vest/dist/vest.modern.mjs.map +1 -0
- package/vest/dist/vest.module.js.map +1 -1
- package/vest/dist/vest.umd.js.map +1 -1
- package/vest/src/__tests__/Form-native-validation.tsx +5 -9
- package/vest/src/__tests__/Form.tsx +6 -10
- package/vest/src/__tests__/__snapshots__/vest.ts.snap +47 -47
- package/vest/src/index.ts +1 -0
- package/yup/dist/index.d.ts +2 -2
- package/yup/dist/types.d.ts +15 -9
- package/yup/dist/yup.d.ts +2 -2
- 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.mjs +2 -0
- package/yup/dist/yup.modern.mjs.map +1 -0
- 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/src/__tests__/Form-native-validation.tsx +5 -9
- package/yup/src/__tests__/Form.tsx +4 -6
- package/yup/src/__tests__/__fixtures__/data.ts +2 -3
- package/yup/src/__tests__/__snapshots__/yup.ts.snap +100 -87
- package/yup/src/__tests__/yup.ts +49 -18
- package/yup/src/types.ts +14 -11
- package/yup/src/yup.ts +1 -1
- package/zod/dist/index.d.ts +2 -2
- package/zod/dist/types.d.ts +13 -13
- package/zod/dist/zod.d.ts +2 -2
- 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.mjs +2 -0
- package/zod/dist/zod.modern.mjs.map +1 -0
- 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__/Form-native-validation.tsx +5 -9
- package/zod/src/__tests__/Form.tsx +4 -6
- package/zod/src/__tests__/__snapshots__/zod.ts.snap +120 -120
- package/zod/src/__tests__/zod.ts +19 -7
- package/zod/src/types.ts +2 -6
- package/zod/src/zod.ts +27 -27
- package/ajv/dist/ajv.modern.js +0 -8
- package/ajv/dist/ajv.modern.js.map +0 -1
- package/class-validator/dist/class-validator.modern.js +0 -2
- package/class-validator/dist/class-validator.modern.js.map +0 -1
- package/computed-types/dist/computed-types.modern.js +0 -2
- package/computed-types/dist/computed-types.modern.js.map +0 -1
- package/io-ts/dist/io-ts.modern.js +0 -2
- package/io-ts/dist/io-ts.modern.js.map +0 -1
- package/joi/dist/joi.modern.js.map +0 -1
- package/nope/dist/nope.modern.js.map +0 -1
- package/superstruct/dist/superstruct.modern.js +0 -2
- package/superstruct/dist/superstruct.modern.js.map +0 -1
- package/typanion/dist/typanion.modern.js.map +0 -1
- package/vest/dist/vest.modern.js.map +0 -1
- package/yup/dist/yup.modern.js +0 -2
- package/yup/dist/yup.modern.js.map +0 -1
- package/zod/dist/zod.modern.js +0 -2
- package/zod/dist/zod.modern.js.map +0 -1
package/yup/src/__tests__/yup.ts
CHANGED
|
@@ -13,8 +13,8 @@ const shouldUseNativeValidation = false;
|
|
|
13
13
|
|
|
14
14
|
describe('yupResolver', () => {
|
|
15
15
|
it('should return values from yupResolver when validation pass', async () => {
|
|
16
|
-
const schemaSpy =
|
|
17
|
-
const schemaSyncSpy =
|
|
16
|
+
const schemaSpy = vi.spyOn(schema, 'validate');
|
|
17
|
+
const schemaSyncSpy = vi.spyOn(schema, 'validateSync');
|
|
18
18
|
|
|
19
19
|
const result = await yupResolver(schema)(validData, undefined, {
|
|
20
20
|
fields,
|
|
@@ -27,8 +27,8 @@ describe('yupResolver', () => {
|
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
it('should return values from yupResolver with `mode: sync` when validation pass', async () => {
|
|
30
|
-
const validateSyncSpy =
|
|
31
|
-
const validateSpy =
|
|
30
|
+
const validateSyncSpy = vi.spyOn(schema, 'validateSync');
|
|
31
|
+
const validateSpy = vi.spyOn(schema, 'validate');
|
|
32
32
|
|
|
33
33
|
const result = await yupResolver(schema, undefined, {
|
|
34
34
|
mode: 'sync',
|
|
@@ -49,8 +49,8 @@ describe('yupResolver', () => {
|
|
|
49
49
|
});
|
|
50
50
|
|
|
51
51
|
it('should return a single error from yupResolver with `mode: sync` when validation fails', async () => {
|
|
52
|
-
const validateSyncSpy =
|
|
53
|
-
const validateSpy =
|
|
52
|
+
const validateSyncSpy = vi.spyOn(schema, 'validateSync');
|
|
53
|
+
const validateSpy = vi.spyOn(schema, 'validate');
|
|
54
54
|
|
|
55
55
|
const result = await yupResolver(schema, undefined, {
|
|
56
56
|
mode: 'sync',
|
|
@@ -92,12 +92,12 @@ describe('yupResolver', () => {
|
|
|
92
92
|
name: yup
|
|
93
93
|
.string()
|
|
94
94
|
.required()
|
|
95
|
-
.when('$min', (min
|
|
95
|
+
.when('$min', ([min], schema) => {
|
|
96
96
|
return min ? schema.min(6) : schema;
|
|
97
97
|
}),
|
|
98
98
|
});
|
|
99
99
|
|
|
100
|
-
const validateSpy =
|
|
100
|
+
const validateSpy = vi.spyOn(schemaWithContext, 'validate');
|
|
101
101
|
|
|
102
102
|
const result = await yupResolver(schemaWithContext)(data, context, {
|
|
103
103
|
fields,
|
|
@@ -115,7 +115,7 @@ describe('yupResolver', () => {
|
|
|
115
115
|
});
|
|
116
116
|
|
|
117
117
|
it('should show a warning log if yup context is used instead only on dev environment', async () => {
|
|
118
|
-
|
|
118
|
+
vi.spyOn(console, 'warn').mockImplementation(() => vi.fn());
|
|
119
119
|
process.env.NODE_ENV = 'development';
|
|
120
120
|
|
|
121
121
|
await yupResolver(yup.object(), { context: { noContext: true } })(
|
|
@@ -133,7 +133,7 @@ describe('yupResolver', () => {
|
|
|
133
133
|
});
|
|
134
134
|
|
|
135
135
|
it('should not show a warning log if yup context is used instead only on production environment', async () => {
|
|
136
|
-
|
|
136
|
+
vi.spyOn(console, 'warn').mockImplementation(() => vi.fn());
|
|
137
137
|
process.env.NODE_ENV = 'production';
|
|
138
138
|
|
|
139
139
|
await yupResolver(yup.object(), { context: { noContext: true } })(
|
|
@@ -166,7 +166,7 @@ describe('yupResolver', () => {
|
|
|
166
166
|
});
|
|
167
167
|
|
|
168
168
|
it("should merge default yup resolver options with yup's options", async () => {
|
|
169
|
-
const validateSpy =
|
|
169
|
+
const validateSpy = vi.spyOn(schema, 'validate');
|
|
170
170
|
|
|
171
171
|
await yupResolver(schema, { stripUnknown: true })(invalidData, undefined, {
|
|
172
172
|
fields,
|
|
@@ -178,15 +178,46 @@ describe('yupResolver', () => {
|
|
|
178
178
|
);
|
|
179
179
|
});
|
|
180
180
|
|
|
181
|
-
it('should throw an error without inner property', (
|
|
182
|
-
yupResolver(schemaWithWhen)(
|
|
181
|
+
it('should throw an error without inner property', async () => {
|
|
182
|
+
const result = await yupResolver(schemaWithWhen)(
|
|
183
|
+
{ name: 'test', value: '' },
|
|
184
|
+
undefined,
|
|
185
|
+
{
|
|
186
|
+
fields,
|
|
187
|
+
shouldUseNativeValidation,
|
|
188
|
+
},
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
expect(result).toMatchSnapshot();
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it('should throw any error unrelated to Yup', async () => {
|
|
195
|
+
const schemaWithCustomError = schema.transform(() => {
|
|
196
|
+
throw Error('custom error');
|
|
197
|
+
});
|
|
198
|
+
const promise = yupResolver(schemaWithCustomError)(validData, undefined, {
|
|
183
199
|
fields,
|
|
184
200
|
shouldUseNativeValidation,
|
|
185
|
-
}).catch((e) => {
|
|
186
|
-
expect(e).toMatchInlineSnapshot(
|
|
187
|
-
`[TypeError: You cannot \`concat()\` schema's of different types: string and number]`,
|
|
188
|
-
);
|
|
189
|
-
done();
|
|
190
201
|
});
|
|
202
|
+
|
|
203
|
+
await expect(promise).rejects.toThrow('custom error');
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('should return values from yupResolver when validation pass & raw=true', async () => {
|
|
207
|
+
const schemaSpy = vi.spyOn(schema, 'validate');
|
|
208
|
+
const schemaSyncSpy = vi.spyOn(schema, 'validateSync');
|
|
209
|
+
|
|
210
|
+
const result = await yupResolver(schema, undefined, { raw: true })(
|
|
211
|
+
validData,
|
|
212
|
+
undefined,
|
|
213
|
+
{
|
|
214
|
+
fields,
|
|
215
|
+
shouldUseNativeValidation,
|
|
216
|
+
},
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
expect(schemaSpy).toHaveBeenCalledTimes(1);
|
|
220
|
+
expect(schemaSyncSpy).not.toHaveBeenCalled();
|
|
221
|
+
expect(result).toEqual({ errors: {}, values: validData });
|
|
191
222
|
});
|
|
192
223
|
});
|
package/yup/src/types.ts
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
FieldValues,
|
|
3
|
-
ResolverOptions,
|
|
4
|
-
ResolverResult,
|
|
5
|
-
} from 'react-hook-form';
|
|
1
|
+
import { FieldValues, ResolverOptions, ResolverResult } from 'react-hook-form';
|
|
6
2
|
import * as Yup from 'yup';
|
|
7
|
-
import type Lazy from 'yup/lib/Lazy';
|
|
8
3
|
|
|
9
|
-
type Options<T extends Yup.
|
|
10
|
-
T['validate']
|
|
11
|
-
>[1];
|
|
4
|
+
type Options<T extends Yup.ObjectSchema<any>> = Parameters<T['validate']>[1];
|
|
12
5
|
|
|
13
|
-
export type Resolver = <T extends Yup.
|
|
6
|
+
export type Resolver = <T extends Yup.ObjectSchema<any>>(
|
|
14
7
|
schema: T,
|
|
15
8
|
schemaOptions?: Options<T>,
|
|
16
|
-
factoryOptions?: {
|
|
9
|
+
factoryOptions?: {
|
|
10
|
+
/**
|
|
11
|
+
* @default async
|
|
12
|
+
*/
|
|
13
|
+
mode?: 'async' | 'sync';
|
|
14
|
+
/**
|
|
15
|
+
* Return the raw input values rather than the parsed values.
|
|
16
|
+
* @default false
|
|
17
|
+
*/
|
|
18
|
+
raw?: boolean;
|
|
19
|
+
},
|
|
17
20
|
) => <TFieldValues extends FieldValues, TContext>(
|
|
18
21
|
values: TFieldValues,
|
|
19
22
|
context: TContext | undefined,
|
package/yup/src/yup.ts
CHANGED
|
@@ -59,7 +59,7 @@ export const yupResolver: Resolver =
|
|
|
59
59
|
options.shouldUseNativeValidation && validateFieldsNatively({}, options);
|
|
60
60
|
|
|
61
61
|
return {
|
|
62
|
-
values: resolverOptions.
|
|
62
|
+
values: resolverOptions.raw ? values : result,
|
|
63
63
|
errors: {},
|
|
64
64
|
};
|
|
65
65
|
} catch (e: any) {
|
package/zod/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './zod';
|
|
2
|
-
export * from './types';
|
|
1
|
+
export * from './zod';
|
|
2
|
+
export * from './types';
|
package/zod/dist/types.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { FieldValues, ResolverResult, ResolverOptions } from 'react-hook-form';
|
|
2
|
-
import { z } from 'zod';
|
|
3
|
-
export
|
|
4
|
-
/**
|
|
5
|
-
* @default async
|
|
6
|
-
*/
|
|
7
|
-
mode?: 'async' | 'sync';
|
|
8
|
-
/**
|
|
9
|
-
* Return the raw input values rather than the parsed values.
|
|
10
|
-
* @default false
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
}) => <TFieldValues extends FieldValues, TContext>(values: TFieldValues, context: TContext | undefined, options: ResolverOptions<TFieldValues>) => Promise<ResolverResult<TFieldValues>>;
|
|
1
|
+
import { FieldValues, ResolverResult, ResolverOptions } from 'react-hook-form';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
export type Resolver = <T extends z.Schema<any, any>>(schema: T, schemaOptions?: Partial<z.ParseParams>, factoryOptions?: {
|
|
4
|
+
/**
|
|
5
|
+
* @default async
|
|
6
|
+
*/
|
|
7
|
+
mode?: 'async' | 'sync';
|
|
8
|
+
/**
|
|
9
|
+
* Return the raw input values rather than the parsed values.
|
|
10
|
+
* @default false
|
|
11
|
+
*/
|
|
12
|
+
raw?: boolean;
|
|
13
|
+
}) => <TFieldValues extends FieldValues, TContext>(values: TFieldValues, context: TContext | undefined, options: ResolverOptions<TFieldValues>) => Promise<ResolverResult<TFieldValues>>;
|
package/zod/dist/zod.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { Resolver } from './types';
|
|
2
|
-
export declare const zodResolver: Resolver;
|
|
1
|
+
import type { Resolver } from './types';
|
|
2
|
+
export declare const zodResolver: Resolver;
|
package/zod/dist/zod.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var r=require("react-hook-form"),e=require("@hookform/resolvers"),o=function(e,o){for(var
|
|
1
|
+
var r=require("react-hook-form"),e=require("@hookform/resolvers"),o=function(e,o){for(var n={};e.length;){var s=e[0],t=s.code,i=s.message,a=s.path.join(".");if(!n[a])if("unionErrors"in s){var u=s.unionErrors[0].errors[0];n[a]={message:u.message,type:u.code}}else n[a]={message:i,type:t};if("unionErrors"in s&&s.unionErrors.forEach(function(r){return r.errors.forEach(function(r){return e.push(r)})}),o){var c=n[a].types,f=c&&c[s.code];n[a]=r.appendErrors(a,o,n,t,f?[].concat(f,s.message):s.message)}e.shift()}return n};exports.zodResolver=function(r,n,s){return void 0===s&&(s={}),function(t,i,a){try{return Promise.resolve(function(o,i){try{var u=Promise.resolve(r["sync"===s.mode?"parse":"parseAsync"](t,n)).then(function(r){return a.shouldUseNativeValidation&&e.validateFieldsNatively({},a),{errors:{},values:s.raw?t:r}})}catch(r){return i(r)}return u&&u.then?u.then(void 0,i):u}(0,function(r){if(function(r){return null!=r.errors}(r))return{values:{},errors:e.toNestError(o(r.errors,!a.shouldUseNativeValidation&&"all"===a.criteriaMode),a)};throw r}))}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 {
|
|
1
|
+
{"version":3,"file":"zod.js","sources":["../src/zod.ts"],"sourcesContent":["import { appendErrors, FieldError, FieldErrors } from 'react-hook-form';\nimport { z, ZodError } from 'zod';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport type { Resolver } from './types';\n\nconst isZodError = (error: any): error is ZodError => error.errors != null;\n\nconst parseErrorSchema = (\n zodErrors: z.ZodIssue[],\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 if ('unionErrors' in error) {\n const unionError = error.unionErrors[0].errors[0];\n\n errors[_path] = {\n message: unionError.message,\n type: unionError.code,\n };\n } else {\n errors[_path] = { message, type: code };\n }\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 const types = errors[_path].types;\n const messages = types && types[error.code];\n\n errors[_path] = appendErrors(\n _path,\n validateAllFieldCriteria,\n errors,\n code,\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n zodErrors.shift();\n }\n\n return errors;\n};\n\nexport const zodResolver: Resolver =\n (schema, schemaOptions, resolverOptions = {}) =>\n async (values, _, options) => {\n try {\n const data = await schema[\n resolverOptions.mode === 'sync' ? 'parse' : 'parseAsync'\n ](values, schemaOptions);\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n errors: {} as FieldErrors,\n values: resolverOptions.raw ? values : data,\n };\n } catch (error: any) {\n if (isZodError(error)) {\n return {\n values: {},\n errors: toNestError(\n parseErrorSchema(\n error.errors,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n }\n\n throw error;\n }\n };\n"],"names":["parseErrorSchema","zodErrors","validateAllFieldCriteria","errors","length","error","code","message","_path","path","join","unionError","unionErrors","type","forEach","e","push","types","messages","appendErrors","concat","shift","schema","schemaOptions","resolverOptions","values","_","options","Promise","resolve","mode","then","data","shouldUseNativeValidation","validateFieldsNatively","raw","_catch","isZodError","toNestError","criteriaMode","reject"],"mappings":"kEAOMA,EAAmB,SACvBC,EACAC,GAGA,IADA,IAAMC,EAAqC,CAAA,EACpCF,EAAUG,QAAU,CACzB,IAAMC,EAAQJ,EAAU,GAChBK,EAAwBD,EAAxBC,KAAMC,EAAkBF,EAAlBE,QACRC,EAD0BH,EAATI,KACJC,KAAK,KAExB,IAAKP,EAAOK,GACV,GAAI,gBAAiBH,EAAO,CAC1B,IAAMM,EAAaN,EAAMO,YAAY,GAAGT,OAAO,GAE/CA,EAAOK,GAAS,CACdD,QAASI,EAAWJ,QACpBM,KAAMF,EAAWL,KAEpB,MACCH,EAAOK,GAAS,CAAED,QAAAA,EAASM,KAAMP,GAUrC,GANI,gBAAiBD,GACnBA,EAAMO,YAAYE,QAAQ,SAACH,GACzB,OAAAA,EAAWR,OAAOW,QAAQ,SAACC,GAAC,OAAKd,EAAUe,KAAKD,EAAE,EAAC,GAInDb,EAA0B,CAC5B,IAAMe,EAAQd,EAAOK,GAAOS,MACtBC,EAAWD,GAASA,EAAMZ,EAAMC,MAEtCH,EAAOK,GAASW,EAAAA,aACdX,EACAN,EACAC,EACAG,EACAY,EACK,GAAgBE,OAAOF,EAAsBb,EAAME,SACpDF,EAAME,QAEb,CAEDN,EAAUoB,OACX,CAED,OAAOlB,CACT,sBAGE,SAACmB,EAAQC,EAAeC,GACjBC,YADiBD,IAAAA,IAAAA,EAAkB,IACnCC,SAAAA,EAAQC,EAAGC,GAAW,IAAA,OAAAC,QAAAC,gCACvBD,QAAAC,QACiBP,EACQ,SAAzBE,EAAgBM,KAAkB,QAAU,cAC5CL,EAAQF,IAAcQ,KAAA,SAFlBC,GAMN,OAFAL,EAAQM,2BAA6BC,EAAsBA,uBAAC,GAAIP,GAEzD,CACLxB,OAAQ,CAAA,EACRsB,OAAQD,EAAgBW,IAAMV,EAASO,EACvC,4DAXuBI,CACvB,EAWK/B,SAAAA,GACP,GAnEa,SAACA,GAAkC,OAAgB,MAAhBA,EAAMF,MAAc,CAmEhEkC,CAAWhC,GACb,MAAO,CACLoB,OAAQ,CAAA,EACRtB,OAAQmC,EAAWA,YACjBtC,EACEK,EAAMF,QACLwB,EAAQM,2BACkB,QAAzBN,EAAQY,cAEZZ,IAKN,MAAMtB,CACP,GACH,CAAC,MAAAU,GAAA,OAAAa,QAAAY,OAAAzB,EAAA,CAAA,CAAA"}
|
package/zod/dist/zod.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{appendErrors as r}from"react-hook-form";import{validateFieldsNatively as e,toNestError as o}from"@hookform/resolvers";var n=function(e,o){for(var n={};e.length;){var
|
|
1
|
+
import{appendErrors as r}from"react-hook-form";import{validateFieldsNatively as e,toNestError as o}from"@hookform/resolvers";var n=function(e,o){for(var n={};e.length;){var t=e[0],s=t.code,i=t.message,a=t.path.join(".");if(!n[a])if("unionErrors"in t){var u=t.unionErrors[0].errors[0];n[a]={message:u.message,type:u.code}}else n[a]={message:i,type:s};if("unionErrors"in t&&t.unionErrors.forEach(function(r){return r.errors.forEach(function(r){return e.push(r)})}),o){var c=n[a].types,f=c&&c[t.code];n[a]=r(a,o,n,s,f?[].concat(f,t.message):t.message)}e.shift()}return n},t=function(r,t,s){return void 0===s&&(s={}),function(i,a,u){try{return Promise.resolve(function(o,n){try{var a=Promise.resolve(r["sync"===s.mode?"parse":"parseAsync"](i,t)).then(function(r){return u.shouldUseNativeValidation&&e({},u),{errors:{},values:s.raw?i:r}})}catch(r){return n(r)}return a&&a.then?a.then(void 0,n):a}(0,function(r){if(function(r){return null!=r.errors}(r))return{values:{},errors:o(n(r.errors,!u.shouldUseNativeValidation&&"all"===u.criteriaMode),u)};throw r}))}catch(r){return Promise.reject(r)}}};export{t as zodResolver};
|
|
2
2
|
//# sourceMappingURL=zod.module.js.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{appendErrors as r}from"react-hook-form";import{validateFieldsNatively as o,toNestError as e}from"@hookform/resolvers";const s=(o,e)=>{const s={};for(;o.length;){const t=o[0],{code:a,message:n,path:i}=t,c=i.join(".");if(!s[c])if("unionErrors"in t){const r=t.unionErrors[0].errors[0];s[c]={message:r.message,type:r.code}}else s[c]={message:n,type:a};if("unionErrors"in t&&t.unionErrors.forEach(r=>r.errors.forEach(r=>o.push(r))),e){const o=s[c].types,n=o&&o[t.code];s[c]=r(c,e,s,a,n?[].concat(n,t.message):t.message)}o.shift()}return s},t=(r,t,a={})=>async(n,i,c)=>{try{const e=await r["sync"===a.mode?"parse":"parseAsync"](n,t);return c.shouldUseNativeValidation&&o({},c),{errors:{},values:a.raw?n:e}}catch(r){if((r=>null!=r.errors)(r))return{values:{},errors:e(s(r.errors,!c.shouldUseNativeValidation&&"all"===c.criteriaMode),c)};throw r}};export{t as zodResolver};
|
|
2
|
+
//# sourceMappingURL=zod.modern.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod.modern.mjs","sources":["../src/zod.ts"],"sourcesContent":["import { appendErrors, FieldError, FieldErrors } from 'react-hook-form';\nimport { z, ZodError } from 'zod';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport type { Resolver } from './types';\n\nconst isZodError = (error: any): error is ZodError => error.errors != null;\n\nconst parseErrorSchema = (\n zodErrors: z.ZodIssue[],\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 if ('unionErrors' in error) {\n const unionError = error.unionErrors[0].errors[0];\n\n errors[_path] = {\n message: unionError.message,\n type: unionError.code,\n };\n } else {\n errors[_path] = { message, type: code };\n }\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 const types = errors[_path].types;\n const messages = types && types[error.code];\n\n errors[_path] = appendErrors(\n _path,\n validateAllFieldCriteria,\n errors,\n code,\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n zodErrors.shift();\n }\n\n return errors;\n};\n\nexport const zodResolver: Resolver =\n (schema, schemaOptions, resolverOptions = {}) =>\n async (values, _, options) => {\n try {\n const data = await schema[\n resolverOptions.mode === 'sync' ? 'parse' : 'parseAsync'\n ](values, schemaOptions);\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n errors: {} as FieldErrors,\n values: resolverOptions.raw ? values : data,\n };\n } catch (error: any) {\n if (isZodError(error)) {\n return {\n values: {},\n errors: toNestError(\n parseErrorSchema(\n error.errors,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n }\n\n throw error;\n }\n };\n"],"names":["parseErrorSchema","zodErrors","validateAllFieldCriteria","errors","length","error","code","message","path","_path","join","unionError","unionErrors","type","forEach","e","push","types","messages","appendErrors","concat","shift","zodResolver","schema","schemaOptions","resolverOptions","async","values","_","options","data","mode","shouldUseNativeValidation","validateFieldsNatively","raw","isZodError","toNestError","criteriaMode"],"mappings":"6HAKA,MAEMA,EAAmBA,CACvBC,EACAC,KAEA,MAAMC,EAAqC,CAAE,EAC7C,KAAOF,EAAUG,QAAU,CACzB,MAAMC,EAAQJ,EAAU,IAClBK,KAAEA,EAAIC,QAAEA,EAAOC,KAAEA,GAASH,EAC1BI,EAAQD,EAAKE,KAAK,KAExB,IAAKP,EAAOM,GACV,GAAI,gBAAiBJ,EAAO,CAC1B,MAAMM,EAAaN,EAAMO,YAAY,GAAGT,OAAO,GAE/CA,EAAOM,GAAS,CACdF,QAASI,EAAWJ,QACpBM,KAAMF,EAAWL,KAEpB,MACCH,EAAOM,GAAS,CAAEF,UAASM,KAAMP,GAUrC,GANI,gBAAiBD,GACnBA,EAAMO,YAAYE,QAASH,GACzBA,EAAWR,OAAOW,QAASC,GAAMd,EAAUe,KAAKD,KAIhDb,EAA0B,CAC5B,MAAMe,EAAQd,EAAOM,GAAOQ,MACtBC,EAAWD,GAASA,EAAMZ,EAAMC,MAEtCH,EAAOM,GAASU,EACdV,EACAP,EACAC,EACAG,EACAY,EACK,GAAgBE,OAAOF,EAAsBb,EAAME,SACpDF,EAAME,QAEb,CAEDN,EAAUoB,OACX,CAED,OAAOlB,GAGImB,EACXA,CAACC,EAAQC,EAAeC,EAAkB,CAAA,IAC1CC,MAAOC,EAAQC,EAAGC,KAChB,IACE,MAAMC,QAAaP,EACQ,SAAzBE,EAAgBM,KAAkB,QAAU,cAC5CJ,EAAQH,GAIV,OAFAK,EAAQG,2BAA6BC,EAAuB,CAAA,EAAIJ,GAEzD,CACL1B,OAAQ,GACRwB,OAAQF,EAAgBS,IAAMP,EAASG,EAE1C,CAAC,MAAOzB,GACP,GAnEcA,IAAkD,MAAhBA,EAAMF,OAmElDgC,CAAW9B,GACb,MAAO,CACLsB,OAAQ,CAAA,EACRxB,OAAQiC,EACNpC,EACEK,EAAMF,QACL0B,EAAQG,2BACkB,QAAzBH,EAAQQ,cAEZR,IAKN,MAAMxB,CACP"}
|
package/zod/dist/zod.module.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{appendErrors as r}from"react-hook-form";import{validateFieldsNatively as e,toNestError as o}from"@hookform/resolvers";var n=function(e,o){for(var n={};e.length;){var
|
|
1
|
+
import{appendErrors as r}from"react-hook-form";import{validateFieldsNatively as e,toNestError as o}from"@hookform/resolvers";var n=function(e,o){for(var n={};e.length;){var t=e[0],s=t.code,i=t.message,a=t.path.join(".");if(!n[a])if("unionErrors"in t){var u=t.unionErrors[0].errors[0];n[a]={message:u.message,type:u.code}}else n[a]={message:i,type:s};if("unionErrors"in t&&t.unionErrors.forEach(function(r){return r.errors.forEach(function(r){return e.push(r)})}),o){var c=n[a].types,f=c&&c[t.code];n[a]=r(a,o,n,s,f?[].concat(f,t.message):t.message)}e.shift()}return n},t=function(r,t,s){return void 0===s&&(s={}),function(i,a,u){try{return Promise.resolve(function(o,n){try{var a=Promise.resolve(r["sync"===s.mode?"parse":"parseAsync"](i,t)).then(function(r){return u.shouldUseNativeValidation&&e({},u),{errors:{},values:s.raw?i:r}})}catch(r){return n(r)}return a&&a.then?a.then(void 0,n):a}(0,function(r){if(function(r){return null!=r.errors}(r))return{values:{},errors:o(n(r.errors,!u.shouldUseNativeValidation&&"all"===u.criteriaMode),u)};throw r}))}catch(r){return Promise.reject(r)}}};export{t as zodResolver};
|
|
2
2
|
//# sourceMappingURL=zod.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zod.module.js","sources":["../src/zod.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"zod.module.js","sources":["../src/zod.ts"],"sourcesContent":["import { appendErrors, FieldError, FieldErrors } from 'react-hook-form';\nimport { z, ZodError } from 'zod';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport type { Resolver } from './types';\n\nconst isZodError = (error: any): error is ZodError => error.errors != null;\n\nconst parseErrorSchema = (\n zodErrors: z.ZodIssue[],\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 if ('unionErrors' in error) {\n const unionError = error.unionErrors[0].errors[0];\n\n errors[_path] = {\n message: unionError.message,\n type: unionError.code,\n };\n } else {\n errors[_path] = { message, type: code };\n }\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 const types = errors[_path].types;\n const messages = types && types[error.code];\n\n errors[_path] = appendErrors(\n _path,\n validateAllFieldCriteria,\n errors,\n code,\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n zodErrors.shift();\n }\n\n return errors;\n};\n\nexport const zodResolver: Resolver =\n (schema, schemaOptions, resolverOptions = {}) =>\n async (values, _, options) => {\n try {\n const data = await schema[\n resolverOptions.mode === 'sync' ? 'parse' : 'parseAsync'\n ](values, schemaOptions);\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n errors: {} as FieldErrors,\n values: resolverOptions.raw ? values : data,\n };\n } catch (error: any) {\n if (isZodError(error)) {\n return {\n values: {},\n errors: toNestError(\n parseErrorSchema(\n error.errors,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n }\n\n throw error;\n }\n };\n"],"names":["parseErrorSchema","zodErrors","validateAllFieldCriteria","errors","length","error","code","message","_path","path","join","unionError","unionErrors","type","forEach","e","push","types","messages","appendErrors","concat","shift","zodResolver","schema","schemaOptions","resolverOptions","values","_","options","Promise","resolve","mode","then","data","shouldUseNativeValidation","validateFieldsNatively","raw","_catch","isZodError","toNestError","criteriaMode","reject"],"mappings":"6HAKA,IAEMA,EAAmB,SACvBC,EACAC,GAGA,IADA,IAAMC,EAAqC,CAAA,EACpCF,EAAUG,QAAU,CACzB,IAAMC,EAAQJ,EAAU,GAChBK,EAAwBD,EAAxBC,KAAMC,EAAkBF,EAAlBE,QACRC,EAD0BH,EAATI,KACJC,KAAK,KAExB,IAAKP,EAAOK,GACV,GAAI,gBAAiBH,EAAO,CAC1B,IAAMM,EAAaN,EAAMO,YAAY,GAAGT,OAAO,GAE/CA,EAAOK,GAAS,CACdD,QAASI,EAAWJ,QACpBM,KAAMF,EAAWL,KAEpB,MACCH,EAAOK,GAAS,CAAED,QAAAA,EAASM,KAAMP,GAUrC,GANI,gBAAiBD,GACnBA,EAAMO,YAAYE,QAAQ,SAACH,GACzB,OAAAA,EAAWR,OAAOW,QAAQ,SAACC,GAAC,OAAKd,EAAUe,KAAKD,EAAE,EAAC,GAInDb,EAA0B,CAC5B,IAAMe,EAAQd,EAAOK,GAAOS,MACtBC,EAAWD,GAASA,EAAMZ,EAAMC,MAEtCH,EAAOK,GAASW,EACdX,EACAN,EACAC,EACAG,EACAY,EACK,GAAgBE,OAAOF,EAAsBb,EAAME,SACpDF,EAAME,QAEb,CAEDN,EAAUoB,OACX,CAED,OAAOlB,CACT,EAEamB,EACX,SAACC,EAAQC,EAAeC,GACjBC,YADiBD,IAAAA,IAAAA,EAAkB,IACnCC,SAAAA,EAAQC,EAAGC,GAAW,IAAA,OAAAC,QAAAC,gCACvBD,QAAAC,QACiBP,EACQ,SAAzBE,EAAgBM,KAAkB,QAAU,cAC5CL,EAAQF,IAAcQ,KAAA,SAFlBC,GAMN,OAFAL,EAAQM,2BAA6BC,EAAuB,GAAIP,GAEzD,CACLzB,OAAQ,CAAA,EACRuB,OAAQD,EAAgBW,IAAMV,EAASO,EACvC,4DAXuBI,CACvB,EAWKhC,SAAAA,GACP,GAnEa,SAACA,GAAkC,OAAgB,MAAhBA,EAAMF,MAAc,CAmEhEmC,CAAWjC,GACb,MAAO,CACLqB,OAAQ,CAAA,EACRvB,OAAQoC,EACNvC,EACEK,EAAMF,QACLyB,EAAQM,2BACkB,QAAzBN,EAAQY,cAEZZ,IAKN,MAAMvB,CACP,GACH,CAAC,MAAAU,GAAA,OAAAc,QAAAY,OAAA1B,EAAA,CAAA,CAAA"}
|
package/zod/dist/zod.umd.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e
|
|
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){var n=function(r,o){for(var n={};r.length;){var s=r[0],t=s.code,i=s.message,a=s.path.join(".");if(!n[a])if("unionErrors"in s){var u=s.unionErrors[0].errors[0];n[a]={message:u.message,type:u.code}}else n[a]={message:i,type:t};if("unionErrors"in s&&s.unionErrors.forEach(function(e){return e.errors.forEach(function(e){return r.push(e)})}),o){var f=n[a].types,c=f&&f[s.code];n[a]=e.appendErrors(a,o,n,t,c?[].concat(c,s.message):s.message)}r.shift()}return n};r.zodResolver=function(r,e,s){return void 0===s&&(s={}),function(t,i,a){try{return Promise.resolve(function(n,i){try{var u=Promise.resolve(r["sync"===s.mode?"parse":"parseAsync"](t,e)).then(function(r){return a.shouldUseNativeValidation&&o.validateFieldsNatively({},a),{errors:{},values:s.raw?t:r}})}catch(r){return i(r)}return u&&u.then?u.then(void 0,i):u}(0,function(r){if(function(r){return null!=r.errors}(r))return{values:{},errors:o.toNestError(n(r.errors,!a.shouldUseNativeValidation&&"all"===a.criteriaMode),a)};throw r}))}catch(r){return Promise.reject(r)}}}});
|
|
2
2
|
//# sourceMappingURL=zod.umd.js.map
|
package/zod/dist/zod.umd.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zod.umd.js","sources":["../src/zod.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"zod.umd.js","sources":["../src/zod.ts"],"sourcesContent":["import { appendErrors, FieldError, FieldErrors } from 'react-hook-form';\nimport { z, ZodError } from 'zod';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport type { Resolver } from './types';\n\nconst isZodError = (error: any): error is ZodError => error.errors != null;\n\nconst parseErrorSchema = (\n zodErrors: z.ZodIssue[],\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 if ('unionErrors' in error) {\n const unionError = error.unionErrors[0].errors[0];\n\n errors[_path] = {\n message: unionError.message,\n type: unionError.code,\n };\n } else {\n errors[_path] = { message, type: code };\n }\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 const types = errors[_path].types;\n const messages = types && types[error.code];\n\n errors[_path] = appendErrors(\n _path,\n validateAllFieldCriteria,\n errors,\n code,\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n zodErrors.shift();\n }\n\n return errors;\n};\n\nexport const zodResolver: Resolver =\n (schema, schemaOptions, resolverOptions = {}) =>\n async (values, _, options) => {\n try {\n const data = await schema[\n resolverOptions.mode === 'sync' ? 'parse' : 'parseAsync'\n ](values, schemaOptions);\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n errors: {} as FieldErrors,\n values: resolverOptions.raw ? values : data,\n };\n } catch (error: any) {\n if (isZodError(error)) {\n return {\n values: {},\n errors: toNestError(\n parseErrorSchema(\n error.errors,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n }\n\n throw error;\n }\n };\n"],"names":["parseErrorSchema","zodErrors","validateAllFieldCriteria","errors","length","error","code","message","_path","path","join","unionError","unionErrors","type","forEach","e","push","types","messages","appendErrors","concat","shift","schema","schemaOptions","resolverOptions","values","_","options","Promise","resolve","mode","then","data","shouldUseNativeValidation","validateFieldsNatively","raw","_catch","isZodError","toNestError","criteriaMode","reject"],"mappings":"wXAKA,IAEMA,EAAmB,SACvBC,EACAC,GAGA,IADA,IAAMC,EAAqC,CAAA,EACpCF,EAAUG,QAAU,CACzB,IAAMC,EAAQJ,EAAU,GAChBK,EAAwBD,EAAxBC,KAAMC,EAAkBF,EAAlBE,QACRC,EAD0BH,EAATI,KACJC,KAAK,KAExB,IAAKP,EAAOK,GACV,GAAI,gBAAiBH,EAAO,CAC1B,IAAMM,EAAaN,EAAMO,YAAY,GAAGT,OAAO,GAE/CA,EAAOK,GAAS,CACdD,QAASI,EAAWJ,QACpBM,KAAMF,EAAWL,KAEpB,MACCH,EAAOK,GAAS,CAAED,QAAAA,EAASM,KAAMP,GAUrC,GANI,gBAAiBD,GACnBA,EAAMO,YAAYE,QAAQ,SAACH,GACzB,OAAAA,EAAWR,OAAOW,QAAQ,SAACC,GAAC,OAAKd,EAAUe,KAAKD,EAAE,EAAC,GAInDb,EAA0B,CAC5B,IAAMe,EAAQd,EAAOK,GAAOS,MACtBC,EAAWD,GAASA,EAAMZ,EAAMC,MAEtCH,EAAOK,GAASW,EAAAA,aACdX,EACAN,EACAC,EACAG,EACAY,EACK,GAAgBE,OAAOF,EAAsBb,EAAME,SACpDF,EAAME,QAEb,CAEDN,EAAUoB,OACX,CAED,OAAOlB,CACT,gBAGE,SAACmB,EAAQC,EAAeC,GACjBC,YADiBD,IAAAA,IAAAA,EAAkB,IACnCC,SAAAA,EAAQC,EAAGC,GAAW,IAAA,OAAAC,QAAAC,gCACvBD,QAAAC,QACiBP,EACQ,SAAzBE,EAAgBM,KAAkB,QAAU,cAC5CL,EAAQF,IAAcQ,KAAA,SAFlBC,GAMN,OAFAL,EAAQM,2BAA6BC,EAAsBA,uBAAC,GAAIP,GAEzD,CACLxB,OAAQ,CAAA,EACRsB,OAAQD,EAAgBW,IAAMV,EAASO,EACvC,4DAXuBI,CACvB,EAWK/B,SAAAA,GACP,GAnEa,SAACA,GAAkC,OAAgB,MAAhBA,EAAMF,MAAc,CAmEhEkC,CAAWhC,GACb,MAAO,CACLoB,OAAQ,CAAA,EACRtB,OAAQmC,EAAWA,YACjBtC,EACEK,EAAMF,QACLwB,EAAQM,2BACkB,QAAzBN,EAAQY,cAEZZ,IAKN,MAAMtB,CACP,GACH,CAAC,MAAAU,GAAA,OAAAa,QAAAY,OAAAzB,EAAA,CAAA,CAAA"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { useForm } from 'react-hook-form';
|
|
3
|
-
import {
|
|
3
|
+
import { render, screen } from '@testing-library/react';
|
|
4
4
|
import user from '@testing-library/user-event';
|
|
5
5
|
import { z } from 'zod';
|
|
6
6
|
import { zodResolver } from '..';
|
|
@@ -37,7 +37,7 @@ function TestComponent({ onSubmit }: Props) {
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
test("form's native validation with Zod", async () => {
|
|
40
|
-
const handleSubmit =
|
|
40
|
+
const handleSubmit = vi.fn();
|
|
41
41
|
render(<TestComponent onSubmit={handleSubmit} />);
|
|
42
42
|
|
|
43
43
|
// username
|
|
@@ -54,9 +54,7 @@ test("form's native validation with Zod", async () => {
|
|
|
54
54
|
expect(passwordField.validity.valid).toBe(true);
|
|
55
55
|
expect(passwordField.validationMessage).toBe('');
|
|
56
56
|
|
|
57
|
-
await
|
|
58
|
-
user.click(screen.getByText(/submit/i));
|
|
59
|
-
});
|
|
57
|
+
await user.click(screen.getByText(/submit/i));
|
|
60
58
|
|
|
61
59
|
// username
|
|
62
60
|
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement;
|
|
@@ -68,10 +66,8 @@ test("form's native validation with Zod", async () => {
|
|
|
68
66
|
expect(passwordField.validity.valid).toBe(false);
|
|
69
67
|
expect(passwordField.validationMessage).toBe(PASSWORD_REQUIRED_MESSAGE);
|
|
70
68
|
|
|
71
|
-
await
|
|
72
|
-
|
|
73
|
-
user.type(screen.getByPlaceholderText(/password/i), 'password');
|
|
74
|
-
});
|
|
69
|
+
await user.type(screen.getByPlaceholderText(/username/i), 'joe');
|
|
70
|
+
await user.type(screen.getByPlaceholderText(/password/i), 'password');
|
|
75
71
|
|
|
76
72
|
// username
|
|
77
73
|
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import { render, screen
|
|
2
|
+
import { render, screen } from '@testing-library/react';
|
|
3
3
|
import user from '@testing-library/user-event';
|
|
4
4
|
import { useForm } from 'react-hook-form';
|
|
5
5
|
import { z } from 'zod';
|
|
@@ -39,14 +39,12 @@ function TestComponent({ onSubmit }: Props) {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
test("form's validation with Zod and TypeScript's integration", async () => {
|
|
42
|
-
const handleSubmit =
|
|
42
|
+
const handleSubmit = vi.fn();
|
|
43
43
|
render(<TestComponent onSubmit={handleSubmit} />);
|
|
44
44
|
|
|
45
|
-
expect(screen.queryAllByRole(
|
|
45
|
+
expect(screen.queryAllByRole('alert')).toHaveLength(0);
|
|
46
46
|
|
|
47
|
-
await
|
|
48
|
-
user.click(screen.getByText(/submit/i));
|
|
49
|
-
});
|
|
47
|
+
await user.click(screen.getByText(/submit/i));
|
|
50
48
|
|
|
51
49
|
expect(screen.getByText(/username field is required/i)).toBeInTheDocument();
|
|
52
50
|
expect(screen.getByText(/password field is required/i)).toBeInTheDocument();
|