@hookform/resolvers 3.9.1 → 4.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 +77 -8
- package/ajv/dist/ajv.js +1 -1
- package/ajv/dist/ajv.js.map +1 -1
- package/ajv/dist/ajv.mjs +1 -1
- package/ajv/dist/ajv.modern.mjs +1 -1
- package/ajv/dist/ajv.modern.mjs.map +1 -1
- package/ajv/dist/ajv.module.js +1 -1
- package/ajv/dist/ajv.module.js.map +1 -1
- package/ajv/dist/ajv.umd.js +1 -1
- package/ajv/dist/ajv.umd.js.map +1 -1
- package/ajv/dist/types.d.ts +8 -0
- package/ajv/src/__tests__/__fixtures__/data-errors.ts +216 -0
- package/ajv/src/__tests__/__snapshots__/ajv-errors.ts.snap +462 -0
- package/ajv/src/__tests__/__snapshots__/ajv.ts.snap +6 -6
- package/ajv/src/__tests__/ajv-errors.ts +227 -0
- package/ajv/src/ajv.ts +27 -14
- package/ajv/src/types.ts +8 -0
- 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/dist/types.d.ts +1 -1
- package/arktype/package.json +1 -1
- package/arktype/src/__tests__/Form-native-validation.tsx +3 -3
- package/arktype/src/__tests__/Form.tsx +2 -2
- package/arktype/src/__tests__/__fixtures__/data.ts +1 -1
- package/arktype/src/__tests__/__snapshots__/arktype.ts.snap +22 -415
- package/arktype/src/arktype.ts +17 -6
- package/arktype/src/types.ts +1 -1
- package/class-validator/dist/class-validator.js +1 -1
- package/class-validator/dist/class-validator.js.map +1 -1
- package/class-validator/dist/class-validator.mjs +1 -1
- package/class-validator/dist/class-validator.modern.mjs +1 -1
- package/class-validator/dist/class-validator.modern.mjs.map +1 -1
- package/class-validator/dist/class-validator.module.js +1 -1
- package/class-validator/dist/class-validator.module.js.map +1 -1
- package/class-validator/dist/class-validator.umd.js +1 -1
- package/class-validator/dist/class-validator.umd.js.map +1 -1
- package/class-validator/dist/types.d.ts +1 -1
- package/class-validator/src/__tests__/class-validator.ts +1 -1
- package/class-validator/src/class-validator.ts +1 -1
- package/class-validator/src/types.ts +1 -1
- package/dist/resolvers.js +1 -1
- package/dist/resolvers.js.map +1 -1
- package/dist/resolvers.mjs +1 -1
- package/dist/resolvers.mjs.map +1 -1
- package/dist/resolvers.module.js +1 -1
- package/dist/resolvers.module.js.map +1 -1
- package/dist/resolvers.umd.js +1 -1
- package/dist/resolvers.umd.js.map +1 -1
- package/effect-ts/dist/effect-ts.js +1 -1
- package/effect-ts/dist/effect-ts.js.map +1 -1
- package/effect-ts/dist/effect-ts.mjs +1 -1
- package/effect-ts/dist/effect-ts.modern.mjs +1 -1
- package/effect-ts/dist/effect-ts.modern.mjs.map +1 -1
- package/effect-ts/dist/effect-ts.module.js +1 -1
- package/effect-ts/dist/effect-ts.module.js.map +1 -1
- package/effect-ts/dist/effect-ts.umd.js +1 -1
- package/effect-ts/dist/effect-ts.umd.js.map +1 -1
- package/effect-ts/dist/types.d.ts +2 -2
- package/effect-ts/package.json +1 -2
- package/effect-ts/src/__tests__/Form-native-validation.tsx +3 -3
- package/effect-ts/src/__tests__/Form.tsx +3 -3
- package/effect-ts/src/__tests__/__fixtures__/data.ts +2 -2
- package/effect-ts/src/__tests__/__snapshots__/effect-ts.ts.snap +35 -1
- package/effect-ts/src/__tests__/effect-ts.ts +61 -0
- package/effect-ts/src/effect-ts.ts +37 -10
- package/effect-ts/src/types.ts +2 -2
- package/io-ts/dist/io-ts.js +1 -1
- package/io-ts/dist/io-ts.js.map +1 -1
- package/io-ts/dist/io-ts.mjs +1 -1
- package/io-ts/dist/io-ts.modern.mjs +1 -1
- package/io-ts/dist/io-ts.modern.mjs.map +1 -1
- package/io-ts/dist/io-ts.module.js +1 -1
- package/io-ts/dist/io-ts.module.js.map +1 -1
- package/io-ts/dist/io-ts.umd.js +1 -1
- package/io-ts/dist/io-ts.umd.js.map +1 -1
- package/package.json +47 -36
- package/standard-schema/dist/index.d.ts +2 -0
- package/standard-schema/dist/standard-schema.d.ts +2 -0
- package/standard-schema/dist/standard-schema.js +2 -0
- package/standard-schema/dist/standard-schema.js.map +1 -0
- package/standard-schema/dist/standard-schema.mjs +2 -0
- package/standard-schema/dist/standard-schema.modern.mjs +2 -0
- package/standard-schema/dist/standard-schema.modern.mjs.map +1 -0
- package/standard-schema/dist/standard-schema.module.js +2 -0
- package/standard-schema/dist/standard-schema.module.js.map +1 -0
- package/standard-schema/dist/standard-schema.umd.js +2 -0
- package/standard-schema/dist/standard-schema.umd.js.map +1 -0
- package/standard-schema/dist/types.d.ts +3 -0
- package/standard-schema/package.json +18 -0
- package/standard-schema/src/__tests__/Form-native-validation.tsx +82 -0
- package/standard-schema/src/__tests__/Form.tsx +56 -0
- package/standard-schema/src/__tests__/__fixtures__/data.ts +65 -0
- package/standard-schema/src/__tests__/__snapshots__/standard-schema.ts.snap +63 -0
- package/standard-schema/src/__tests__/standard-schema.ts +28 -0
- package/standard-schema/src/index.ts +2 -0
- package/standard-schema/src/standard-schema.ts +45 -0
- package/standard-schema/src/types.ts +10 -0
- package/superstruct/dist/superstruct.js +1 -1
- package/superstruct/dist/superstruct.js.map +1 -1
- package/superstruct/dist/superstruct.mjs +1 -1
- package/superstruct/dist/superstruct.modern.mjs +1 -1
- package/superstruct/dist/superstruct.modern.mjs.map +1 -1
- package/superstruct/dist/superstruct.module.js +1 -1
- package/superstruct/dist/superstruct.module.js.map +1 -1
- package/superstruct/dist/superstruct.umd.js +1 -1
- package/superstruct/dist/superstruct.umd.js.map +1 -1
- package/superstruct/src/superstruct.ts +1 -1
- package/typebox/src/__tests__/__snapshots__/typebox-compiler.ts.snap +21 -21
- package/typebox/src/__tests__/__snapshots__/typebox.ts.snap +21 -21
- 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/typeschema.ts +3 -1
- package/valibot/dist/valibot.js +1 -1
- package/valibot/dist/valibot.js.map +1 -1
- package/valibot/dist/valibot.mjs +1 -1
- package/valibot/dist/valibot.modern.mjs +1 -1
- package/valibot/dist/valibot.modern.mjs.map +1 -1
- package/valibot/dist/valibot.module.js +1 -1
- package/valibot/dist/valibot.module.js.map +1 -1
- package/valibot/dist/valibot.umd.js +1 -1
- package/valibot/dist/valibot.umd.js.map +1 -1
- package/valibot/package.json +1 -1
- package/valibot/src/__tests__/valibot.ts +1 -1
- package/valibot/src/valibot.ts +3 -1
- package/vine/dist/vine.js +1 -1
- package/vine/dist/vine.js.map +1 -1
- package/vine/dist/vine.mjs +1 -1
- package/vine/dist/vine.modern.mjs +1 -1
- package/vine/dist/vine.modern.mjs.map +1 -1
- package/vine/dist/vine.module.js +1 -1
- package/vine/dist/vine.module.js.map +1 -1
- package/vine/dist/vine.umd.js +1 -1
- package/vine/dist/vine.umd.js.map +1 -1
- package/vine/src/vine.ts +1 -1
- 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/src/yup.ts +1 -1
- package/zod/dist/zod.js +1 -1
- package/zod/dist/zod.js.map +1 -1
- package/zod/dist/zod.mjs +1 -1
- package/zod/dist/zod.modern.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/zod.ts +1 -1
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { ajvResolver } from '..';
|
|
2
|
+
import * as fixture from './__fixtures__/data-errors';
|
|
3
|
+
|
|
4
|
+
const shouldUseNativeValidation = false;
|
|
5
|
+
|
|
6
|
+
describe('ajvResolver with errorMessage', () => {
|
|
7
|
+
it('should return values when validation pass', async () => {
|
|
8
|
+
expect(
|
|
9
|
+
await ajvResolver(fixture.schemaA)(fixture.validDataA, undefined, {
|
|
10
|
+
fields: fixture.fieldsA,
|
|
11
|
+
criteriaMode: 'all',
|
|
12
|
+
shouldUseNativeValidation,
|
|
13
|
+
}),
|
|
14
|
+
).toEqual({
|
|
15
|
+
values: fixture.validDataA,
|
|
16
|
+
errors: {},
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should return customized error messages when validation fails', async () => {
|
|
21
|
+
expect(
|
|
22
|
+
await ajvResolver(fixture.schemaA)(
|
|
23
|
+
fixture.invalidDataA,
|
|
24
|
+
{},
|
|
25
|
+
{
|
|
26
|
+
fields: fixture.fieldsA,
|
|
27
|
+
criteriaMode: 'all',
|
|
28
|
+
shouldUseNativeValidation,
|
|
29
|
+
},
|
|
30
|
+
),
|
|
31
|
+
).toMatchSnapshot();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should return customized error messages when requirement fails', async () => {
|
|
35
|
+
expect(
|
|
36
|
+
await ajvResolver(fixture.schemaA)({}, undefined, {
|
|
37
|
+
fields: fixture.fieldsA,
|
|
38
|
+
criteriaMode: 'all',
|
|
39
|
+
shouldUseNativeValidation,
|
|
40
|
+
}),
|
|
41
|
+
).toMatchSnapshot();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should return customized error messages when some properties are undefined', async () => {
|
|
45
|
+
expect(
|
|
46
|
+
await ajvResolver(fixture.schemaA, undefined, { mode: 'sync' })(
|
|
47
|
+
fixture.undefinedDataA,
|
|
48
|
+
undefined,
|
|
49
|
+
{
|
|
50
|
+
fields: fixture.fieldsA,
|
|
51
|
+
criteriaMode: 'all',
|
|
52
|
+
shouldUseNativeValidation,
|
|
53
|
+
},
|
|
54
|
+
),
|
|
55
|
+
).toMatchSnapshot();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('should return the same customized message for all validation failures', async () => {
|
|
59
|
+
expect(
|
|
60
|
+
await ajvResolver(fixture.schemaB)(
|
|
61
|
+
fixture.invalidDataB,
|
|
62
|
+
{},
|
|
63
|
+
{
|
|
64
|
+
fields: fixture.fieldsRest,
|
|
65
|
+
criteriaMode: 'all',
|
|
66
|
+
shouldUseNativeValidation,
|
|
67
|
+
},
|
|
68
|
+
),
|
|
69
|
+
).toMatchSnapshot();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('should return the same customized error message when requirement fails', async () => {
|
|
73
|
+
expect(
|
|
74
|
+
await ajvResolver(fixture.schemaB)({}, undefined, {
|
|
75
|
+
fields: fixture.fieldsRest,
|
|
76
|
+
criteriaMode: 'all',
|
|
77
|
+
shouldUseNativeValidation,
|
|
78
|
+
}),
|
|
79
|
+
).toMatchSnapshot();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('should return the same customized message when some properties are undefined', async () => {
|
|
83
|
+
expect(
|
|
84
|
+
await ajvResolver(fixture.schemaB)(fixture.undefinedDataB, undefined, {
|
|
85
|
+
fields: fixture.fieldsRest,
|
|
86
|
+
criteriaMode: 'all',
|
|
87
|
+
shouldUseNativeValidation,
|
|
88
|
+
}),
|
|
89
|
+
).toMatchSnapshot();
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('should return customized error messages for certain keywords when walidation fails', async () => {
|
|
93
|
+
expect(
|
|
94
|
+
await ajvResolver(fixture.schemaC)(
|
|
95
|
+
fixture.invalidDataC,
|
|
96
|
+
{},
|
|
97
|
+
{
|
|
98
|
+
fields: fixture.fieldsRest,
|
|
99
|
+
criteriaMode: 'all',
|
|
100
|
+
shouldUseNativeValidation,
|
|
101
|
+
},
|
|
102
|
+
),
|
|
103
|
+
).toMatchSnapshot();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('should return customized error messages for certain keywords when requirement fails', async () => {
|
|
107
|
+
expect(
|
|
108
|
+
await ajvResolver(fixture.schemaC)({}, undefined, {
|
|
109
|
+
fields: fixture.fieldsRest,
|
|
110
|
+
criteriaMode: 'all',
|
|
111
|
+
shouldUseNativeValidation,
|
|
112
|
+
}),
|
|
113
|
+
).toMatchSnapshot();
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('should return customized error messages for certain keywords when some properties are undefined', async () => {
|
|
117
|
+
expect(
|
|
118
|
+
await ajvResolver(fixture.schemaC)(fixture.undefinedDataC, undefined, {
|
|
119
|
+
fields: fixture.fieldsRest,
|
|
120
|
+
criteriaMode: 'all',
|
|
121
|
+
shouldUseNativeValidation,
|
|
122
|
+
}),
|
|
123
|
+
).toMatchSnapshot();
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('should return different messages for different properties when walidation fails', async () => {
|
|
127
|
+
expect(
|
|
128
|
+
await ajvResolver(fixture.schemaD)(
|
|
129
|
+
fixture.invalidDataD,
|
|
130
|
+
{},
|
|
131
|
+
{
|
|
132
|
+
fields: fixture.fieldsRest,
|
|
133
|
+
criteriaMode: 'all',
|
|
134
|
+
shouldUseNativeValidation,
|
|
135
|
+
},
|
|
136
|
+
),
|
|
137
|
+
).toMatchSnapshot();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('should return different messages for different properties when requirement fails', async () => {
|
|
141
|
+
expect(
|
|
142
|
+
await ajvResolver(fixture.schemaD)({}, undefined, {
|
|
143
|
+
fields: fixture.fieldsRest,
|
|
144
|
+
criteriaMode: 'all',
|
|
145
|
+
shouldUseNativeValidation,
|
|
146
|
+
}),
|
|
147
|
+
).toMatchSnapshot();
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('should return different messages for different properties when some properties are undefined', async () => {
|
|
151
|
+
expect(
|
|
152
|
+
await ajvResolver(fixture.schemaD)(fixture.undefinedDataD, undefined, {
|
|
153
|
+
fields: fixture.fieldsRest,
|
|
154
|
+
criteriaMode: 'all',
|
|
155
|
+
shouldUseNativeValidation,
|
|
156
|
+
}),
|
|
157
|
+
).toMatchSnapshot();
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('should return customized errors for properties/items when walidation fails', async () => {
|
|
161
|
+
expect(
|
|
162
|
+
await ajvResolver(fixture.schemaE)(
|
|
163
|
+
fixture.invalidDataE,
|
|
164
|
+
{},
|
|
165
|
+
{
|
|
166
|
+
fields: fixture.fieldsRest,
|
|
167
|
+
criteriaMode: 'all',
|
|
168
|
+
shouldUseNativeValidation,
|
|
169
|
+
},
|
|
170
|
+
),
|
|
171
|
+
).toMatchSnapshot();
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it('should return customized errors for properties/items when requirement fails', async () => {
|
|
175
|
+
expect(
|
|
176
|
+
await ajvResolver(fixture.schemaE)({}, undefined, {
|
|
177
|
+
fields: fixture.fieldsRest,
|
|
178
|
+
criteriaMode: 'all',
|
|
179
|
+
shouldUseNativeValidation,
|
|
180
|
+
}),
|
|
181
|
+
).toMatchSnapshot();
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('should return customized errors for properties/items when some properties are undefined', async () => {
|
|
185
|
+
expect(
|
|
186
|
+
await ajvResolver(fixture.schemaE)(fixture.undefinedDataE, undefined, {
|
|
187
|
+
fields: fixture.fieldsRest,
|
|
188
|
+
criteriaMode: 'all',
|
|
189
|
+
shouldUseNativeValidation,
|
|
190
|
+
}),
|
|
191
|
+
).toMatchSnapshot();
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it('should return a default message if there is no specific message for the error when walidation fails', async () => {
|
|
195
|
+
expect(
|
|
196
|
+
await ajvResolver(fixture.schemaF)(
|
|
197
|
+
fixture.invalidDataF,
|
|
198
|
+
{},
|
|
199
|
+
{
|
|
200
|
+
fields: fixture.fieldsRest,
|
|
201
|
+
criteriaMode: 'all',
|
|
202
|
+
shouldUseNativeValidation,
|
|
203
|
+
},
|
|
204
|
+
),
|
|
205
|
+
).toMatchSnapshot();
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it('should return a default message if there is no specific message for the error when requirement fails', async () => {
|
|
209
|
+
expect(
|
|
210
|
+
await ajvResolver(fixture.schemaF)({}, undefined, {
|
|
211
|
+
fields: fixture.fieldsRest,
|
|
212
|
+
criteriaMode: 'all',
|
|
213
|
+
shouldUseNativeValidation,
|
|
214
|
+
}),
|
|
215
|
+
).toMatchSnapshot();
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('should return a default message if there is no specific message for the error when some properties are undefined', async () => {
|
|
219
|
+
expect(
|
|
220
|
+
await ajvResolver(fixture.schemaF)(fixture.undefinedDataF, undefined, {
|
|
221
|
+
fields: fixture.fieldsRest,
|
|
222
|
+
criteriaMode: 'all',
|
|
223
|
+
shouldUseNativeValidation,
|
|
224
|
+
}),
|
|
225
|
+
).toMatchSnapshot();
|
|
226
|
+
});
|
|
227
|
+
});
|
package/ajv/src/ajv.ts
CHANGED
|
@@ -2,47 +2,60 @@ import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
|
|
|
2
2
|
import Ajv, { DefinedError } from 'ajv';
|
|
3
3
|
import ajvErrors from 'ajv-errors';
|
|
4
4
|
import { FieldError, appendErrors } from 'react-hook-form';
|
|
5
|
-
import { Resolver } from './types';
|
|
5
|
+
import { AjvError, Resolver } from './types';
|
|
6
6
|
|
|
7
7
|
const parseErrorSchema = (
|
|
8
|
-
ajvErrors:
|
|
8
|
+
ajvErrors: AjvError[],
|
|
9
9
|
validateAllFieldCriteria: boolean,
|
|
10
10
|
) => {
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
const parsedErrors: Record<string, FieldError> = {};
|
|
12
|
+
|
|
13
|
+
const reduceError = (error: AjvError) => {
|
|
14
|
+
// Ajv will return empty instancePath when require error
|
|
13
15
|
if (error.keyword === 'required') {
|
|
14
|
-
error.instancePath +=
|
|
16
|
+
error.instancePath += `/${error.params.missingProperty}`;
|
|
15
17
|
}
|
|
16
|
-
});
|
|
17
18
|
|
|
18
|
-
return ajvErrors.reduce<Record<string, FieldError>>((previous, error) => {
|
|
19
19
|
// `/deepObject/data` -> `deepObject.data`
|
|
20
20
|
const path = error.instancePath.substring(1).replace(/\//g, '.');
|
|
21
21
|
|
|
22
|
-
if (!
|
|
23
|
-
|
|
22
|
+
if (!parsedErrors[path]) {
|
|
23
|
+
parsedErrors[path] = {
|
|
24
24
|
message: error.message,
|
|
25
25
|
type: error.keyword,
|
|
26
26
|
};
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
if (validateAllFieldCriteria) {
|
|
30
|
-
const types =
|
|
30
|
+
const types = parsedErrors[path].types;
|
|
31
31
|
const messages = types && types[error.keyword];
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
parsedErrors[path] = appendErrors(
|
|
34
34
|
path,
|
|
35
35
|
validateAllFieldCriteria,
|
|
36
|
-
|
|
36
|
+
parsedErrors,
|
|
37
37
|
error.keyword,
|
|
38
38
|
messages
|
|
39
39
|
? ([] as string[]).concat(messages as string[], error.message || '')
|
|
40
40
|
: error.message,
|
|
41
41
|
) as FieldError;
|
|
42
42
|
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
for (let index = 0; index < ajvErrors.length; index += 1) {
|
|
46
|
+
const error = ajvErrors[index];
|
|
47
|
+
|
|
48
|
+
if (error.keyword === 'errorMessage') {
|
|
49
|
+
error.params.errors.forEach((originalError) => {
|
|
50
|
+
originalError.message = error.message;
|
|
51
|
+
reduceError(originalError);
|
|
52
|
+
});
|
|
53
|
+
} else {
|
|
54
|
+
reduceError(error);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
43
57
|
|
|
44
|
-
|
|
45
|
-
}, {});
|
|
58
|
+
return parsedErrors;
|
|
46
59
|
};
|
|
47
60
|
|
|
48
61
|
export const ajvResolver: Resolver =
|
package/ajv/src/types.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as Ajv from 'ajv';
|
|
2
|
+
import type { DefinedError, ErrorObject } from 'ajv';
|
|
2
3
|
import { FieldValues, ResolverOptions, ResolverResult } from 'react-hook-form';
|
|
3
4
|
|
|
4
5
|
export type Resolver = <T>(
|
|
@@ -10,3 +11,10 @@ export type Resolver = <T>(
|
|
|
10
11
|
context: TContext | undefined,
|
|
11
12
|
options: ResolverOptions<TFieldValues>,
|
|
12
13
|
) => Promise<ResolverResult<TFieldValues>>;
|
|
14
|
+
|
|
15
|
+
// ajv doesn't export any types for errors with `keyword='errorMessage'`
|
|
16
|
+
type ErrorMessage = ErrorObject<
|
|
17
|
+
'errorMessage',
|
|
18
|
+
{ errors: (DefinedError & { emUsed: boolean })[] }
|
|
19
|
+
>;
|
|
20
|
+
export type AjvError = ErrorMessage | DefinedError;
|
package/arktype/dist/arktype.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var r=require("@hookform/resolvers"),e=require("arktype");exports.arktypeResolver=function(
|
|
1
|
+
var r=require("@hookform/resolvers"),e=require("arktype"),o=function(r){for(var e=[].concat(r),o={};e.length;){var s=e[0],t=s.path.join(".");o[t]||(o[t]={message:s.message,type:s.code}),e.shift()}return o};exports.arktypeResolver=function(s,t,a){return void 0===a&&(a={}),function(t,i,n){var v=s(t);return v instanceof e.ArkErrors?{values:{},errors:r.toNestErrors(o(v),n)}:(n.shouldUseNativeValidation&&r.validateFieldsNatively({},n),{errors:{},values:a.raw?Object.assign({},t):v})}};
|
|
2
2
|
//# sourceMappingURL=arktype.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"arktype.js","sources":["../src/arktype.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { ArkErrors } from 'arktype';\nimport { FieldError, FieldErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (
|
|
1
|
+
{"version":3,"file":"arktype.js","sources":["../src/arktype.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { ArkErrors } from 'arktype';\nimport { FieldError, FieldErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (arkErrors: ArkErrors): Record<string, FieldError> => {\n const errors = [...arkErrors];\n const fieldsErrors: Record<string, FieldError> = {};\n\n for (; errors.length; ) {\n const error = errors[0];\n const _path = error.path.join('.');\n\n if (!fieldsErrors[_path]) {\n fieldsErrors[_path] = { message: error.message, type: error.code };\n }\n\n errors.shift();\n }\n\n return fieldsErrors;\n};\n\nexport const arktypeResolver: Resolver =\n (schema, _schemaOptions, resolverOptions = {}) =>\n (values, _, options) => {\n const out = schema(values);\n\n if (out instanceof ArkErrors) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(out), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n errors: {} as FieldErrors,\n values: resolverOptions.raw ? Object.assign({}, values) : out,\n };\n };\n"],"names":["parseErrorSchema","arkErrors","errors","concat","fieldsErrors","length","error","_path","path","join","message","type","code","shift","schema","_schemaOptions","resolverOptions","values","_","options","out","ArkErrors","toNestErrors","shouldUseNativeValidation","validateFieldsNatively","raw","Object","assign"],"mappings":"0DAKMA,EAAmB,SAACC,GAIxB,IAHA,IAAMC,EAAMC,GAAAA,OAAOF,GACbG,EAA2C,GAE1CF,EAAOG,QAAU,CACtB,IAAMC,EAAQJ,EAAO,GACfK,EAAQD,EAAME,KAAKC,KAAK,KAEzBL,EAAaG,KAChBH,EAAaG,GAAS,CAAEG,QAASJ,EAAMI,QAASC,KAAML,EAAMM,OAG9DV,EAAOW,OACT,CAEA,OAAOT,CACT,0BAGE,SAACU,EAAQC,EAAgBC,GACzB,YADyBA,IAAAA,IAAAA,EAAkB,CAAE,GAC7C,SAACC,EAAQC,EAAGC,GACV,IAAMC,EAAMN,EAAOG,GAEnB,OAAIG,aAAeC,EAAAA,UACV,CACLJ,OAAQ,GACRf,OAAQoB,EAAYA,aAACtB,EAAiBoB,GAAMD,KAIhDA,EAAQI,2BAA6BC,EAAsBA,uBAAC,GAAIL,GAEzD,CACLjB,OAAQ,GACRe,OAAQD,EAAgBS,IAAMC,OAAOC,OAAO,GAAIV,GAAUG,GAE9D,CAAC"}
|
package/arktype/dist/arktype.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{toNestErrors as r,validateFieldsNatively as
|
|
1
|
+
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";import{ArkErrors as o}from"arktype";var t=function(r){for(var e=[].concat(r),o={};e.length;){var t=e[0],a=t.path.join(".");o[a]||(o[a]={message:t.message,type:t.code}),e.shift()}return o},a=function(a,n,s){return void 0===s&&(s={}),function(n,i,f){var u=a(n);return u instanceof o?{values:{},errors:r(t(u),f)}:(f.shouldUseNativeValidation&&e({},f),{errors:{},values:s.raw?Object.assign({},n):u})}};export{a as arktypeResolver};
|
|
2
2
|
//# sourceMappingURL=arktype.module.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{toNestErrors as
|
|
1
|
+
import{toNestErrors as e,validateFieldsNatively as o}from"@hookform/resolvers";import{ArkErrors as r}from"arktype";const s=e=>{const o=[...e],r={};for(;o.length;){const e=o[0],s=e.path.join(".");r[s]||(r[s]={message:e.message,type:e.code}),o.shift()}return r},t=(t,a,n={})=>(a,i,c)=>{const m=t(a);return m instanceof r?{values:{},errors:e(s(m),c)}:(c.shouldUseNativeValidation&&o({},c),{errors:{},values:n.raw?Object.assign({},a):m})};export{t as arktypeResolver};
|
|
2
2
|
//# sourceMappingURL=arktype.modern.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"arktype.modern.mjs","sources":["../src/arktype.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { ArkErrors } from 'arktype';\nimport { FieldError, FieldErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (
|
|
1
|
+
{"version":3,"file":"arktype.modern.mjs","sources":["../src/arktype.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { ArkErrors } from 'arktype';\nimport { FieldError, FieldErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (arkErrors: ArkErrors): Record<string, FieldError> => {\n const errors = [...arkErrors];\n const fieldsErrors: Record<string, FieldError> = {};\n\n for (; errors.length; ) {\n const error = errors[0];\n const _path = error.path.join('.');\n\n if (!fieldsErrors[_path]) {\n fieldsErrors[_path] = { message: error.message, type: error.code };\n }\n\n errors.shift();\n }\n\n return fieldsErrors;\n};\n\nexport const arktypeResolver: Resolver =\n (schema, _schemaOptions, resolverOptions = {}) =>\n (values, _, options) => {\n const out = schema(values);\n\n if (out instanceof ArkErrors) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(out), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n errors: {} as FieldErrors,\n values: resolverOptions.raw ? Object.assign({}, values) : out,\n };\n };\n"],"names":["parseErrorSchema","arkErrors","errors","fieldsErrors","length","error","_path","path","join","message","type","code","shift","arktypeResolver","schema","_schemaOptions","resolverOptions","values","_","options","out","ArkErrors","toNestErrors","shouldUseNativeValidation","validateFieldsNatively","raw","Object","assign"],"mappings":"mHAKA,MAAMA,EAAoBC,IACxB,MAAMC,EAAS,IAAID,GACbE,EAA2C,GAEjD,KAAOD,EAAOE,QAAU,CACtB,MAAMC,EAAQH,EAAO,GACfI,EAAQD,EAAME,KAAKC,KAAK,KAEzBL,EAAaG,KAChBH,EAAaG,GAAS,CAAEG,QAASJ,EAAMI,QAASC,KAAML,EAAMM,OAG9DT,EAAOU,OACT,CAEA,OAAOT,GAGIU,EACXA,CAACC,EAAQC,EAAgBC,EAAkB,KAC3C,CAACC,EAAQC,EAAGC,KACV,MAAMC,EAAMN,EAAOG,GAEnB,OAAIG,aAAeC,EACV,CACLJ,OAAQ,CAAA,EACRf,OAAQoB,EAAatB,EAAiBoB,GAAMD,KAIhDA,EAAQI,2BAA6BC,EAAuB,CAAA,EAAIL,GAEzD,CACLjB,OAAQ,GACRe,OAAQD,EAAgBS,IAAMC,OAAOC,OAAO,CAAE,EAAEV,GAAUG"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{toNestErrors as r,validateFieldsNatively as
|
|
1
|
+
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";import{ArkErrors as o}from"arktype";var t=function(r){for(var e=[].concat(r),o={};e.length;){var t=e[0],a=t.path.join(".");o[a]||(o[a]={message:t.message,type:t.code}),e.shift()}return o},a=function(a,n,s){return void 0===s&&(s={}),function(n,i,f){var u=a(n);return u instanceof o?{values:{},errors:r(t(u),f)}:(f.shouldUseNativeValidation&&e({},f),{errors:{},values:s.raw?Object.assign({},n):u})}};export{a as arktypeResolver};
|
|
2
2
|
//# sourceMappingURL=arktype.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"arktype.module.js","sources":["../src/arktype.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { ArkErrors } from 'arktype';\nimport { FieldError, FieldErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (
|
|
1
|
+
{"version":3,"file":"arktype.module.js","sources":["../src/arktype.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { ArkErrors } from 'arktype';\nimport { FieldError, FieldErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (arkErrors: ArkErrors): Record<string, FieldError> => {\n const errors = [...arkErrors];\n const fieldsErrors: Record<string, FieldError> = {};\n\n for (; errors.length; ) {\n const error = errors[0];\n const _path = error.path.join('.');\n\n if (!fieldsErrors[_path]) {\n fieldsErrors[_path] = { message: error.message, type: error.code };\n }\n\n errors.shift();\n }\n\n return fieldsErrors;\n};\n\nexport const arktypeResolver: Resolver =\n (schema, _schemaOptions, resolverOptions = {}) =>\n (values, _, options) => {\n const out = schema(values);\n\n if (out instanceof ArkErrors) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(out), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n errors: {} as FieldErrors,\n values: resolverOptions.raw ? Object.assign({}, values) : out,\n };\n };\n"],"names":["parseErrorSchema","arkErrors","errors","concat","fieldsErrors","length","error","_path","path","join","message","type","code","shift","arktypeResolver","schema","_schemaOptions","resolverOptions","values","_","options","out","ArkErrors","toNestErrors","shouldUseNativeValidation","validateFieldsNatively","raw","Object","assign"],"mappings":"mHAKA,IAAMA,EAAmB,SAACC,GAIxB,IAHA,IAAMC,EAAMC,GAAAA,OAAOF,GACbG,EAA2C,GAE1CF,EAAOG,QAAU,CACtB,IAAMC,EAAQJ,EAAO,GACfK,EAAQD,EAAME,KAAKC,KAAK,KAEzBL,EAAaG,KAChBH,EAAaG,GAAS,CAAEG,QAASJ,EAAMI,QAASC,KAAML,EAAMM,OAG9DV,EAAOW,OACT,CAEA,OAAOT,CACT,EAEaU,EACX,SAACC,EAAQC,EAAgBC,GACzB,YADyBA,IAAAA,IAAAA,EAAkB,CAAE,GAC7C,SAACC,EAAQC,EAAGC,GACV,IAAMC,EAAMN,EAAOG,GAEnB,OAAIG,aAAeC,EACV,CACLJ,OAAQ,GACRhB,OAAQqB,EAAavB,EAAiBqB,GAAMD,KAIhDA,EAAQI,2BAA6BC,EAAuB,GAAIL,GAEzD,CACLlB,OAAQ,GACRgB,OAAQD,EAAgBS,IAAMC,OAAOC,OAAO,GAAIV,GAAUG,GAE9D,CAAC"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e,
|
|
1
|
+
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@hookform/resolvers"),require("arktype")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers","arktype"],r):r((e||self).hookformResolversArktype={},e.hookformResolvers,e.arktype)}(this,function(e,r,o){var t=function(e){for(var r=[].concat(e),o={};r.length;){var t=r[0],s=t.path.join(".");o[s]||(o[s]={message:t.message,type:t.code}),r.shift()}return o};e.arktypeResolver=function(e,s,n){return void 0===n&&(n={}),function(s,i,a){var f=e(s);return f instanceof o.ArkErrors?{values:{},errors:r.toNestErrors(t(f),a)}:(a.shouldUseNativeValidation&&r.validateFieldsNatively({},a),{errors:{},values:n.raw?Object.assign({},s):f})}}});
|
|
2
2
|
//# sourceMappingURL=arktype.umd.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"arktype.umd.js","sources":["../src/arktype.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { ArkErrors } from 'arktype';\nimport { FieldError, FieldErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (
|
|
1
|
+
{"version":3,"file":"arktype.umd.js","sources":["../src/arktype.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { ArkErrors } from 'arktype';\nimport { FieldError, FieldErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (arkErrors: ArkErrors): Record<string, FieldError> => {\n const errors = [...arkErrors];\n const fieldsErrors: Record<string, FieldError> = {};\n\n for (; errors.length; ) {\n const error = errors[0];\n const _path = error.path.join('.');\n\n if (!fieldsErrors[_path]) {\n fieldsErrors[_path] = { message: error.message, type: error.code };\n }\n\n errors.shift();\n }\n\n return fieldsErrors;\n};\n\nexport const arktypeResolver: Resolver =\n (schema, _schemaOptions, resolverOptions = {}) =>\n (values, _, options) => {\n const out = schema(values);\n\n if (out instanceof ArkErrors) {\n return {\n values: {},\n errors: toNestErrors(parseErrorSchema(out), options),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n errors: {} as FieldErrors,\n values: resolverOptions.raw ? Object.assign({}, values) : out,\n };\n };\n"],"names":["parseErrorSchema","arkErrors","errors","concat","fieldsErrors","length","error","_path","path","join","message","type","code","shift","schema","_schemaOptions","resolverOptions","values","_","options","out","ArkErrors","toNestErrors","shouldUseNativeValidation","validateFieldsNatively","raw","Object","assign"],"mappings":"sWAKA,IAAMA,EAAmB,SAACC,GAIxB,IAHA,IAAMC,EAAMC,GAAAA,OAAOF,GACbG,EAA2C,GAE1CF,EAAOG,QAAU,CACtB,IAAMC,EAAQJ,EAAO,GACfK,EAAQD,EAAME,KAAKC,KAAK,KAEzBL,EAAaG,KAChBH,EAAaG,GAAS,CAAEG,QAASJ,EAAMI,QAASC,KAAML,EAAMM,OAG9DV,EAAOW,OACT,CAEA,OAAOT,CACT,oBAGE,SAACU,EAAQC,EAAgBC,GACzB,YADyBA,IAAAA,IAAAA,EAAkB,CAAE,GAC7C,SAACC,EAAQC,EAAGC,GACV,IAAMC,EAAMN,EAAOG,GAEnB,OAAIG,aAAeC,EAAAA,UACV,CACLJ,OAAQ,GACRf,OAAQoB,EAAYA,aAACtB,EAAiBoB,GAAMD,KAIhDA,EAAQI,2BAA6BC,EAAsBA,uBAAC,GAAIL,GAEzD,CACLjB,OAAQ,GACRe,OAAQD,EAAgBS,IAAMC,OAAOC,OAAO,GAAIV,GAAUG,GAE9D,CAAC"}
|
package/arktype/dist/types.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Type } from 'arktype';
|
|
2
2
|
import { FieldValues, ResolverOptions, ResolverResult } from 'react-hook-form';
|
|
3
|
-
export type Resolver = <T extends Type<any>>(schema: T, schemaOptions?: undefined, factoryOptions?: {
|
|
3
|
+
export type Resolver = <T extends Type<any, any>>(schema: T, schemaOptions?: undefined, factoryOptions?: {
|
|
4
4
|
/**
|
|
5
5
|
* Return the raw input values rather than the parsed values.
|
|
6
6
|
* @default false
|
package/arktype/package.json
CHANGED
|
@@ -33,7 +33,7 @@ function TestComponent({ onSubmit }: Props) {
|
|
|
33
33
|
);
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
test("form's native validation with
|
|
36
|
+
test("form's native validation with Arktype", async () => {
|
|
37
37
|
const handleSubmit = vi.fn();
|
|
38
38
|
render(<TestComponent onSubmit={handleSubmit} />);
|
|
39
39
|
|
|
@@ -57,14 +57,14 @@ test("form's native validation with Zod", async () => {
|
|
|
57
57
|
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement;
|
|
58
58
|
expect(usernameField.validity.valid).toBe(false);
|
|
59
59
|
expect(usernameField.validationMessage).toBe(
|
|
60
|
-
'username must be
|
|
60
|
+
'username must be at least length 2',
|
|
61
61
|
);
|
|
62
62
|
|
|
63
63
|
// password
|
|
64
64
|
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
|
|
65
65
|
expect(passwordField.validity.valid).toBe(false);
|
|
66
66
|
expect(passwordField.validationMessage).toBe(
|
|
67
|
-
'password must be
|
|
67
|
+
'password must be at least length 2',
|
|
68
68
|
);
|
|
69
69
|
|
|
70
70
|
await user.type(screen.getByPlaceholderText(/username/i), 'joe');
|
|
@@ -47,10 +47,10 @@ test("form's validation with arkType and TypeScript's integration", async () =>
|
|
|
47
47
|
await user.click(screen.getByText(/submit/i));
|
|
48
48
|
|
|
49
49
|
expect(
|
|
50
|
-
screen.getByText('username must be
|
|
50
|
+
screen.getByText('username must be at least length 2'),
|
|
51
51
|
).toBeInTheDocument();
|
|
52
52
|
expect(
|
|
53
|
-
screen.getByText('password must be
|
|
53
|
+
screen.getByText('password must be at least length 2'),
|
|
54
54
|
).toBeInTheDocument();
|
|
55
55
|
expect(handleSubmit).not.toHaveBeenCalled();
|
|
56
56
|
});
|