@hookform/resolvers 4.1.3 → 5.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 +30 -0
- package/ajv/package.json +1 -1
- package/arktype/dist/arktype.d.ts +8 -20
- package/arktype/dist/arktype.js +1 -1
- package/arktype/dist/arktype.js.map +1 -1
- package/arktype/dist/arktype.mjs +1 -1
- package/arktype/dist/arktype.modern.mjs +1 -1
- package/arktype/dist/arktype.modern.mjs.map +1 -1
- package/arktype/dist/arktype.module.js +1 -1
- package/arktype/dist/arktype.module.js.map +1 -1
- package/arktype/dist/arktype.umd.js +1 -1
- package/arktype/dist/arktype.umd.js.map +1 -1
- package/arktype/package.json +1 -1
- package/arktype/src/__tests__/Form.tsx +0 -28
- package/arktype/src/__tests__/arktype.ts +56 -0
- package/arktype/src/arktype.ts +58 -22
- package/class-validator/src/__tests__/Form.tsx +0 -26
- package/computed-types/dist/computed-types.d.ts +2 -3
- package/computed-types/dist/computed-types.js.map +1 -1
- package/computed-types/dist/computed-types.modern.mjs.map +1 -1
- package/computed-types/dist/computed-types.module.js.map +1 -1
- package/computed-types/dist/computed-types.umd.js.map +1 -1
- package/computed-types/package.json +1 -1
- package/computed-types/src/__tests__/Form.tsx +0 -28
- package/computed-types/src/__tests__/computed-types.ts +55 -1
- package/computed-types/src/computed-types.ts +7 -5
- package/effect-ts/dist/effect-ts.d.ts +8 -16
- package/effect-ts/dist/effect-ts.js.map +1 -1
- package/effect-ts/dist/effect-ts.modern.mjs.map +1 -1
- package/effect-ts/dist/effect-ts.module.js.map +1 -1
- package/effect-ts/dist/effect-ts.umd.js.map +1 -1
- package/effect-ts/package.json +1 -1
- package/effect-ts/src/__tests__/effect-ts.ts +61 -0
- package/effect-ts/src/effect-ts.ts +21 -3
- package/fluentvalidation-ts/package.json +1 -1
- package/io-ts/dist/io-ts.d.ts +8 -17
- package/io-ts/dist/io-ts.js.map +1 -1
- package/io-ts/dist/io-ts.modern.mjs.map +1 -1
- package/io-ts/dist/io-ts.module.js.map +1 -1
- package/io-ts/dist/io-ts.umd.js.map +1 -1
- package/io-ts/package.json +1 -1
- package/io-ts/src/__tests__/__fixtures__/data.ts +3 -3
- package/io-ts/src/__tests__/io-ts.ts +54 -0
- package/io-ts/src/io-ts.ts +35 -8
- package/joi/package.json +1 -1
- package/nope/package.json +1 -1
- package/package.json +13 -13
- package/standard-schema/dist/standard-schema.d.ts +6 -23
- package/standard-schema/dist/standard-schema.js +1 -1
- package/standard-schema/dist/standard-schema.js.map +1 -1
- package/standard-schema/dist/standard-schema.mjs +1 -1
- package/standard-schema/dist/standard-schema.modern.mjs +1 -1
- package/standard-schema/dist/standard-schema.modern.mjs.map +1 -1
- package/standard-schema/dist/standard-schema.module.js +1 -1
- package/standard-schema/dist/standard-schema.module.js.map +1 -1
- package/standard-schema/dist/standard-schema.umd.js +1 -1
- package/standard-schema/dist/standard-schema.umd.js.map +1 -1
- package/standard-schema/package.json +1 -1
- package/standard-schema/src/__tests__/Form.tsx +0 -28
- package/standard-schema/src/__tests__/__fixtures__/data.ts +2 -2
- package/standard-schema/src/__tests__/standard-schema.ts +78 -1
- package/standard-schema/src/standard-schema.ts +32 -5
- package/superstruct/dist/superstruct.d.ts +6 -20
- package/superstruct/dist/superstruct.js.map +1 -1
- package/superstruct/dist/superstruct.modern.mjs.map +1 -1
- package/superstruct/dist/superstruct.module.js.map +1 -1
- package/superstruct/dist/superstruct.umd.js.map +1 -1
- package/superstruct/package.json +1 -1
- package/superstruct/src/__tests__/superstruct.ts +60 -0
- package/superstruct/src/superstruct.ts +20 -4
- package/typanion/dist/typanion.d.ts +8 -16
- package/typanion/dist/typanion.js.map +1 -1
- package/typanion/dist/typanion.modern.mjs.map +1 -1
- package/typanion/dist/typanion.module.js.map +1 -1
- package/typanion/dist/typanion.umd.js.map +1 -1
- package/typanion/package.json +1 -1
- package/typanion/src/__tests__/Form.tsx +0 -32
- package/typanion/src/__tests__/typanion.ts +49 -0
- package/typanion/src/typanion.ts +22 -4
- package/typebox/dist/typebox.d.ts +3 -3
- package/typebox/dist/typebox.js.map +1 -1
- package/typebox/dist/typebox.modern.mjs.map +1 -1
- package/typebox/dist/typebox.module.js.map +1 -1
- package/typebox/dist/typebox.umd.js.map +1 -1
- package/typebox/package.json +1 -1
- package/typebox/src/__tests__/Form-native-validation-compiler.tsx +1 -1
- package/typebox/src/__tests__/Form.tsx +0 -28
- package/typebox/src/__tests__/__fixtures__/data.ts +3 -3
- package/typebox/src/__tests__/typebox.ts +98 -0
- package/typebox/src/typebox.ts +7 -14
- package/typeschema/dist/index.d.ts +0 -1
- package/typeschema/dist/typeschema.d.ts +8 -19
- package/typeschema/dist/typeschema.js +1 -1
- package/typeschema/dist/typeschema.js.map +1 -1
- package/typeschema/dist/typeschema.mjs +1 -1
- package/typeschema/dist/typeschema.modern.mjs +1 -1
- package/typeschema/dist/typeschema.modern.mjs.map +1 -1
- package/typeschema/dist/typeschema.module.js +1 -1
- package/typeschema/dist/typeschema.module.js.map +1 -1
- package/typeschema/dist/typeschema.umd.js +1 -1
- package/typeschema/dist/typeschema.umd.js.map +1 -1
- package/typeschema/package.json +1 -1
- package/typeschema/src/__tests__/Form.tsx +2 -4
- package/typeschema/src/__tests__/__fixtures__/data.ts +1 -1
- package/typeschema/src/__tests__/typeschema.ts +77 -0
- package/typeschema/src/index.ts +0 -1
- package/typeschema/src/typeschema.ts +52 -25
- package/valibot/dist/valibot.d.ts +9 -30
- package/valibot/dist/valibot.js.map +1 -1
- package/valibot/dist/valibot.modern.mjs.map +1 -1
- package/valibot/dist/valibot.module.js.map +1 -1
- package/valibot/dist/valibot.umd.js.map +1 -1
- package/valibot/package.json +1 -1
- package/valibot/src/__tests__/valibot.ts +69 -3
- package/valibot/src/valibot.ts +29 -13
- package/vest/package.json +1 -1
- package/vine/dist/vine.d.ts +10 -24
- package/vine/dist/vine.js.map +1 -1
- package/vine/dist/vine.modern.mjs.map +1 -1
- package/vine/dist/vine.module.js.map +1 -1
- package/vine/dist/vine.umd.js.map +1 -1
- package/vine/package.json +1 -1
- package/vine/src/__tests__/Form.tsx +0 -28
- package/vine/src/__tests__/__fixtures__/data.ts +4 -4
- package/vine/src/__tests__/vine.ts +69 -0
- package/vine/src/vine.ts +26 -4
- package/yup/dist/yup.d.ts +7 -21
- 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 +1 -1
- package/yup/dist/yup.modern.mjs.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 +1 -1
- package/yup/src/__tests__/yup.ts +66 -0
- package/yup/src/yup.ts +30 -8
- package/zod/dist/zod.d.ts +7 -22
- package/zod/dist/zod.js.map +1 -1
- package/zod/dist/zod.modern.mjs.map +1 -1
- package/zod/dist/zod.module.js.map +1 -1
- package/zod/dist/zod.umd.js.map +1 -1
- package/zod/package.json +1 -1
- package/zod/src/__tests__/Form.tsx +0 -28
- package/zod/src/__tests__/__fixtures__/data.ts +2 -2
- package/zod/src/__tests__/zod.ts +69 -0
- package/zod/src/zod.ts +29 -9
- package/typeschema/dist/types.d.ts +0 -9
- package/typeschema/src/types.ts +0 -18
package/yup/src/__tests__/yup.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type Resolver, type SubmitHandler, useForm } from 'react-hook-form';
|
|
1
2
|
/* eslint-disable no-console, @typescript-eslint/ban-ts-comment */
|
|
2
3
|
import * as yup from 'yup';
|
|
3
4
|
import { yupResolver } from '..';
|
|
@@ -216,4 +217,69 @@ describe('yupResolver', () => {
|
|
|
216
217
|
expect(schemaSyncSpy).not.toHaveBeenCalled();
|
|
217
218
|
expect(result).toEqual({ errors: {}, values: { firstName: 'resolver' } });
|
|
218
219
|
});
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Type inference tests
|
|
223
|
+
*/
|
|
224
|
+
it('should correctly infer the output type from a yup schema', () => {
|
|
225
|
+
const resolver = yupResolver(yup.object({ id: yup.number().required() }));
|
|
226
|
+
|
|
227
|
+
expectTypeOf(resolver).toEqualTypeOf<
|
|
228
|
+
Resolver<{ id: number }, unknown, { id: number }>
|
|
229
|
+
>();
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it('should correctly infer the output type from a yup schema using a transform', () => {
|
|
233
|
+
const resolver = yupResolver(
|
|
234
|
+
yup.object({
|
|
235
|
+
id: yup
|
|
236
|
+
.number()
|
|
237
|
+
.required()
|
|
238
|
+
.transform((val) => String(val)),
|
|
239
|
+
}),
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
// Because Yup is not able to infer the output type from the schema with a transform, the output type remains unchanged
|
|
243
|
+
expectTypeOf(resolver).toEqualTypeOf<
|
|
244
|
+
Resolver<{ id: number }, unknown, { id: number }>
|
|
245
|
+
>();
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it('should correctly infer the output type from a yup schema for the handleSubmit function in useForm', () => {
|
|
249
|
+
const schema = yup.object({ id: yup.number().required() });
|
|
250
|
+
|
|
251
|
+
const form = useForm({
|
|
252
|
+
resolver: yupResolver(schema),
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
|
|
256
|
+
|
|
257
|
+
expectTypeOf(form.handleSubmit).parameter(0).toEqualTypeOf<
|
|
258
|
+
SubmitHandler<{
|
|
259
|
+
id: number;
|
|
260
|
+
}>
|
|
261
|
+
>();
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it('should correctly infer the output type from a yup schema with a transform for the handleSubmit function in useForm', () => {
|
|
265
|
+
const schema = yup.object({
|
|
266
|
+
id: yup
|
|
267
|
+
.number()
|
|
268
|
+
.required()
|
|
269
|
+
.transform((val) => String(val)),
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
const form = useForm({
|
|
273
|
+
resolver: yupResolver(schema),
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
|
|
277
|
+
|
|
278
|
+
// Because Yup is not able to infer the output type from the schema with a transform, the output type remains unchanged
|
|
279
|
+
expectTypeOf(form.handleSubmit).parameter(0).toEqualTypeOf<
|
|
280
|
+
SubmitHandler<{
|
|
281
|
+
id: number;
|
|
282
|
+
}>
|
|
283
|
+
>();
|
|
284
|
+
});
|
|
219
285
|
});
|
package/yup/src/yup.ts
CHANGED
|
@@ -42,6 +42,28 @@ function parseErrorSchema(
|
|
|
42
42
|
);
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
+
export function yupResolver<Input extends FieldValues, Context, Output>(
|
|
46
|
+
schema:
|
|
47
|
+
| Yup.ObjectSchema<Input, any, Output, any>
|
|
48
|
+
| ReturnType<typeof Yup.lazy<Yup.ObjectSchema<Input, any, Output, any>>>,
|
|
49
|
+
schemaOptions?: Parameters<(typeof schema)['validate']>[1],
|
|
50
|
+
resolverOptions?: {
|
|
51
|
+
mode?: 'async' | 'sync';
|
|
52
|
+
raw?: false;
|
|
53
|
+
},
|
|
54
|
+
): Resolver<Input, Context, Yup.InferType<typeof schema>>;
|
|
55
|
+
|
|
56
|
+
export function yupResolver<Input extends FieldValues, Context, Output>(
|
|
57
|
+
schema:
|
|
58
|
+
| Yup.ObjectSchema<Input, any, Output, any>
|
|
59
|
+
| ReturnType<typeof Yup.lazy<Yup.ObjectSchema<Input, any, Output, any>>>,
|
|
60
|
+
schemaOptions: Parameters<(typeof schema)['validate']>[1] | undefined,
|
|
61
|
+
resolverOptions: {
|
|
62
|
+
mode?: 'async' | 'sync';
|
|
63
|
+
raw: true;
|
|
64
|
+
},
|
|
65
|
+
): Resolver<Input, Context, Input>;
|
|
66
|
+
|
|
45
67
|
/**
|
|
46
68
|
* Creates a resolver for react-hook-form using Yup schema validation
|
|
47
69
|
* @param {Yup.ObjectSchema<TFieldValues> | ReturnType<typeof Yup.lazy<Yup.ObjectSchema<TFieldValues>>>} schema - Yup validation schema
|
|
@@ -49,7 +71,7 @@ function parseErrorSchema(
|
|
|
49
71
|
* @param {Object} resolverOptions - Additional resolver configuration
|
|
50
72
|
* @param {('async' | 'sync')} [resolverOptions.mode] - Validation mode
|
|
51
73
|
* @param {boolean} [resolverOptions.raw] - If true, returns raw values instead of validated results
|
|
52
|
-
* @returns {Resolver<Yup.InferType<typeof schema
|
|
74
|
+
* @returns {Resolver<Yup.InferType<typeof schema> | Input>} A resolver function compatible with react-hook-form
|
|
53
75
|
* @example
|
|
54
76
|
* const schema = Yup.object({
|
|
55
77
|
* name: Yup.string().required(),
|
|
@@ -60,19 +82,19 @@ function parseErrorSchema(
|
|
|
60
82
|
* resolver: yupResolver(schema)
|
|
61
83
|
* });
|
|
62
84
|
*/
|
|
63
|
-
export function yupResolver<
|
|
85
|
+
export function yupResolver<Input extends FieldValues, Context, Output>(
|
|
64
86
|
schema:
|
|
65
|
-
| Yup.ObjectSchema<
|
|
66
|
-
| ReturnType<typeof Yup.lazy<Yup.ObjectSchema<
|
|
67
|
-
schemaOptions
|
|
87
|
+
| Yup.ObjectSchema<Input, any, Output, any>
|
|
88
|
+
| ReturnType<typeof Yup.lazy<Yup.ObjectSchema<Input, any, Output, any>>>,
|
|
89
|
+
schemaOptions?: Parameters<(typeof schema)['validate']>[1],
|
|
68
90
|
resolverOptions: {
|
|
69
91
|
mode?: 'async' | 'sync';
|
|
70
92
|
raw?: boolean;
|
|
71
93
|
} = {},
|
|
72
|
-
): Resolver<Yup.InferType<typeof schema
|
|
73
|
-
return async (values, context, options) => {
|
|
94
|
+
): Resolver<Input, Context, Yup.InferType<typeof schema> | Input> {
|
|
95
|
+
return async (values: Input, context, options) => {
|
|
74
96
|
try {
|
|
75
|
-
if (schemaOptions
|
|
97
|
+
if (schemaOptions?.context && process.env.NODE_ENV === 'development') {
|
|
76
98
|
// eslint-disable-next-line no-console
|
|
77
99
|
console.warn(
|
|
78
100
|
"You should not used the yup options context. Please, use the 'useForm' context object instead",
|
package/zod/dist/zod.d.ts
CHANGED
|
@@ -1,25 +1,10 @@
|
|
|
1
1
|
import { FieldValues, Resolver } from 'react-hook-form';
|
|
2
2
|
import { z } from 'zod';
|
|
3
|
-
|
|
4
|
-
* Creates a resolver function for react-hook-form that validates form data using a Zod schema
|
|
5
|
-
* @param {z.ZodSchema<TFieldValues>} schema - The Zod schema used to validate the form data
|
|
6
|
-
* @param {Partial<z.ParseParams>} [schemaOptions] - Optional configuration options for Zod parsing
|
|
7
|
-
* @param {Object} [resolverOptions] - Optional resolver-specific configuration
|
|
8
|
-
* @param {('async'|'sync')} [resolverOptions.mode='async'] - Validation mode. Use 'sync' for synchronous validation
|
|
9
|
-
* @param {boolean} [resolverOptions.raw=false] - If true, returns the raw form values instead of the parsed data
|
|
10
|
-
* @returns {Resolver<z.infer<typeof schema>>} A resolver function compatible with react-hook-form
|
|
11
|
-
* @throws {Error} Throws if validation fails with a non-Zod error
|
|
12
|
-
* @example
|
|
13
|
-
* const schema = z.object({
|
|
14
|
-
* name: z.string().min(2),
|
|
15
|
-
* age: z.number().min(18)
|
|
16
|
-
* });
|
|
17
|
-
*
|
|
18
|
-
* useForm({
|
|
19
|
-
* resolver: zodResolver(schema)
|
|
20
|
-
* });
|
|
21
|
-
*/
|
|
22
|
-
export declare function zodResolver<TFieldValues extends FieldValues>(schema: z.ZodSchema<TFieldValues, any, any>, schemaOptions?: Partial<z.ParseParams>, resolverOptions?: {
|
|
3
|
+
export declare function zodResolver<Input extends FieldValues, Context, Output>(schema: z.ZodSchema<Output, any, Input>, schemaOptions?: Partial<z.ParseParams>, resolverOptions?: {
|
|
23
4
|
mode?: 'async' | 'sync';
|
|
24
|
-
raw?:
|
|
25
|
-
}): Resolver<
|
|
5
|
+
raw?: false;
|
|
6
|
+
}): Resolver<Input, Context, Output>;
|
|
7
|
+
export declare function zodResolver<Input extends FieldValues, Context, Output>(schema: z.ZodSchema<Output, any, Input>, schemaOptions: Partial<z.ParseParams> | undefined, resolverOptions: {
|
|
8
|
+
mode?: 'async' | 'sync';
|
|
9
|
+
raw: true;
|
|
10
|
+
}): Resolver<Input, Context, Input>;
|
package/zod/dist/zod.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zod.js","sources":["../src/zod.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n FieldError,\n FieldErrors,\n FieldValues,\n Resolver,\n appendErrors,\n} from 'react-hook-form';\nimport { ZodError, z } from 'zod';\n\nconst isZodError = (error: any): error is ZodError =>\n Array.isArray(error?.errors);\n\nfunction 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\n/**\n * Creates a resolver function for react-hook-form that validates form data using a Zod schema\n * @param {z.ZodSchema<
|
|
1
|
+
{"version":3,"file":"zod.js","sources":["../src/zod.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n FieldError,\n FieldErrors,\n FieldValues,\n Resolver,\n ResolverError,\n ResolverSuccess,\n appendErrors,\n} from 'react-hook-form';\nimport { ZodError, z } from 'zod';\n\nconst isZodError = (error: any): error is ZodError =>\n Array.isArray(error?.errors);\n\nfunction 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 function zodResolver<Input extends FieldValues, Context, Output>(\n schema: z.ZodSchema<Output, any, Input>,\n schemaOptions?: Partial<z.ParseParams>,\n resolverOptions?: {\n mode?: 'async' | 'sync';\n raw?: false;\n },\n): Resolver<Input, Context, Output>;\n\nexport function zodResolver<Input extends FieldValues, Context, Output>(\n schema: z.ZodSchema<Output, any, Input>,\n schemaOptions: Partial<z.ParseParams> | undefined,\n resolverOptions: {\n mode?: 'async' | 'sync';\n raw: true;\n },\n): Resolver<Input, Context, Input>;\n\n/**\n * Creates a resolver function for react-hook-form that validates form data using a Zod schema\n * @param {z.ZodSchema<Input>} schema - The Zod schema used to validate the form data\n * @param {Partial<z.ParseParams>} [schemaOptions] - Optional configuration options for Zod parsing\n * @param {Object} [resolverOptions] - Optional resolver-specific configuration\n * @param {('async'|'sync')} [resolverOptions.mode='async'] - Validation mode. Use 'sync' for synchronous validation\n * @param {boolean} [resolverOptions.raw=false] - If true, returns the raw form values instead of the parsed data\n * @returns {Resolver<z.output<typeof schema>>} A resolver function compatible with react-hook-form\n * @throws {Error} Throws if validation fails with a non-Zod error\n * @example\n * const schema = z.object({\n * name: z.string().min(2),\n * age: z.number().min(18)\n * });\n *\n * useForm({\n * resolver: zodResolver(schema)\n * });\n */\nexport function zodResolver<Input extends FieldValues, Context, Output>(\n schema: z.ZodSchema<Output, any, Input>,\n schemaOptions?: Partial<z.ParseParams>,\n resolverOptions: {\n mode?: 'async' | 'sync';\n raw?: boolean;\n } = {},\n): Resolver<Input, Context, Output | Input> {\n return async (values: Input, _, 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 ? Object.assign({}, values) : data,\n } satisfies ResolverSuccess<Output | Input>;\n } catch (error) {\n if (isZodError(error)) {\n return {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n error.errors,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n } satisfies ResolverError<Input>;\n }\n\n throw error;\n }\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","Object","assign","_catch","Array","isArray","isZodError","toNestErrors","criteriaMode","reject"],"mappings":"kEAeA,SAASA,EACPC,EACAC,GAGA,IADA,IAAMC,EAAqC,CAAE,EACtCF,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,KAErB,MACEH,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,EAAYA,aAC1BX,EACAN,EACAC,EACAG,EACAY,EACK,GAAgBE,OAAOF,EAAsBb,EAAME,SACpDF,EAAME,QAEd,CAEAN,EAAUoB,OACZ,CAEA,OAAOlB,CACT,qBAuCM,SACJmB,EACAC,EACAC,GAKA,YALAA,IAAAA,IAAAA,EAGI,CAAE,GAEQC,SAAAA,EAAeC,EAAGC,GAAW,IAAA,OAAAC,QAAAC,gCACrCD,QAAAC,QACiBP,EACQ,SAAzBE,EAAgBM,KAAkB,QAAU,cAC5CL,EAAQF,IAAcQ,KAFlBC,SAAAA,GAMN,OAFAL,EAAQM,2BAA6BC,EAAAA,uBAAuB,CAAA,EAAIP,GAEzD,CACLxB,OAAQ,CAAA,EACRsB,OAAQD,EAAgBW,IAAMC,OAAOC,OAAO,CAAE,EAAEZ,GAAUO,EAChB,4DAXLM,CAAA,EAYhCjC,SAAAA,GACP,GA/Ga,SAACA,GAClB,OAAAkC,MAAMC,QAAa,MAALnC,OAAK,EAALA,EAAOF,OAAO,CA8GpBsC,CAAWpC,GACb,MAAO,CACLoB,OAAQ,CAAA,EACRtB,OAAQuC,EAAYA,aAClB1C,EACEK,EAAMF,QACLwB,EAAQM,2BACkB,QAAzBN,EAAQgB,cAEZhB,IAKN,MAAMtB,CACR,GACF,CAAC,MAAAU,GAAAa,OAAAA,QAAAgB,OAAA7B,EACH,CAAA,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zod.modern.mjs","sources":["../src/zod.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n FieldError,\n FieldErrors,\n FieldValues,\n Resolver,\n appendErrors,\n} from 'react-hook-form';\nimport { ZodError, z } from 'zod';\n\nconst isZodError = (error: any): error is ZodError =>\n Array.isArray(error?.errors);\n\nfunction 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\n/**\n * Creates a resolver function for react-hook-form that validates form data using a Zod schema\n * @param {z.ZodSchema<
|
|
1
|
+
{"version":3,"file":"zod.modern.mjs","sources":["../src/zod.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n FieldError,\n FieldErrors,\n FieldValues,\n Resolver,\n ResolverError,\n ResolverSuccess,\n appendErrors,\n} from 'react-hook-form';\nimport { ZodError, z } from 'zod';\n\nconst isZodError = (error: any): error is ZodError =>\n Array.isArray(error?.errors);\n\nfunction 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 function zodResolver<Input extends FieldValues, Context, Output>(\n schema: z.ZodSchema<Output, any, Input>,\n schemaOptions?: Partial<z.ParseParams>,\n resolverOptions?: {\n mode?: 'async' | 'sync';\n raw?: false;\n },\n): Resolver<Input, Context, Output>;\n\nexport function zodResolver<Input extends FieldValues, Context, Output>(\n schema: z.ZodSchema<Output, any, Input>,\n schemaOptions: Partial<z.ParseParams> | undefined,\n resolverOptions: {\n mode?: 'async' | 'sync';\n raw: true;\n },\n): Resolver<Input, Context, Input>;\n\n/**\n * Creates a resolver function for react-hook-form that validates form data using a Zod schema\n * @param {z.ZodSchema<Input>} schema - The Zod schema used to validate the form data\n * @param {Partial<z.ParseParams>} [schemaOptions] - Optional configuration options for Zod parsing\n * @param {Object} [resolverOptions] - Optional resolver-specific configuration\n * @param {('async'|'sync')} [resolverOptions.mode='async'] - Validation mode. Use 'sync' for synchronous validation\n * @param {boolean} [resolverOptions.raw=false] - If true, returns the raw form values instead of the parsed data\n * @returns {Resolver<z.output<typeof schema>>} A resolver function compatible with react-hook-form\n * @throws {Error} Throws if validation fails with a non-Zod error\n * @example\n * const schema = z.object({\n * name: z.string().min(2),\n * age: z.number().min(18)\n * });\n *\n * useForm({\n * resolver: zodResolver(schema)\n * });\n */\nexport function zodResolver<Input extends FieldValues, Context, Output>(\n schema: z.ZodSchema<Output, any, Input>,\n schemaOptions?: Partial<z.ParseParams>,\n resolverOptions: {\n mode?: 'async' | 'sync';\n raw?: boolean;\n } = {},\n): Resolver<Input, Context, Output | Input> {\n return async (values: Input, _, 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 ? Object.assign({}, values) : data,\n } satisfies ResolverSuccess<Output | Input>;\n } catch (error) {\n if (isZodError(error)) {\n return {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n error.errors,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n } satisfies ResolverError<Input>;\n }\n\n throw error;\n }\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","data","mode","shouldUseNativeValidation","validateFieldsNatively","raw","Object","assign","Array","isArray","isZodError","toNestErrors","criteriaMode"],"mappings":"8HAeA,SAASA,EACPC,EACAC,GAEA,MAAMC,EAAqC,GAC3C,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,KAErB,MACEH,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,QAEd,CAEAN,EAAUoB,OACZ,CAEA,OAAOlB,CACT,CAuCgB,SAAAmB,EACdC,EACAC,EACAC,EAGI,IAEJ,OAAcC,MAAAA,EAAeC,EAAGC,KAC9B,IACE,MAAMC,QAAaN,EACQ,SAAzBE,EAAgBK,KAAkB,QAAU,cAC5CJ,EAAQF,GAIV,OAFAI,EAAQG,2BAA6BC,EAAuB,GAAIJ,GAEzD,CACLzB,OAAQ,CAAiB,EACzBuB,OAAQD,EAAgBQ,IAAMC,OAAOC,OAAO,CAAE,EAAET,GAAUG,EAE9D,CAAE,MAAOxB,GACP,GA/GcA,IAClB+B,MAAMC,cAAQhC,SAAAA,EAAOF,QA8GbmC,CAAWjC,GACb,MAAO,CACLqB,OAAQ,CAAA,EACRvB,OAAQoC,EACNvC,EACEK,EAAMF,QACLyB,EAAQG,2BACkB,QAAzBH,EAAQY,cAEZZ,IAKN,MAAMvB,CACR,EAEJ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zod.module.js","sources":["../src/zod.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n FieldError,\n FieldErrors,\n FieldValues,\n Resolver,\n appendErrors,\n} from 'react-hook-form';\nimport { ZodError, z } from 'zod';\n\nconst isZodError = (error: any): error is ZodError =>\n Array.isArray(error?.errors);\n\nfunction 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\n/**\n * Creates a resolver function for react-hook-form that validates form data using a Zod schema\n * @param {z.ZodSchema<
|
|
1
|
+
{"version":3,"file":"zod.module.js","sources":["../src/zod.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n FieldError,\n FieldErrors,\n FieldValues,\n Resolver,\n ResolverError,\n ResolverSuccess,\n appendErrors,\n} from 'react-hook-form';\nimport { ZodError, z } from 'zod';\n\nconst isZodError = (error: any): error is ZodError =>\n Array.isArray(error?.errors);\n\nfunction 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 function zodResolver<Input extends FieldValues, Context, Output>(\n schema: z.ZodSchema<Output, any, Input>,\n schemaOptions?: Partial<z.ParseParams>,\n resolverOptions?: {\n mode?: 'async' | 'sync';\n raw?: false;\n },\n): Resolver<Input, Context, Output>;\n\nexport function zodResolver<Input extends FieldValues, Context, Output>(\n schema: z.ZodSchema<Output, any, Input>,\n schemaOptions: Partial<z.ParseParams> | undefined,\n resolverOptions: {\n mode?: 'async' | 'sync';\n raw: true;\n },\n): Resolver<Input, Context, Input>;\n\n/**\n * Creates a resolver function for react-hook-form that validates form data using a Zod schema\n * @param {z.ZodSchema<Input>} schema - The Zod schema used to validate the form data\n * @param {Partial<z.ParseParams>} [schemaOptions] - Optional configuration options for Zod parsing\n * @param {Object} [resolverOptions] - Optional resolver-specific configuration\n * @param {('async'|'sync')} [resolverOptions.mode='async'] - Validation mode. Use 'sync' for synchronous validation\n * @param {boolean} [resolverOptions.raw=false] - If true, returns the raw form values instead of the parsed data\n * @returns {Resolver<z.output<typeof schema>>} A resolver function compatible with react-hook-form\n * @throws {Error} Throws if validation fails with a non-Zod error\n * @example\n * const schema = z.object({\n * name: z.string().min(2),\n * age: z.number().min(18)\n * });\n *\n * useForm({\n * resolver: zodResolver(schema)\n * });\n */\nexport function zodResolver<Input extends FieldValues, Context, Output>(\n schema: z.ZodSchema<Output, any, Input>,\n schemaOptions?: Partial<z.ParseParams>,\n resolverOptions: {\n mode?: 'async' | 'sync';\n raw?: boolean;\n } = {},\n): Resolver<Input, Context, Output | Input> {\n return async (values: Input, _, 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 ? Object.assign({}, values) : data,\n } satisfies ResolverSuccess<Output | Input>;\n } catch (error) {\n if (isZodError(error)) {\n return {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n error.errors,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n } satisfies ResolverError<Input>;\n }\n\n throw error;\n }\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","Object","assign","_catch","Array","isArray","isZodError","toNestErrors","criteriaMode","reject"],"mappings":"8HAeA,SAASA,EACPC,EACAC,GAGA,IADA,IAAMC,EAAqC,CAAE,EACtCF,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,KAErB,MACEH,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,QAEd,CAEAN,EAAUoB,OACZ,CAEA,OAAOlB,CACT,CAuCM,SAAUmB,EACdC,EACAC,EACAC,GAKA,YALAA,IAAAA,IAAAA,EAGI,CAAE,GAEQC,SAAAA,EAAeC,EAAGC,GAAW,IAAA,OAAAC,QAAAC,gCACrCD,QAAAC,QACiBP,EACQ,SAAzBE,EAAgBM,KAAkB,QAAU,cAC5CL,EAAQF,IAAcQ,KAFlBC,SAAAA,GAMN,OAFAL,EAAQM,2BAA6BC,EAAuB,CAAA,EAAIP,GAEzD,CACLzB,OAAQ,CAAA,EACRuB,OAAQD,EAAgBW,IAAMC,OAAOC,OAAO,CAAE,EAAEZ,GAAUO,EAChB,4DAXLM,CAAA,EAYhClC,SAAAA,GACP,GA/Ga,SAACA,GAClB,OAAAmC,MAAMC,QAAa,MAALpC,OAAK,EAALA,EAAOF,OAAO,CA8GpBuC,CAAWrC,GACb,MAAO,CACLqB,OAAQ,CAAA,EACRvB,OAAQwC,EACN3C,EACEK,EAAMF,QACLyB,EAAQM,2BACkB,QAAzBN,EAAQgB,cAEZhB,IAKN,MAAMvB,CACR,GACF,CAAC,MAAAU,GAAAc,OAAAA,QAAAgB,OAAA9B,EACH,CAAA,CAAA"}
|
package/zod/dist/zod.umd.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zod.umd.js","sources":["../src/zod.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n FieldError,\n FieldErrors,\n FieldValues,\n Resolver,\n appendErrors,\n} from 'react-hook-form';\nimport { ZodError, z } from 'zod';\n\nconst isZodError = (error: any): error is ZodError =>\n Array.isArray(error?.errors);\n\nfunction 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\n/**\n * Creates a resolver function for react-hook-form that validates form data using a Zod schema\n * @param {z.ZodSchema<
|
|
1
|
+
{"version":3,"file":"zod.umd.js","sources":["../src/zod.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n FieldError,\n FieldErrors,\n FieldValues,\n Resolver,\n ResolverError,\n ResolverSuccess,\n appendErrors,\n} from 'react-hook-form';\nimport { ZodError, z } from 'zod';\n\nconst isZodError = (error: any): error is ZodError =>\n Array.isArray(error?.errors);\n\nfunction 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 function zodResolver<Input extends FieldValues, Context, Output>(\n schema: z.ZodSchema<Output, any, Input>,\n schemaOptions?: Partial<z.ParseParams>,\n resolverOptions?: {\n mode?: 'async' | 'sync';\n raw?: false;\n },\n): Resolver<Input, Context, Output>;\n\nexport function zodResolver<Input extends FieldValues, Context, Output>(\n schema: z.ZodSchema<Output, any, Input>,\n schemaOptions: Partial<z.ParseParams> | undefined,\n resolverOptions: {\n mode?: 'async' | 'sync';\n raw: true;\n },\n): Resolver<Input, Context, Input>;\n\n/**\n * Creates a resolver function for react-hook-form that validates form data using a Zod schema\n * @param {z.ZodSchema<Input>} schema - The Zod schema used to validate the form data\n * @param {Partial<z.ParseParams>} [schemaOptions] - Optional configuration options for Zod parsing\n * @param {Object} [resolverOptions] - Optional resolver-specific configuration\n * @param {('async'|'sync')} [resolverOptions.mode='async'] - Validation mode. Use 'sync' for synchronous validation\n * @param {boolean} [resolverOptions.raw=false] - If true, returns the raw form values instead of the parsed data\n * @returns {Resolver<z.output<typeof schema>>} A resolver function compatible with react-hook-form\n * @throws {Error} Throws if validation fails with a non-Zod error\n * @example\n * const schema = z.object({\n * name: z.string().min(2),\n * age: z.number().min(18)\n * });\n *\n * useForm({\n * resolver: zodResolver(schema)\n * });\n */\nexport function zodResolver<Input extends FieldValues, Context, Output>(\n schema: z.ZodSchema<Output, any, Input>,\n schemaOptions?: Partial<z.ParseParams>,\n resolverOptions: {\n mode?: 'async' | 'sync';\n raw?: boolean;\n } = {},\n): Resolver<Input, Context, Output | Input> {\n return async (values: Input, _, 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 ? Object.assign({}, values) : data,\n } satisfies ResolverSuccess<Output | Input>;\n } catch (error) {\n if (isZodError(error)) {\n return {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n error.errors,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n } satisfies ResolverError<Input>;\n }\n\n throw error;\n }\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","Object","assign","_catch","Array","isArray","isZodError","toNestErrors","criteriaMode","reject"],"mappings":"wXAeA,SAASA,EACPC,EACAC,GAGA,IADA,IAAMC,EAAqC,CAAE,EACtCF,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,KAErB,MACEH,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,EAAYA,aAC1BX,EACAN,EACAC,EACAG,EACAY,EACK,GAAgBE,OAAOF,EAAsBb,EAAME,SACpDF,EAAME,QAEd,CAEAN,EAAUoB,OACZ,CAEA,OAAOlB,CACT,eAuCM,SACJmB,EACAC,EACAC,GAKA,YALAA,IAAAA,IAAAA,EAGI,CAAE,GAEQC,SAAAA,EAAeC,EAAGC,GAAW,IAAA,OAAAC,QAAAC,gCACrCD,QAAAC,QACiBP,EACQ,SAAzBE,EAAgBM,KAAkB,QAAU,cAC5CL,EAAQF,IAAcQ,KAFlBC,SAAAA,GAMN,OAFAL,EAAQM,2BAA6BC,EAAAA,uBAAuB,CAAA,EAAIP,GAEzD,CACLxB,OAAQ,CAAA,EACRsB,OAAQD,EAAgBW,IAAMC,OAAOC,OAAO,CAAE,EAAEZ,GAAUO,EAChB,4DAXLM,CAAA,EAYhCjC,SAAAA,GACP,GA/Ga,SAACA,GAClB,OAAAkC,MAAMC,QAAa,MAALnC,OAAK,EAALA,EAAOF,OAAO,CA8GpBsC,CAAWpC,GACb,MAAO,CACLoB,OAAQ,CAAA,EACRtB,OAAQuC,EAAYA,aAClB1C,EACEK,EAAMF,QACLwB,EAAQM,2BACkB,QAAzBN,EAAQgB,cAEZhB,IAKN,MAAMtB,CACR,GACF,CAAC,MAAAU,GAAAa,OAAAA,QAAAgB,OAAA7B,EACH,CAAA,CAAA"}
|
package/zod/package.json
CHANGED
|
@@ -10,8 +10,6 @@ const schema = z.object({
|
|
|
10
10
|
password: z.string().nonempty({ message: 'password field is required' }),
|
|
11
11
|
});
|
|
12
12
|
|
|
13
|
-
type FormData = z.infer<typeof schema> & { unusedProperty: string };
|
|
14
|
-
|
|
15
13
|
function TestComponent({
|
|
16
14
|
onSubmit,
|
|
17
15
|
}: { onSubmit: (data: z.infer<typeof schema>) => void }) {
|
|
@@ -48,29 +46,3 @@ test("form's validation with Zod and TypeScript's integration", async () => {
|
|
|
48
46
|
expect(screen.getByText(/password field is required/i)).toBeInTheDocument();
|
|
49
47
|
expect(handleSubmit).not.toHaveBeenCalled();
|
|
50
48
|
});
|
|
51
|
-
|
|
52
|
-
export function TestComponentManualType({
|
|
53
|
-
onSubmit,
|
|
54
|
-
}: {
|
|
55
|
-
onSubmit: (data: FormData) => void;
|
|
56
|
-
}) {
|
|
57
|
-
const {
|
|
58
|
-
register,
|
|
59
|
-
handleSubmit,
|
|
60
|
-
formState: { errors },
|
|
61
|
-
} = useForm<z.infer<typeof schema>, undefined, FormData>({
|
|
62
|
-
resolver: zodResolver(schema), // Useful to check TypeScript regressions
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
return (
|
|
66
|
-
<form onSubmit={handleSubmit(onSubmit)}>
|
|
67
|
-
<input {...register('username')} />
|
|
68
|
-
{errors.username && <span role="alert">{errors.username.message}</span>}
|
|
69
|
-
|
|
70
|
-
<input {...register('password')} />
|
|
71
|
-
{errors.password && <span role="alert">{errors.password.message}</span>}
|
|
72
|
-
|
|
73
|
-
<button type="submit">submit</button>
|
|
74
|
-
</form>
|
|
75
|
-
);
|
|
76
|
-
}
|
|
@@ -58,7 +58,7 @@ export const validData = {
|
|
|
58
58
|
},
|
|
59
59
|
],
|
|
60
60
|
dateStr: '2020-01-01',
|
|
61
|
-
}
|
|
61
|
+
} satisfies z.input<typeof schema>;
|
|
62
62
|
|
|
63
63
|
export const invalidData = {
|
|
64
64
|
password: '___',
|
|
@@ -66,7 +66,7 @@ export const invalidData = {
|
|
|
66
66
|
birthYear: 'birthYear',
|
|
67
67
|
like: [{ id: 'z' }],
|
|
68
68
|
url: 'abc',
|
|
69
|
-
} as
|
|
69
|
+
} as unknown as z.input<typeof schema>;
|
|
70
70
|
|
|
71
71
|
export const fields: Record<InternalFieldName, Field['_f']> = {
|
|
72
72
|
username: {
|
package/zod/src/__tests__/zod.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Resolver, SubmitHandler, useForm } from 'react-hook-form';
|
|
2
|
+
import { z } from 'zod';
|
|
1
3
|
import { zodResolver } from '..';
|
|
2
4
|
import { fields, invalidData, schema, validData } from './__fixtures__/data';
|
|
3
5
|
|
|
@@ -89,4 +91,71 @@ describe('zodResolver', () => {
|
|
|
89
91
|
|
|
90
92
|
await expect(promise).rejects.toThrow('custom error');
|
|
91
93
|
});
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Type inference tests
|
|
97
|
+
*/
|
|
98
|
+
it('should correctly infer the output type from a zod schema', () => {
|
|
99
|
+
const resolver = zodResolver(z.object({ id: z.number() }));
|
|
100
|
+
|
|
101
|
+
expectTypeOf(resolver).toEqualTypeOf<
|
|
102
|
+
Resolver<{ id: number }, unknown, { id: number }>
|
|
103
|
+
>();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('should correctly infer the output type from a zod schema using a transform', () => {
|
|
107
|
+
const resolver = zodResolver(
|
|
108
|
+
z.object({ id: z.number().transform((val) => String(val)) }),
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
expectTypeOf(resolver).toEqualTypeOf<
|
|
112
|
+
Resolver<{ id: number }, unknown, { id: string }>
|
|
113
|
+
>();
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('should correctly infer the output type from a zod schema when a different input type is specified', () => {
|
|
117
|
+
const schema = z.object({ id: z.number() }).transform(({ id }) => {
|
|
118
|
+
return { id: String(id) };
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
const resolver = zodResolver<{ id: number }, any, z.output<typeof schema>>(
|
|
122
|
+
schema,
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
expectTypeOf(resolver).toEqualTypeOf<
|
|
126
|
+
Resolver<{ id: number }, any, { id: string }>
|
|
127
|
+
>();
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('should correctly infer the output type from a Zod schema for the handleSubmit function in useForm', () => {
|
|
131
|
+
const schema = z.object({ id: z.number() });
|
|
132
|
+
|
|
133
|
+
const form = useForm({
|
|
134
|
+
resolver: zodResolver(schema),
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
|
|
138
|
+
|
|
139
|
+
expectTypeOf(form.handleSubmit).parameter(0).toEqualTypeOf<
|
|
140
|
+
SubmitHandler<{
|
|
141
|
+
id: number;
|
|
142
|
+
}>
|
|
143
|
+
>();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('should correctly infer the output type from a Zod schema with a transform for the handleSubmit function in useForm', () => {
|
|
147
|
+
const schema = z.object({ id: z.number().transform((val) => String(val)) });
|
|
148
|
+
|
|
149
|
+
const form = useForm({
|
|
150
|
+
resolver: zodResolver(schema),
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
|
|
154
|
+
|
|
155
|
+
expectTypeOf(form.handleSubmit).parameter(0).toEqualTypeOf<
|
|
156
|
+
SubmitHandler<{
|
|
157
|
+
id: string;
|
|
158
|
+
}>
|
|
159
|
+
>();
|
|
160
|
+
});
|
|
92
161
|
});
|
package/zod/src/zod.ts
CHANGED
|
@@ -4,6 +4,8 @@ import {
|
|
|
4
4
|
FieldErrors,
|
|
5
5
|
FieldValues,
|
|
6
6
|
Resolver,
|
|
7
|
+
ResolverError,
|
|
8
|
+
ResolverSuccess,
|
|
7
9
|
appendErrors,
|
|
8
10
|
} from 'react-hook-form';
|
|
9
11
|
import { ZodError, z } from 'zod';
|
|
@@ -61,14 +63,32 @@ function parseErrorSchema(
|
|
|
61
63
|
return errors;
|
|
62
64
|
}
|
|
63
65
|
|
|
66
|
+
export function zodResolver<Input extends FieldValues, Context, Output>(
|
|
67
|
+
schema: z.ZodSchema<Output, any, Input>,
|
|
68
|
+
schemaOptions?: Partial<z.ParseParams>,
|
|
69
|
+
resolverOptions?: {
|
|
70
|
+
mode?: 'async' | 'sync';
|
|
71
|
+
raw?: false;
|
|
72
|
+
},
|
|
73
|
+
): Resolver<Input, Context, Output>;
|
|
74
|
+
|
|
75
|
+
export function zodResolver<Input extends FieldValues, Context, Output>(
|
|
76
|
+
schema: z.ZodSchema<Output, any, Input>,
|
|
77
|
+
schemaOptions: Partial<z.ParseParams> | undefined,
|
|
78
|
+
resolverOptions: {
|
|
79
|
+
mode?: 'async' | 'sync';
|
|
80
|
+
raw: true;
|
|
81
|
+
},
|
|
82
|
+
): Resolver<Input, Context, Input>;
|
|
83
|
+
|
|
64
84
|
/**
|
|
65
85
|
* Creates a resolver function for react-hook-form that validates form data using a Zod schema
|
|
66
|
-
* @param {z.ZodSchema<
|
|
86
|
+
* @param {z.ZodSchema<Input>} schema - The Zod schema used to validate the form data
|
|
67
87
|
* @param {Partial<z.ParseParams>} [schemaOptions] - Optional configuration options for Zod parsing
|
|
68
88
|
* @param {Object} [resolverOptions] - Optional resolver-specific configuration
|
|
69
89
|
* @param {('async'|'sync')} [resolverOptions.mode='async'] - Validation mode. Use 'sync' for synchronous validation
|
|
70
90
|
* @param {boolean} [resolverOptions.raw=false] - If true, returns the raw form values instead of the parsed data
|
|
71
|
-
* @returns {Resolver<z.
|
|
91
|
+
* @returns {Resolver<z.output<typeof schema>>} A resolver function compatible with react-hook-form
|
|
72
92
|
* @throws {Error} Throws if validation fails with a non-Zod error
|
|
73
93
|
* @example
|
|
74
94
|
* const schema = z.object({
|
|
@@ -80,15 +100,15 @@ function parseErrorSchema(
|
|
|
80
100
|
* resolver: zodResolver(schema)
|
|
81
101
|
* });
|
|
82
102
|
*/
|
|
83
|
-
export function zodResolver<
|
|
84
|
-
schema: z.ZodSchema<
|
|
103
|
+
export function zodResolver<Input extends FieldValues, Context, Output>(
|
|
104
|
+
schema: z.ZodSchema<Output, any, Input>,
|
|
85
105
|
schemaOptions?: Partial<z.ParseParams>,
|
|
86
106
|
resolverOptions: {
|
|
87
107
|
mode?: 'async' | 'sync';
|
|
88
108
|
raw?: boolean;
|
|
89
109
|
} = {},
|
|
90
|
-
): Resolver<
|
|
91
|
-
return async (values, _, options) => {
|
|
110
|
+
): Resolver<Input, Context, Output | Input> {
|
|
111
|
+
return async (values: Input, _, options) => {
|
|
92
112
|
try {
|
|
93
113
|
const data = await schema[
|
|
94
114
|
resolverOptions.mode === 'sync' ? 'parse' : 'parseAsync'
|
|
@@ -99,8 +119,8 @@ export function zodResolver<TFieldValues extends FieldValues>(
|
|
|
99
119
|
return {
|
|
100
120
|
errors: {} as FieldErrors,
|
|
101
121
|
values: resolverOptions.raw ? Object.assign({}, values) : data,
|
|
102
|
-
}
|
|
103
|
-
} catch (error
|
|
122
|
+
} satisfies ResolverSuccess<Output | Input>;
|
|
123
|
+
} catch (error) {
|
|
104
124
|
if (isZodError(error)) {
|
|
105
125
|
return {
|
|
106
126
|
values: {},
|
|
@@ -112,7 +132,7 @@ export function zodResolver<TFieldValues extends FieldValues>(
|
|
|
112
132
|
),
|
|
113
133
|
options,
|
|
114
134
|
),
|
|
115
|
-
}
|
|
135
|
+
} satisfies ResolverError<Input>;
|
|
116
136
|
}
|
|
117
137
|
|
|
118
138
|
throw error;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { Schema } from '@typeschema/main';
|
|
2
|
-
import { FieldValues, ResolverOptions, ResolverResult } from 'react-hook-form';
|
|
3
|
-
export type Resolver = <T extends Schema>(schema: T, schemaOptions?: never, factoryOptions?: {
|
|
4
|
-
/**
|
|
5
|
-
* Return the raw input values rather than the parsed values.
|
|
6
|
-
* @default false
|
|
7
|
-
*/
|
|
8
|
-
raw?: boolean;
|
|
9
|
-
}) => <TFieldValues extends FieldValues, TContext>(values: TFieldValues, context: TContext | undefined, options: ResolverOptions<TFieldValues>) => Promise<ResolverResult<TFieldValues>>;
|
package/typeschema/src/types.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { Schema } from '@typeschema/main';
|
|
2
|
-
import { FieldValues, ResolverOptions, ResolverResult } from 'react-hook-form';
|
|
3
|
-
|
|
4
|
-
export type Resolver = <T extends Schema>(
|
|
5
|
-
schema: T,
|
|
6
|
-
schemaOptions?: never,
|
|
7
|
-
factoryOptions?: {
|
|
8
|
-
/**
|
|
9
|
-
* Return the raw input values rather than the parsed values.
|
|
10
|
-
* @default false
|
|
11
|
-
*/
|
|
12
|
-
raw?: boolean;
|
|
13
|
-
},
|
|
14
|
-
) => <TFieldValues extends FieldValues, TContext>(
|
|
15
|
-
values: TFieldValues,
|
|
16
|
-
context: TContext | undefined,
|
|
17
|
-
options: ResolverOptions<TFieldValues>,
|
|
18
|
-
) => Promise<ResolverResult<TFieldValues>>;
|