@hookform/resolvers 5.4.2 → 5.4.3
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/arktype/src/__tests__/__snapshots__/arktype.ts.snap +11 -11
- package/arktype/src/__tests__/arktype.ts +15 -6
- package/computed-types/src/__tests__/computed-types.ts +15 -6
- package/effect-ts/src/__tests__/effect-ts.ts +15 -6
- package/io-ts/src/__tests__/io-ts.ts +15 -6
- package/package.json +1 -1
- package/standard-schema/src/__tests__/__snapshots__/standard-schema.ts.snap +31 -0
- package/standard-schema/src/__tests__/standard-schema.ts +21 -13
- package/superstruct/dist/superstruct.d.ts +5 -0
- 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/src/__tests__/superstruct.ts +22 -8
- package/superstruct/src/superstruct.ts +13 -0
- package/typanion/src/__tests__/typanion.ts +8 -3
- package/typebox/src/__tests__/typebox.ts +31 -18
- 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/src/__tests__/typeschema.ts +23 -15
- package/typeschema/src/typeschema.ts +6 -6
- package/valibot/src/__tests__/valibot.ts +15 -6
- package/vine/src/__tests__/vine.ts +15 -6
- package/yup/dist/yup.js.map +1 -1
- package/yup/dist/yup.modern.mjs.map +1 -1
- package/yup/dist/yup.module.js.map +1 -1
- package/yup/dist/yup.umd.js.map +1 -1
- package/yup/src/__tests__/yup.ts +15 -6
- package/yup/src/yup.ts +10 -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 +1 -1
- package/zod/dist/zod.modern.mjs.map +1 -1
- package/zod/dist/zod.module.js +1 -1
- package/zod/dist/zod.module.js.map +1 -1
- package/zod/dist/zod.umd.js +1 -1
- package/zod/dist/zod.umd.js.map +1 -1
- package/zod/src/__tests__/__snapshots__/zod-v3.ts.snap +430 -0
- package/zod/src/__tests__/__snapshots__/zod-v4-mini.ts.snap +472 -0
- package/zod/src/__tests__/__snapshots__/zod-v4.ts.snap +434 -0
- package/zod/src/__tests__/zod-v3.ts +45 -6
- package/zod/src/__tests__/zod-v4-mini.ts +21 -11
- package/zod/src/__tests__/zod-v4.ts +47 -11
- package/zod/src/zod.ts +15 -2
|
@@ -6,41 +6,41 @@ exports[`arktypeResolver > should return a single error from arktypeResolver whe
|
|
|
6
6
|
"accessToken": {
|
|
7
7
|
"message": "accessToken must be a number or a string (was missing)",
|
|
8
8
|
"ref": undefined,
|
|
9
|
-
"type": "
|
|
9
|
+
"type": "",
|
|
10
10
|
},
|
|
11
11
|
"birthYear": {
|
|
12
12
|
"message": "birthYear must be a number (was a string)",
|
|
13
13
|
"ref": undefined,
|
|
14
|
-
"type": "
|
|
14
|
+
"type": "",
|
|
15
15
|
},
|
|
16
16
|
"dateStr": {
|
|
17
17
|
"message": "dateStr must be a Date (was missing)",
|
|
18
18
|
"ref": undefined,
|
|
19
|
-
"type": "
|
|
19
|
+
"type": "",
|
|
20
20
|
},
|
|
21
21
|
"email": {
|
|
22
22
|
"message": "email must be an email address (was "")",
|
|
23
23
|
"ref": {
|
|
24
24
|
"name": "email",
|
|
25
25
|
},
|
|
26
|
-
"type": "
|
|
26
|
+
"type": "",
|
|
27
27
|
},
|
|
28
28
|
"enabled": {
|
|
29
29
|
"message": "enabled must be boolean (was missing)",
|
|
30
30
|
"ref": undefined,
|
|
31
|
-
"type": "
|
|
31
|
+
"type": "",
|
|
32
32
|
},
|
|
33
33
|
"like": [
|
|
34
34
|
{
|
|
35
35
|
"id": {
|
|
36
36
|
"message": "like[0].id must be a number (was a string)",
|
|
37
37
|
"ref": undefined,
|
|
38
|
-
"type": "
|
|
38
|
+
"type": "",
|
|
39
39
|
},
|
|
40
40
|
"name": {
|
|
41
41
|
"message": "like[0].name must be a string (was missing)",
|
|
42
42
|
"ref": undefined,
|
|
43
|
-
"type": "
|
|
43
|
+
"type": "",
|
|
44
44
|
},
|
|
45
45
|
},
|
|
46
46
|
],
|
|
@@ -49,24 +49,24 @@ exports[`arktypeResolver > should return a single error from arktypeResolver whe
|
|
|
49
49
|
"ref": {
|
|
50
50
|
"name": "password",
|
|
51
51
|
},
|
|
52
|
-
"type": "
|
|
52
|
+
"type": "",
|
|
53
53
|
},
|
|
54
54
|
"repeatPassword": {
|
|
55
55
|
"message": "repeatPassword must be a string (was missing)",
|
|
56
56
|
"ref": undefined,
|
|
57
|
-
"type": "
|
|
57
|
+
"type": "",
|
|
58
58
|
},
|
|
59
59
|
"tags": {
|
|
60
60
|
"message": "tags must be an array (was missing)",
|
|
61
61
|
"ref": undefined,
|
|
62
|
-
"type": "
|
|
62
|
+
"type": "",
|
|
63
63
|
},
|
|
64
64
|
"username": {
|
|
65
65
|
"message": "username must be a string (was missing)",
|
|
66
66
|
"ref": {
|
|
67
67
|
"name": "username",
|
|
68
68
|
},
|
|
69
|
-
"type": "
|
|
69
|
+
"type": "",
|
|
70
70
|
},
|
|
71
71
|
},
|
|
72
72
|
"values": {},
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { renderHook } from '@testing-library/react';
|
|
1
2
|
import { type } from 'arktype';
|
|
2
3
|
import { Resolver, useForm } from 'react-hook-form';
|
|
3
4
|
import { SubmitHandler } from 'react-hook-form';
|
|
@@ -51,9 +52,13 @@ describe('arktypeResolver', () => {
|
|
|
51
52
|
it('should correctly infer the output type from a arktype schema for the handleSubmit function in useForm', () => {
|
|
52
53
|
const schema = type({ id: 'number' });
|
|
53
54
|
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
})
|
|
55
|
+
const {
|
|
56
|
+
result: { current: form },
|
|
57
|
+
} = renderHook(() =>
|
|
58
|
+
useForm({
|
|
59
|
+
resolver: arktypeResolver(schema),
|
|
60
|
+
}),
|
|
61
|
+
);
|
|
57
62
|
|
|
58
63
|
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
|
|
59
64
|
|
|
@@ -67,9 +72,13 @@ describe('arktypeResolver', () => {
|
|
|
67
72
|
it('should correctly infer the output type from a arktype schema with a transform for the handleSubmit function in useForm', () => {
|
|
68
73
|
const schema = type({ id: type('string').pipe((s) => Number.parseInt(s)) });
|
|
69
74
|
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
})
|
|
75
|
+
const {
|
|
76
|
+
result: { current: form },
|
|
77
|
+
} = renderHook(() =>
|
|
78
|
+
useForm({
|
|
79
|
+
resolver: arktypeResolver(schema),
|
|
80
|
+
}),
|
|
81
|
+
);
|
|
73
82
|
|
|
74
83
|
expectTypeOf(form.watch('id')).toEqualTypeOf<string>();
|
|
75
84
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { renderHook } from '@testing-library/react';
|
|
1
2
|
import Schema, { number } from 'computed-types';
|
|
2
3
|
import { Resolver, SubmitHandler, useForm } from 'react-hook-form';
|
|
3
4
|
import { computedTypesResolver } from '..';
|
|
@@ -65,9 +66,13 @@ describe('computedTypesResolver', () => {
|
|
|
65
66
|
it('should correctly infer the output type from a computedTypes schema for the handleSubmit function in useForm', () => {
|
|
66
67
|
const schema = Schema({ id: number });
|
|
67
68
|
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
})
|
|
69
|
+
const {
|
|
70
|
+
result: { current: form },
|
|
71
|
+
} = renderHook(() =>
|
|
72
|
+
useForm({
|
|
73
|
+
resolver: computedTypesResolver(schema),
|
|
74
|
+
}),
|
|
75
|
+
);
|
|
71
76
|
|
|
72
77
|
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
|
|
73
78
|
|
|
@@ -81,9 +86,13 @@ describe('computedTypesResolver', () => {
|
|
|
81
86
|
it('should correctly infer the output type from a computedTypes schema with a transform for the handleSubmit function in useForm', () => {
|
|
82
87
|
const schema = Schema({ id: number.transform((val) => String(val)) });
|
|
83
88
|
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
})
|
|
89
|
+
const {
|
|
90
|
+
result: { current: form },
|
|
91
|
+
} = renderHook(() =>
|
|
92
|
+
useForm({
|
|
93
|
+
resolver: computedTypesResolver(schema),
|
|
94
|
+
}),
|
|
95
|
+
);
|
|
87
96
|
|
|
88
97
|
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
|
|
89
98
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { renderHook } from '@testing-library/react';
|
|
1
2
|
import { Schema } from 'effect';
|
|
2
3
|
import { Resolver, useForm } from 'react-hook-form';
|
|
3
4
|
import { SubmitHandler } from 'react-hook-form';
|
|
@@ -114,9 +115,13 @@ describe('effectTsResolver', () => {
|
|
|
114
115
|
it('should correctly infer the output type from a effectTs schema for the handleSubmit function in useForm', () => {
|
|
115
116
|
const schema = Schema.Struct({ id: Schema.Number });
|
|
116
117
|
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
})
|
|
118
|
+
const {
|
|
119
|
+
result: { current: form },
|
|
120
|
+
} = renderHook(() =>
|
|
121
|
+
useForm({
|
|
122
|
+
resolver: effectTsResolver(schema),
|
|
123
|
+
}),
|
|
124
|
+
);
|
|
120
125
|
|
|
121
126
|
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
|
|
122
127
|
|
|
@@ -133,9 +138,13 @@ describe('effectTsResolver', () => {
|
|
|
133
138
|
}),
|
|
134
139
|
});
|
|
135
140
|
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
})
|
|
141
|
+
const {
|
|
142
|
+
result: { current: form },
|
|
143
|
+
} = renderHook(() =>
|
|
144
|
+
useForm({
|
|
145
|
+
resolver: effectTsResolver(schema),
|
|
146
|
+
}),
|
|
147
|
+
);
|
|
139
148
|
|
|
140
149
|
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
|
|
141
150
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { renderHook } from '@testing-library/react';
|
|
1
2
|
import * as t from 'io-ts';
|
|
2
3
|
import * as tt from 'io-ts-types';
|
|
3
4
|
import { Resolver, SubmitHandler, useForm } from 'react-hook-form';
|
|
@@ -60,9 +61,13 @@ describe('ioTsResolver', () => {
|
|
|
60
61
|
it('should correctly infer the output type from a io-ts schema for the handleSubmit function in useForm', () => {
|
|
61
62
|
const schema = t.type({ id: t.number });
|
|
62
63
|
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
})
|
|
64
|
+
const {
|
|
65
|
+
result: { current: form },
|
|
66
|
+
} = renderHook(() =>
|
|
67
|
+
useForm({
|
|
68
|
+
resolver: ioTsResolver(schema),
|
|
69
|
+
}),
|
|
70
|
+
);
|
|
66
71
|
|
|
67
72
|
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
|
|
68
73
|
|
|
@@ -76,9 +81,13 @@ describe('ioTsResolver', () => {
|
|
|
76
81
|
it('should correctly infer the output type from a io-ts schema with a transform for the handleSubmit function in useForm', () => {
|
|
77
82
|
const schema = t.type({ id: tt.NumberFromString });
|
|
78
83
|
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
})
|
|
84
|
+
const {
|
|
85
|
+
result: { current: form },
|
|
86
|
+
} = renderHook(() =>
|
|
87
|
+
useForm({
|
|
88
|
+
resolver: ioTsResolver(schema),
|
|
89
|
+
}),
|
|
90
|
+
);
|
|
82
91
|
|
|
83
92
|
expectTypeOf(form.watch('id')).toEqualTypeOf<string>();
|
|
84
93
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hookform/resolvers",
|
|
3
3
|
"amdName": "hookformResolvers",
|
|
4
|
-
"version": "5.4.
|
|
4
|
+
"version": "5.4.3",
|
|
5
5
|
"description": "React Hook Form validation resolvers: Yup, Joi, Superstruct, Zod, Vest, Class Validator, io-ts, Nope, computed-types, TypeBox, arktype, Typanion, Effect-TS and VineJS",
|
|
6
6
|
"main": "dist/resolvers.js",
|
|
7
7
|
"module": "dist/resolvers.module.js",
|
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
2
|
|
|
3
|
+
exports[`standardSchemaResolver > should collect all root errors when validateAllFieldCriteria is true 1`] = `
|
|
4
|
+
{
|
|
5
|
+
"errors": {
|
|
6
|
+
"root": {
|
|
7
|
+
"message": "Invalid union
|
|
8
|
+
Missing discriminator",
|
|
9
|
+
"ref": undefined,
|
|
10
|
+
"type": "",
|
|
11
|
+
"types": {
|
|
12
|
+
"0": "Invalid union",
|
|
13
|
+
"1": "Missing discriminator",
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
"values": {},
|
|
18
|
+
}
|
|
19
|
+
`;
|
|
20
|
+
|
|
3
21
|
exports[`standardSchemaResolver > should correctly handle path segments that are objects 1`] = `
|
|
4
22
|
{
|
|
5
23
|
"errors": {
|
|
@@ -24,6 +42,19 @@ exports[`standardSchemaResolver > should correctly handle path segments that are
|
|
|
24
42
|
}
|
|
25
43
|
`;
|
|
26
44
|
|
|
45
|
+
exports[`standardSchemaResolver > should return a root error when a standard schema issue has no path 1`] = `
|
|
46
|
+
{
|
|
47
|
+
"errors": {
|
|
48
|
+
"root": {
|
|
49
|
+
"message": "Invalid input",
|
|
50
|
+
"ref": undefined,
|
|
51
|
+
"type": "",
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
"values": {},
|
|
55
|
+
}
|
|
56
|
+
`;
|
|
57
|
+
|
|
27
58
|
exports[`standardSchemaResolver > should return a single error from standardSchemaResolver when validation fails 1`] = `
|
|
28
59
|
{
|
|
29
60
|
"errors": {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { renderHook } from '@testing-library/react';
|
|
1
2
|
import { Resolver, SubmitHandler, useForm } from 'react-hook-form';
|
|
2
3
|
import { z } from 'zod/v3';
|
|
3
4
|
import { standardSchemaResolver } from '..';
|
|
@@ -218,13 +219,16 @@ describe('standardSchemaResolver', () => {
|
|
|
218
219
|
|
|
219
220
|
it('should correctly infer the output type from a standardSchema schema for the handleSubmit function in useForm', () => {
|
|
220
221
|
const schema = z.object({ id: z.number() });
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
222
|
+
const {
|
|
223
|
+
result: { current: form },
|
|
224
|
+
} = renderHook(() =>
|
|
225
|
+
useForm({
|
|
226
|
+
resolver: standardSchemaResolver(schema),
|
|
227
|
+
defaultValues: {
|
|
228
|
+
id: 3,
|
|
229
|
+
},
|
|
230
|
+
}),
|
|
231
|
+
);
|
|
228
232
|
|
|
229
233
|
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
|
|
230
234
|
|
|
@@ -238,12 +242,16 @@ describe('standardSchemaResolver', () => {
|
|
|
238
242
|
it('should correctly infer the output type from a standardSchema schema with a transform for the handleSubmit function in useForm', () => {
|
|
239
243
|
const schema = z.object({ id: z.number().transform((val) => String(val)) });
|
|
240
244
|
|
|
241
|
-
const
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
245
|
+
const {
|
|
246
|
+
result: { current: form },
|
|
247
|
+
} = renderHook(() =>
|
|
248
|
+
useForm({
|
|
249
|
+
resolver: standardSchemaResolver(schema),
|
|
250
|
+
defaultValues: {
|
|
251
|
+
id: 3,
|
|
252
|
+
},
|
|
253
|
+
}),
|
|
254
|
+
);
|
|
247
255
|
|
|
248
256
|
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
|
|
249
257
|
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { FieldValues, Resolver } from 'react-hook-form';
|
|
2
2
|
import { Infer, Struct, validate } from 'superstruct';
|
|
3
|
+
export declare function superstructResolver<Input extends FieldValues, Context, Output>(schema: Struct<Output, any>, schemaOptions: Omit<Parameters<typeof validate>[2], 'coerce'> & {
|
|
4
|
+
coerce: true;
|
|
5
|
+
}, resolverOptions?: {
|
|
6
|
+
raw?: boolean;
|
|
7
|
+
}): Resolver<Input, Context, Output>;
|
|
3
8
|
export declare function superstructResolver<Input extends FieldValues, Context, Output>(schema: Struct<Input, any>, schemaOptions?: Parameters<typeof validate>[2], resolverOptions?: {
|
|
4
9
|
raw?: false;
|
|
5
10
|
}): Resolver<Input, Context, Infer<typeof schema>>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"superstruct.js","sources":["../src/superstruct.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError, FieldValues, Resolver } from 'react-hook-form';\nimport { Infer, Struct, StructError, validate } from 'superstruct';\n\nfunction parseErrorSchema(error: StructError) {\n return error.failures().reduce<Record<string, FieldError>>(\n (previous, error) =>\n (previous[error.path.join('.')] = {\n message: error.message,\n type: error.type,\n }) && previous,\n {},\n );\n}\n\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions?: {\n raw?: false;\n },\n): Resolver<Input, Context, Infer<typeof schema>>;\n\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions: Parameters<typeof validate>[2] | undefined,\n resolverOptions: {\n raw: true;\n },\n): Resolver<Input, Context, Input>;\n\n/**\n * Creates a resolver for react-hook-form using Superstruct schema validation\n * @param {Struct<TFieldValues, any>} schema - The Superstruct schema to validate against\n * @param {Parameters<typeof validate>[2]} [schemaOptions] - Optional Superstruct validation options\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {boolean} [resolverOptions.raw=false] - If true, returns raw values rather than validated results\n * @returns {Resolver<Infer<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = struct({\n * name: string(),\n * age: number()\n * });\n *\n * useForm({\n * resolver: superstructResolver(schema)\n * });\n */\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions: {\n raw?: boolean;\n } = {},\n): Resolver<Input, Context, Input | Output> {\n return (values: Input, _, options) => {\n const result = validate(values, schema, schemaOptions);\n\n if (result[0]) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(result[0]), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? Object.assign({}, values) : result[1],\n errors: {},\n };\n };\n}\n"],"names":["schema","schemaOptions","resolverOptions","values","_","options","error","result","validate","errors","toNestErrors","failures","reduce","previous","path","join","message","type","shouldUseNativeValidation","validateFieldsNatively","raw","Object","assign"],"mappings":"
|
|
1
|
+
{"version":3,"file":"superstruct.js","sources":["../src/superstruct.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError, FieldValues, Resolver } from 'react-hook-form';\nimport { Infer, Struct, StructError, validate } from 'superstruct';\n\nfunction parseErrorSchema(error: StructError) {\n return error.failures().reduce<Record<string, FieldError>>(\n (previous, error) =>\n (previous[error.path.join('.')] = {\n message: error.message,\n type: error.type,\n }) && previous,\n {},\n );\n}\n\n// This is required to correctly type the input of the returned function when coerce is true\n// superstruct does not store the input type of a schema alongside it\n// Infer<typeof schema> gives the output type of the coercion\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Output, any>,\n schemaOptions: Omit<Parameters<typeof validate>[2], 'coerce'> & {\n coerce: true;\n },\n resolverOptions?: {\n raw?: boolean;\n },\n): Resolver<Input, Context, Output>;\n\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions?: {\n raw?: false;\n },\n): Resolver<Input, Context, Infer<typeof schema>>;\n\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions: Parameters<typeof validate>[2] | undefined,\n resolverOptions: {\n raw: true;\n },\n): Resolver<Input, Context, Input>;\n\n/**\n * Creates a resolver for react-hook-form using Superstruct schema validation\n * @param {Struct<TFieldValues, any>} schema - The Superstruct schema to validate against\n * @param {Parameters<typeof validate>[2]} [schemaOptions] - Optional Superstruct validation options\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {boolean} [resolverOptions.raw=false] - If true, returns raw values rather than validated results\n * @returns {Resolver<Infer<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = struct({\n * name: string(),\n * age: number()\n * });\n *\n * useForm({\n * resolver: superstructResolver(schema)\n * });\n */\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions: {\n raw?: boolean;\n } = {},\n): Resolver<Input, Context, Input | Output> {\n return (values: Input, _, options) => {\n const result = validate(values, schema, schemaOptions);\n\n if (result[0]) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(result[0]), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? Object.assign({}, values) : result[1],\n errors: {},\n };\n };\n}\n"],"names":["schema","schemaOptions","resolverOptions","values","_","options","error","result","validate","errors","toNestErrors","failures","reduce","previous","path","join","message","type","shouldUseNativeValidation","validateFieldsNatively","raw","Object","assign"],"mappings":"mGA8DEA,EACAC,EACAC,GAIA,gBAJAA,IAAAA,EAEI,CAAE,GAEEC,SAAAA,EAAeC,EAAGC,GACxB,IAjEsBC,EAiEhBC,EAASC,EAAAA,SAASL,EAAQH,EAAQC,GAExC,OAAIM,EAAO,GACF,CACLJ,OAAQ,CAAA,EACRM,OAAQC,EAAYA,cAtEFJ,EAsEoBC,EAAO,GArE5CD,EAAMK,WAAWC,OACtB,SAACC,EAAUP,GACT,OAACO,EAASP,EAAMQ,KAAKC,KAAK,MAAQ,CAChCC,QAASV,EAAMU,QACfC,KAAMX,EAAMW,QACRJ,CAAQ,EAChB,CAAA,IA+DsDR,KAItDA,EAAQa,2BAA6BC,yBAAuB,CAAA,EAAId,GAEzD,CACLF,OAAQD,EAAgBkB,IAAMC,OAAOC,OAAO,CAAE,EAAEnB,GAAUI,EAAO,GACjEE,OAAQ,CAAA,GAEZ,CACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"superstruct.modern.mjs","sources":["../src/superstruct.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError, FieldValues, Resolver } from 'react-hook-form';\nimport { Infer, Struct, StructError, validate } from 'superstruct';\n\nfunction parseErrorSchema(error: StructError) {\n return error.failures().reduce<Record<string, FieldError>>(\n (previous, error) =>\n (previous[error.path.join('.')] = {\n message: error.message,\n type: error.type,\n }) && previous,\n {},\n );\n}\n\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions?: {\n raw?: false;\n },\n): Resolver<Input, Context, Infer<typeof schema>>;\n\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions: Parameters<typeof validate>[2] | undefined,\n resolverOptions: {\n raw: true;\n },\n): Resolver<Input, Context, Input>;\n\n/**\n * Creates a resolver for react-hook-form using Superstruct schema validation\n * @param {Struct<TFieldValues, any>} schema - The Superstruct schema to validate against\n * @param {Parameters<typeof validate>[2]} [schemaOptions] - Optional Superstruct validation options\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {boolean} [resolverOptions.raw=false] - If true, returns raw values rather than validated results\n * @returns {Resolver<Infer<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = struct({\n * name: string(),\n * age: number()\n * });\n *\n * useForm({\n * resolver: superstructResolver(schema)\n * });\n */\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions: {\n raw?: boolean;\n } = {},\n): Resolver<Input, Context, Input | Output> {\n return (values: Input, _, options) => {\n const result = validate(values, schema, schemaOptions);\n\n if (result[0]) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(result[0]), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? Object.assign({}, values) : result[1],\n errors: {},\n };\n };\n}\n"],"names":["superstructResolver","schema","schemaOptions","resolverOptions","values","_","options","result","validate","errors","toNestErrors","error","failures","reduce","previous","path","join","message","type","shouldUseNativeValidation","validateFieldsNatively","raw","Object","assign"],"mappings":"
|
|
1
|
+
{"version":3,"file":"superstruct.modern.mjs","sources":["../src/superstruct.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError, FieldValues, Resolver } from 'react-hook-form';\nimport { Infer, Struct, StructError, validate } from 'superstruct';\n\nfunction parseErrorSchema(error: StructError) {\n return error.failures().reduce<Record<string, FieldError>>(\n (previous, error) =>\n (previous[error.path.join('.')] = {\n message: error.message,\n type: error.type,\n }) && previous,\n {},\n );\n}\n\n// This is required to correctly type the input of the returned function when coerce is true\n// superstruct does not store the input type of a schema alongside it\n// Infer<typeof schema> gives the output type of the coercion\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Output, any>,\n schemaOptions: Omit<Parameters<typeof validate>[2], 'coerce'> & {\n coerce: true;\n },\n resolverOptions?: {\n raw?: boolean;\n },\n): Resolver<Input, Context, Output>;\n\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions?: {\n raw?: false;\n },\n): Resolver<Input, Context, Infer<typeof schema>>;\n\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions: Parameters<typeof validate>[2] | undefined,\n resolverOptions: {\n raw: true;\n },\n): Resolver<Input, Context, Input>;\n\n/**\n * Creates a resolver for react-hook-form using Superstruct schema validation\n * @param {Struct<TFieldValues, any>} schema - The Superstruct schema to validate against\n * @param {Parameters<typeof validate>[2]} [schemaOptions] - Optional Superstruct validation options\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {boolean} [resolverOptions.raw=false] - If true, returns raw values rather than validated results\n * @returns {Resolver<Infer<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = struct({\n * name: string(),\n * age: number()\n * });\n *\n * useForm({\n * resolver: superstructResolver(schema)\n * });\n */\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions: {\n raw?: boolean;\n } = {},\n): Resolver<Input, Context, Input | Output> {\n return (values: Input, _, options) => {\n const result = validate(values, schema, schemaOptions);\n\n if (result[0]) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(result[0]), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? Object.assign({}, values) : result[1],\n errors: {},\n };\n };\n}\n"],"names":["superstructResolver","schema","schemaOptions","resolverOptions","values","_","options","result","validate","errors","toNestErrors","error","failures","reduce","previous","path","join","message","type","shouldUseNativeValidation","validateFieldsNatively","raw","Object","assign"],"mappings":"sHA6DgB,SAAAA,EACdC,EACAC,EACAC,EAEI,CAAE,GAEN,MAAO,CAACC,EAAeC,EAAGC,KACxB,MAAMC,EAASC,EAASJ,EAAQH,EAAQC,GAExC,OAAIK,EAAO,GACF,CACLH,OAAQ,CAAA,EACRK,OAAQC,GAtEUC,EAsEoBJ,EAAO,GArE5CI,EAAMC,WAAWC,OACtB,CAACC,EAAUH,KACRG,EAASH,EAAMI,KAAKC,KAAK,MAAQ,CAChCC,QAASN,EAAMM,QACfC,KAAMP,EAAMO,QACRJ,EACR,KA+DsDR,KAItDA,EAAQa,2BAA6BC,EAAuB,GAAId,GAEzD,CACLF,OAAQD,EAAgBkB,IAAMC,OAAOC,OAAO,CAAA,EAAInB,GAAUG,EAAO,GACjEE,OAAQ,CAAA,IA9Ed,IAA0BE,EAiF1B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"superstruct.module.js","sources":["../src/superstruct.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError, FieldValues, Resolver } from 'react-hook-form';\nimport { Infer, Struct, StructError, validate } from 'superstruct';\n\nfunction parseErrorSchema(error: StructError) {\n return error.failures().reduce<Record<string, FieldError>>(\n (previous, error) =>\n (previous[error.path.join('.')] = {\n message: error.message,\n type: error.type,\n }) && previous,\n {},\n );\n}\n\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions?: {\n raw?: false;\n },\n): Resolver<Input, Context, Infer<typeof schema>>;\n\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions: Parameters<typeof validate>[2] | undefined,\n resolverOptions: {\n raw: true;\n },\n): Resolver<Input, Context, Input>;\n\n/**\n * Creates a resolver for react-hook-form using Superstruct schema validation\n * @param {Struct<TFieldValues, any>} schema - The Superstruct schema to validate against\n * @param {Parameters<typeof validate>[2]} [schemaOptions] - Optional Superstruct validation options\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {boolean} [resolverOptions.raw=false] - If true, returns raw values rather than validated results\n * @returns {Resolver<Infer<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = struct({\n * name: string(),\n * age: number()\n * });\n *\n * useForm({\n * resolver: superstructResolver(schema)\n * });\n */\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions: {\n raw?: boolean;\n } = {},\n): Resolver<Input, Context, Input | Output> {\n return (values: Input, _, options) => {\n const result = validate(values, schema, schemaOptions);\n\n if (result[0]) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(result[0]), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? Object.assign({}, values) : result[1],\n errors: {},\n };\n };\n}\n"],"names":["superstructResolver","schema","schemaOptions","resolverOptions","values","_","options","error","result","validate","errors","toNestErrors","failures","reduce","previous","path","join","message","type","shouldUseNativeValidation","validateFieldsNatively","raw","Object","assign"],"mappings":"+
|
|
1
|
+
{"version":3,"file":"superstruct.module.js","sources":["../src/superstruct.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError, FieldValues, Resolver } from 'react-hook-form';\nimport { Infer, Struct, StructError, validate } from 'superstruct';\n\nfunction parseErrorSchema(error: StructError) {\n return error.failures().reduce<Record<string, FieldError>>(\n (previous, error) =>\n (previous[error.path.join('.')] = {\n message: error.message,\n type: error.type,\n }) && previous,\n {},\n );\n}\n\n// This is required to correctly type the input of the returned function when coerce is true\n// superstruct does not store the input type of a schema alongside it\n// Infer<typeof schema> gives the output type of the coercion\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Output, any>,\n schemaOptions: Omit<Parameters<typeof validate>[2], 'coerce'> & {\n coerce: true;\n },\n resolverOptions?: {\n raw?: boolean;\n },\n): Resolver<Input, Context, Output>;\n\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions?: {\n raw?: false;\n },\n): Resolver<Input, Context, Infer<typeof schema>>;\n\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions: Parameters<typeof validate>[2] | undefined,\n resolverOptions: {\n raw: true;\n },\n): Resolver<Input, Context, Input>;\n\n/**\n * Creates a resolver for react-hook-form using Superstruct schema validation\n * @param {Struct<TFieldValues, any>} schema - The Superstruct schema to validate against\n * @param {Parameters<typeof validate>[2]} [schemaOptions] - Optional Superstruct validation options\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {boolean} [resolverOptions.raw=false] - If true, returns raw values rather than validated results\n * @returns {Resolver<Infer<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = struct({\n * name: string(),\n * age: number()\n * });\n *\n * useForm({\n * resolver: superstructResolver(schema)\n * });\n */\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions: {\n raw?: boolean;\n } = {},\n): Resolver<Input, Context, Input | Output> {\n return (values: Input, _, options) => {\n const result = validate(values, schema, schemaOptions);\n\n if (result[0]) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(result[0]), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? Object.assign({}, values) : result[1],\n errors: {},\n };\n };\n}\n"],"names":["superstructResolver","schema","schemaOptions","resolverOptions","values","_","options","error","result","validate","errors","toNestErrors","failures","reduce","previous","path","join","message","type","shouldUseNativeValidation","validateFieldsNatively","raw","Object","assign"],"mappings":"+HA6DgBA,EACdC,EACAC,EACAC,GAIA,gBAJAA,IAAAA,EAEI,CAAE,GAEEC,SAAAA,EAAeC,EAAGC,GACxB,IAjEsBC,EAiEhBC,EAASC,EAASL,EAAQH,EAAQC,GAExC,OAAIM,EAAO,GACF,CACLJ,OAAQ,CAAA,EACRM,OAAQC,GAtEUJ,EAsEoBC,EAAO,GArE5CD,EAAMK,WAAWC,OACtB,SAACC,EAAUP,GACT,OAACO,EAASP,EAAMQ,KAAKC,KAAK,MAAQ,CAChCC,QAASV,EAAMU,QACfC,KAAMX,EAAMW,QACRJ,CAAQ,EAChB,CAAA,IA+DsDR,KAItDA,EAAQa,2BAA6BC,EAAuB,CAAA,EAAId,GAEzD,CACLF,OAAQD,EAAgBkB,IAAMC,OAAOC,OAAO,CAAE,EAAEnB,GAAUI,EAAO,GACjEE,OAAQ,CAAA,GAEZ,CACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"superstruct.umd.js","sources":["../src/superstruct.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError, FieldValues, Resolver } from 'react-hook-form';\nimport { Infer, Struct, StructError, validate } from 'superstruct';\n\nfunction parseErrorSchema(error: StructError) {\n return error.failures().reduce<Record<string, FieldError>>(\n (previous, error) =>\n (previous[error.path.join('.')] = {\n message: error.message,\n type: error.type,\n }) && previous,\n {},\n );\n}\n\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions?: {\n raw?: false;\n },\n): Resolver<Input, Context, Infer<typeof schema>>;\n\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions: Parameters<typeof validate>[2] | undefined,\n resolverOptions: {\n raw: true;\n },\n): Resolver<Input, Context, Input>;\n\n/**\n * Creates a resolver for react-hook-form using Superstruct schema validation\n * @param {Struct<TFieldValues, any>} schema - The Superstruct schema to validate against\n * @param {Parameters<typeof validate>[2]} [schemaOptions] - Optional Superstruct validation options\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {boolean} [resolverOptions.raw=false] - If true, returns raw values rather than validated results\n * @returns {Resolver<Infer<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = struct({\n * name: string(),\n * age: number()\n * });\n *\n * useForm({\n * resolver: superstructResolver(schema)\n * });\n */\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions: {\n raw?: boolean;\n } = {},\n): Resolver<Input, Context, Input | Output> {\n return (values: Input, _, options) => {\n const result = validate(values, schema, schemaOptions);\n\n if (result[0]) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(result[0]), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? Object.assign({}, values) : result[1],\n errors: {},\n };\n };\n}\n"],"names":["schema","schemaOptions","resolverOptions","values","_","options","error","result","validate","errors","toNestErrors","failures","reduce","previous","path","join","message","type","shouldUseNativeValidation","validateFieldsNatively","raw","Object","assign"],"mappings":"
|
|
1
|
+
{"version":3,"file":"superstruct.umd.js","sources":["../src/superstruct.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError, FieldValues, Resolver } from 'react-hook-form';\nimport { Infer, Struct, StructError, validate } from 'superstruct';\n\nfunction parseErrorSchema(error: StructError) {\n return error.failures().reduce<Record<string, FieldError>>(\n (previous, error) =>\n (previous[error.path.join('.')] = {\n message: error.message,\n type: error.type,\n }) && previous,\n {},\n );\n}\n\n// This is required to correctly type the input of the returned function when coerce is true\n// superstruct does not store the input type of a schema alongside it\n// Infer<typeof schema> gives the output type of the coercion\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Output, any>,\n schemaOptions: Omit<Parameters<typeof validate>[2], 'coerce'> & {\n coerce: true;\n },\n resolverOptions?: {\n raw?: boolean;\n },\n): Resolver<Input, Context, Output>;\n\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions?: {\n raw?: false;\n },\n): Resolver<Input, Context, Infer<typeof schema>>;\n\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions: Parameters<typeof validate>[2] | undefined,\n resolverOptions: {\n raw: true;\n },\n): Resolver<Input, Context, Input>;\n\n/**\n * Creates a resolver for react-hook-form using Superstruct schema validation\n * @param {Struct<TFieldValues, any>} schema - The Superstruct schema to validate against\n * @param {Parameters<typeof validate>[2]} [schemaOptions] - Optional Superstruct validation options\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {boolean} [resolverOptions.raw=false] - If true, returns raw values rather than validated results\n * @returns {Resolver<Infer<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = struct({\n * name: string(),\n * age: number()\n * });\n *\n * useForm({\n * resolver: superstructResolver(schema)\n * });\n */\nexport function superstructResolver<Input extends FieldValues, Context, Output>(\n schema: Struct<Input, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions: {\n raw?: boolean;\n } = {},\n): Resolver<Input, Context, Input | Output> {\n return (values: Input, _, options) => {\n const result = validate(values, schema, schemaOptions);\n\n if (result[0]) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(result[0]), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? Object.assign({}, values) : result[1],\n errors: {},\n };\n };\n}\n"],"names":["schema","schemaOptions","resolverOptions","values","_","options","error","result","validate","errors","toNestErrors","failures","reduce","previous","path","join","message","type","shouldUseNativeValidation","validateFieldsNatively","raw","Object","assign"],"mappings":"qZA8DEA,EACAC,EACAC,GAIA,gBAJAA,IAAAA,EAEI,CAAE,GAEEC,SAAAA,EAAeC,EAAGC,GACxB,IAjEsBC,EAiEhBC,EAASC,EAAAA,SAASL,EAAQH,EAAQC,GAExC,OAAIM,EAAO,GACF,CACLJ,OAAQ,CAAA,EACRM,OAAQC,EAAYA,cAtEFJ,EAsEoBC,EAAO,GArE5CD,EAAMK,WAAWC,OACtB,SAACC,EAAUP,GACT,OAACO,EAASP,EAAMQ,KAAKC,KAAK,MAAQ,CAChCC,QAASV,EAAMU,QACfC,KAAMX,EAAMW,QACRJ,CAAQ,EAChB,CAAA,IA+DsDR,KAItDA,EAAQa,2BAA6BC,yBAAuB,CAAA,EAAId,GAEzD,CACLF,OAAQD,EAAgBkB,IAAMC,OAAOC,OAAO,CAAE,EAAEnB,GAAUI,EAAO,GACjEE,OAAQ,CAAA,GAEZ,CACF"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { renderHook } from '@testing-library/react';
|
|
1
2
|
import { Resolver, SubmitHandler, useForm } from 'react-hook-form';
|
|
2
3
|
import * as s from 'superstruct';
|
|
3
4
|
import { superstructResolver } from '..';
|
|
@@ -18,8 +19,9 @@ describe('superstructResolver', () => {
|
|
|
18
19
|
it('should return values from superstructResolver with coerced values', async () => {
|
|
19
20
|
const result = await superstructResolver(
|
|
20
21
|
s.object({
|
|
21
|
-
id: s.coerce(s.
|
|
22
|
+
id: s.coerce(s.string(), s.number(), (val) => String(val)),
|
|
22
23
|
}),
|
|
24
|
+
{ coerce: true },
|
|
23
25
|
)({ id: 1 }, undefined, {
|
|
24
26
|
fields,
|
|
25
27
|
shouldUseNativeValidation,
|
|
@@ -64,9 +66,13 @@ describe('superstructResolver', () => {
|
|
|
64
66
|
it('should correctly infer the output type from a superstruct schema for the handleSubmit function in useForm', () => {
|
|
65
67
|
const schema = s.object({ id: s.number() });
|
|
66
68
|
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
})
|
|
69
|
+
const {
|
|
70
|
+
result: { current: form },
|
|
71
|
+
} = renderHook(() =>
|
|
72
|
+
useForm({
|
|
73
|
+
resolver: superstructResolver(schema, {}),
|
|
74
|
+
}),
|
|
75
|
+
);
|
|
70
76
|
|
|
71
77
|
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
|
|
72
78
|
|
|
@@ -82,11 +88,19 @@ describe('superstructResolver', () => {
|
|
|
82
88
|
id: s.coerce(s.string(), s.number(), (val) => String(val)),
|
|
83
89
|
});
|
|
84
90
|
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
})
|
|
91
|
+
const {
|
|
92
|
+
result: { current: form },
|
|
93
|
+
} = renderHook(() =>
|
|
94
|
+
useForm({
|
|
95
|
+
// With coerce, there is no way to infer the input type of the schema
|
|
96
|
+
resolver: superstructResolver<{ id: number }, unknown, { id: string }>(
|
|
97
|
+
schema,
|
|
98
|
+
{ coerce: true },
|
|
99
|
+
),
|
|
100
|
+
}),
|
|
101
|
+
);
|
|
88
102
|
|
|
89
|
-
expectTypeOf(form.watch('id')).toEqualTypeOf<
|
|
103
|
+
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
|
|
90
104
|
|
|
91
105
|
expectTypeOf(form.handleSubmit).parameter(0).toEqualTypeOf<
|
|
92
106
|
SubmitHandler<{
|
|
@@ -13,6 +13,19 @@ function parseErrorSchema(error: StructError) {
|
|
|
13
13
|
);
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
// This is required to correctly type the input of the returned function when coerce is true
|
|
17
|
+
// superstruct does not store the input type of a schema alongside it
|
|
18
|
+
// Infer<typeof schema> gives the output type of the coercion
|
|
19
|
+
export function superstructResolver<Input extends FieldValues, Context, Output>(
|
|
20
|
+
schema: Struct<Output, any>,
|
|
21
|
+
schemaOptions: Omit<Parameters<typeof validate>[2], 'coerce'> & {
|
|
22
|
+
coerce: true;
|
|
23
|
+
},
|
|
24
|
+
resolverOptions?: {
|
|
25
|
+
raw?: boolean;
|
|
26
|
+
},
|
|
27
|
+
): Resolver<Input, Context, Output>;
|
|
28
|
+
|
|
16
29
|
export function superstructResolver<Input extends FieldValues, Context, Output>(
|
|
17
30
|
schema: Struct<Input, any>,
|
|
18
31
|
schemaOptions?: Parameters<typeof validate>[2],
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { renderHook } from '@testing-library/react';
|
|
1
2
|
import { SubmitHandler } from 'react-hook-form';
|
|
2
3
|
import { useForm } from 'react-hook-form';
|
|
3
4
|
import { Resolver } from 'react-hook-form';
|
|
@@ -51,9 +52,13 @@ describe('typanionResolver', () => {
|
|
|
51
52
|
it('should correctly infer the output type from a typanion schema for the handleSubmit function in useForm', () => {
|
|
52
53
|
const schema = t.isObject({ id: t.isNumber() });
|
|
53
54
|
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
})
|
|
55
|
+
const {
|
|
56
|
+
result: { current: form },
|
|
57
|
+
} = renderHook(() =>
|
|
58
|
+
useForm({
|
|
59
|
+
resolver: typanionResolver(schema),
|
|
60
|
+
}),
|
|
61
|
+
);
|
|
57
62
|
|
|
58
63
|
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
|
|
59
64
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Type } from '@sinclair/typebox';
|
|
2
2
|
import { TypeCompiler } from '@sinclair/typebox/compiler';
|
|
3
|
+
import { renderHook } from '@testing-library/react';
|
|
3
4
|
import { Resolver, SubmitHandler, useForm } from 'react-hook-form';
|
|
4
5
|
import { typeboxResolver } from '..';
|
|
5
6
|
import { fields, invalidData, schema, validData } from './__fixtures__/data';
|
|
@@ -72,12 +73,16 @@ describe('typeboxResolver', () => {
|
|
|
72
73
|
it('should correctly infer the output type from a typebox schema for the handleSubmit function in useForm', () => {
|
|
73
74
|
const schema = Type.Object({ id: Type.Number() });
|
|
74
75
|
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
76
|
+
const {
|
|
77
|
+
result: { current: form },
|
|
78
|
+
} = renderHook(() =>
|
|
79
|
+
useForm({
|
|
80
|
+
resolver: typeboxResolver(schema),
|
|
81
|
+
defaultValues: {
|
|
82
|
+
id: 3,
|
|
83
|
+
},
|
|
84
|
+
}),
|
|
85
|
+
);
|
|
81
86
|
|
|
82
87
|
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
|
|
83
88
|
|
|
@@ -91,12 +96,16 @@ describe('typeboxResolver', () => {
|
|
|
91
96
|
it('should correctly infer the output type from a typebox schema with TypeCompiler for the handleSubmit function in useForm', () => {
|
|
92
97
|
const typecheck = TypeCompiler.Compile(Type.Object({ id: Type.Number() }));
|
|
93
98
|
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
99
|
+
const {
|
|
100
|
+
result: { current: form },
|
|
101
|
+
} = renderHook(() =>
|
|
102
|
+
useForm({
|
|
103
|
+
resolver: typeboxResolver(typecheck),
|
|
104
|
+
defaultValues: {
|
|
105
|
+
id: 3,
|
|
106
|
+
},
|
|
107
|
+
}),
|
|
108
|
+
);
|
|
100
109
|
|
|
101
110
|
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
|
|
102
111
|
|
|
@@ -114,12 +123,16 @@ describe('typeboxResolver', () => {
|
|
|
114
123
|
.Encode((v) => Number.parseInt(v)),
|
|
115
124
|
});
|
|
116
125
|
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
126
|
+
const {
|
|
127
|
+
result: { current: form },
|
|
128
|
+
} = renderHook(() =>
|
|
129
|
+
useForm({
|
|
130
|
+
resolver: typeboxResolver(schema),
|
|
131
|
+
defaultValues: {
|
|
132
|
+
id: 3,
|
|
133
|
+
},
|
|
134
|
+
}),
|
|
135
|
+
);
|
|
123
136
|
|
|
124
137
|
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
|
|
125
138
|
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var r=require("@hookform/resolvers"),e=require("react-hook-form");function t(r,e){(null==e||e>r.length)&&(e=r.length);for(var t=0,n=Array(e);t<e;t++)n[t]=r[t];return n}exports.typeschemaResolver=function(n,a,o){return void 0===o&&(o={}),function(a,i,s){try{var u=function(){if(l.issues){var n=function(r,n){for(var a,o={},i=function(r){var e="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(e)return(e=e.call(r)).next.bind(e);if(Array.isArray(r)||(e=function(r,e){if(r){if("string"==typeof r)return t(r,e);var n={}.toString.call(r).slice(8,-1);return"Object"===n&&r.constructor&&(n=r.constructor.name),"Map"===n||"Set"===n?Array.from(r):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?t(r,e):void 0}}(r))){e&&(r=e);var n=0;return function(){return n>=r.length?{done:!0}:{done:!1,value:r[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(Object.assign([],r));!(a=i()).done;){var s=a.value;if(s.path){var u=s.path.join(".");if(o[u]||(o[u]={message:s.message,type:""}),n){var l=o[u].types,c=l&&l[""];o[u]=e.appendErrors(u,n,o,"",c?[].concat(c,s.message):s.message)}}}return o}(l.issues,!s.shouldUseNativeValidation&&"all"===s.criteriaMode);return{values:{},errors:r.toNestErrors(n,s)}}return s.shouldUseNativeValidation&&r.validateFieldsNatively({},s),{values:o.raw?Object.assign({},a):l.value,errors:{}}},l=n["~standard"].validate(a),c=function(){if(l instanceof Promise)return Promise.resolve(l).then(function(r){l=r})}();return Promise.resolve(c&&c.then?c.then(u):u())}catch(r){return Promise.reject(r)}}};
|
|
2
2
|
//# sourceMappingURL=typeschema.js.map
|