@hookform/resolvers 4.0.0 → 4.1.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 +35 -2
- package/ajv/dist/ajv.d.ts +20 -0
- package/ajv/dist/ajv.js.map +1 -1
- package/ajv/dist/ajv.modern.mjs.map +1 -1
- package/ajv/dist/ajv.module.js.map +1 -1
- package/ajv/dist/ajv.umd.js.map +1 -1
- package/ajv/src/ajv.ts +20 -0
- package/arktype/dist/arktype.d.ts +20 -2
- 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/index.d.ts +0 -1
- package/arktype/src/__tests__/Form.tsx +32 -6
- package/arktype/src/__tests__/__fixtures__/data.ts +1 -1
- package/arktype/src/arktype.ts +29 -9
- package/arktype/src/index.ts +0 -1
- package/class-validator/dist/class-validator.d.ts +29 -2
- package/class-validator/dist/class-validator.js +1 -1
- package/class-validator/dist/class-validator.js.map +1 -1
- package/class-validator/dist/class-validator.mjs +1 -1
- package/class-validator/dist/class-validator.modern.mjs +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/index.d.ts +0 -1
- package/class-validator/src/__tests__/Form.tsx +26 -0
- package/class-validator/src/__tests__/__fixtures__/data.ts +1 -1
- package/class-validator/src/__tests__/class-validator.ts +21 -10
- package/class-validator/src/class-validator.ts +46 -12
- package/class-validator/src/index.ts +0 -1
- package/computed-types/dist/computed-types.d.ts +18 -2
- package/computed-types/dist/computed-types.js.map +1 -1
- package/computed-types/dist/computed-types.mjs +1 -1
- package/computed-types/dist/computed-types.modern.mjs +1 -1
- package/computed-types/dist/computed-types.modern.mjs.map +1 -1
- package/computed-types/dist/computed-types.module.js +1 -1
- package/computed-types/dist/computed-types.module.js.map +1 -1
- package/computed-types/dist/computed-types.umd.js.map +1 -1
- package/computed-types/dist/index.d.ts +0 -1
- package/computed-types/src/__tests__/Form-native-validation.tsx +2 -4
- package/computed-types/src/__tests__/Form.tsx +30 -6
- package/computed-types/src/__tests__/__fixtures__/data.ts +1 -1
- package/computed-types/src/__tests__/computed-types.ts +2 -0
- package/computed-types/src/computed-types.ts +24 -7
- package/computed-types/src/index.ts +0 -1
- package/effect-ts/dist/effect-ts.d.ts +19 -2
- 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.map +1 -1
- package/effect-ts/dist/index.d.ts +0 -1
- package/effect-ts/src/__tests__/Form.tsx +30 -4
- package/effect-ts/src/__tests__/__fixtures__/data.ts +1 -1
- package/effect-ts/src/effect-ts.ts +30 -8
- package/effect-ts/src/index.ts +0 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.d.ts +21 -0
- package/fluentvalidation-ts/dist/fluentvalidation-ts.js +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.js.map +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.mjs +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.modern.mjs +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.modern.mjs.map +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.module.js +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.module.js.map +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.umd.js +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.umd.js.map +1 -1
- package/fluentvalidation-ts/src/fluentvalidation-ts.ts +24 -3
- package/io-ts/dist/errorsToRecord.d.ts +5 -1
- package/io-ts/dist/index.d.ts +0 -1
- package/io-ts/dist/io-ts.d.ts +19 -2
- 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.map +1 -1
- package/io-ts/src/__tests__/Form-native-validation.tsx +4 -11
- package/io-ts/src/__tests__/Form.tsx +30 -6
- package/io-ts/src/__tests__/__fixtures__/data.ts +2 -19
- package/io-ts/src/errorsToRecord.ts +4 -1
- package/io-ts/src/index.ts +0 -1
- package/io-ts/src/io-ts.ts +47 -25
- package/joi/dist/joi.d.ts +17 -0
- package/joi/dist/joi.js.map +1 -1
- package/joi/dist/joi.modern.mjs.map +1 -1
- package/joi/dist/joi.module.js.map +1 -1
- package/joi/dist/joi.umd.js.map +1 -1
- package/joi/src/joi.ts +17 -0
- package/nope/dist/nope.d.ts +17 -0
- package/nope/dist/nope.js +1 -1
- package/nope/dist/nope.js.map +1 -1
- package/nope/dist/nope.mjs +1 -1
- package/nope/dist/nope.modern.mjs.map +1 -1
- package/nope/dist/nope.module.js +1 -1
- package/nope/dist/nope.module.js.map +1 -1
- package/nope/dist/nope.umd.js +1 -1
- package/nope/dist/nope.umd.js.map +1 -1
- package/nope/src/nope.ts +17 -0
- package/package.json +4 -1
- package/standard-schema/dist/index.d.ts +0 -1
- package/standard-schema/dist/standard-schema.d.ts +25 -2
- 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/src/__tests__/Form.tsx +32 -6
- package/standard-schema/src/__tests__/__fixtures__/data.ts +44 -21
- package/standard-schema/src/__tests__/__snapshots__/standard-schema.ts.snap +198 -11
- package/standard-schema/src/__tests__/standard-schema.ts +30 -2
- package/standard-schema/src/index.ts +0 -1
- package/standard-schema/src/standard-schema.ts +54 -11
- package/superstruct/dist/index.d.ts +0 -1
- package/superstruct/dist/superstruct.d.ts +22 -2
- 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.map +1 -1
- package/superstruct/src/__tests__/Form.tsx +26 -0
- package/superstruct/src/__tests__/__fixtures__/data.ts +1 -1
- package/superstruct/src/index.ts +0 -1
- package/superstruct/src/superstruct.ts +31 -9
- package/typanion/dist/index.d.ts +0 -1
- package/typanion/dist/typanion.d.ts +18 -2
- package/typanion/dist/typanion.js.map +1 -1
- package/typanion/dist/typanion.mjs +1 -1
- package/typanion/dist/typanion.modern.mjs +1 -1
- package/typanion/dist/typanion.modern.mjs.map +1 -1
- package/typanion/dist/typanion.module.js +1 -1
- package/typanion/dist/typanion.module.js.map +1 -1
- package/typanion/dist/typanion.umd.js.map +1 -1
- package/typanion/src/__tests__/Form-native-validation.tsx +6 -14
- package/typanion/src/__tests__/Form.tsx +32 -8
- package/typanion/src/__tests__/__fixtures__/data.ts +9 -9
- package/typanion/src/__tests__/typanion.ts +3 -3
- package/typanion/src/index.ts +0 -1
- package/typanion/src/typanion.ts +32 -9
- package/typebox/dist/index.d.ts +0 -1
- package/typebox/dist/typebox.d.ts +20 -2
- package/typebox/dist/typebox.js +1 -1
- package/typebox/dist/typebox.js.map +1 -1
- package/typebox/dist/typebox.mjs +1 -1
- package/typebox/dist/typebox.modern.mjs +1 -1
- package/typebox/dist/typebox.modern.mjs.map +1 -1
- package/typebox/dist/typebox.module.js +1 -1
- package/typebox/dist/typebox.module.js.map +1 -1
- package/typebox/dist/typebox.umd.js +1 -1
- package/typebox/dist/typebox.umd.js.map +1 -1
- package/typebox/src/__tests__/Form-compiler.tsx +4 -8
- package/typebox/src/__tests__/Form.tsx +32 -6
- package/typebox/src/__tests__/__fixtures__/data.ts +1 -1
- package/typebox/src/index.ts +0 -1
- package/typebox/src/typebox.ts +33 -7
- package/typeschema/dist/typeschema.d.ts +17 -0
- package/typeschema/dist/typeschema.js.map +1 -1
- package/typeschema/dist/typeschema.modern.mjs.map +1 -1
- package/typeschema/dist/typeschema.module.js.map +1 -1
- package/typeschema/dist/typeschema.umd.js.map +1 -1
- package/typeschema/src/typeschema.ts +17 -0
- package/valibot/dist/index.d.ts +0 -1
- package/valibot/dist/valibot.d.ts +31 -2
- 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.map +1 -1
- package/valibot/src/__tests__/Form.tsx +26 -0
- package/valibot/src/__tests__/__fixtures__/data.ts +7 -3
- package/valibot/src/index.ts +0 -1
- package/valibot/src/valibot.ts +52 -5
- package/vest/dist/vest.d.ts +18 -0
- package/vest/dist/vest.js.map +1 -1
- package/vest/dist/vest.modern.mjs.map +1 -1
- package/vest/dist/vest.module.js.map +1 -1
- package/vest/dist/vest.umd.js.map +1 -1
- package/vest/src/vest.ts +18 -0
- package/vine/dist/index.d.ts +0 -1
- package/vine/dist/vine.d.ts +25 -2
- 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/__tests__/Form-native-validation.tsx +2 -4
- package/vine/src/__tests__/Form.tsx +30 -6
- package/vine/src/__tests__/__fixtures__/data.ts +1 -1
- package/vine/src/index.ts +0 -1
- package/vine/src/vine.ts +37 -9
- package/yup/dist/yup.d.ts +18 -7
- 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/yup.ts +21 -10
- package/zod/dist/index.d.ts +0 -1
- package/zod/dist/zod.d.ts +25 -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__/Form.tsx +30 -6
- package/zod/src/__tests__/__fixtures__/data.ts +3 -3
- package/zod/src/index.ts +0 -1
- package/zod/src/zod.ts +39 -8
- package/arktype/dist/types.d.ts +0 -9
- package/arktype/src/types.ts +0 -18
- package/class-validator/dist/types.d.ts +0 -12
- package/class-validator/src/types.ts +0 -16
- package/computed-types/dist/types.d.ts +0 -2
- package/computed-types/src/types.ts +0 -13
- package/effect-ts/dist/types.d.ts +0 -4
- package/effect-ts/src/types.ts +0 -12
- package/io-ts/dist/types.d.ts +0 -7
- package/io-ts/src/types.ts +0 -18
- package/standard-schema/dist/types.d.ts +0 -3
- package/standard-schema/src/types.ts +0 -10
- package/superstruct/dist/types.d.ts +0 -11
- package/superstruct/src/types.ts +0 -20
- package/typanion/dist/types.d.ts +0 -9
- package/typanion/src/types.ts +0 -20
- package/typebox/dist/types.d.ts +0 -4
- package/typebox/src/types.ts +0 -11
- package/valibot/dist/types.d.ts +0 -13
- package/valibot/src/types.ts +0 -34
- package/vine/dist/types.d.ts +0 -6
- package/vine/src/types.ts +0 -13
- package/zod/dist/types.d.ts +0 -13
- package/zod/src/types.ts +0 -22
|
@@ -4,60 +4,247 @@ exports[`standardSchemaResolver > should return a single error from standardSche
|
|
|
4
4
|
{
|
|
5
5
|
"errors": {
|
|
6
6
|
"accessToken": {
|
|
7
|
-
"message": "
|
|
7
|
+
"message": "Invalid input",
|
|
8
8
|
"ref": undefined,
|
|
9
|
+
"type": "",
|
|
9
10
|
},
|
|
10
11
|
"birthYear": {
|
|
11
|
-
"message": "
|
|
12
|
+
"message": "Expected number, received string",
|
|
12
13
|
"ref": undefined,
|
|
14
|
+
"type": "",
|
|
13
15
|
},
|
|
14
16
|
"dateStr": {
|
|
15
|
-
"message": "
|
|
17
|
+
"message": "Required",
|
|
16
18
|
"ref": undefined,
|
|
19
|
+
"type": "",
|
|
17
20
|
},
|
|
18
21
|
"email": {
|
|
19
|
-
"message": "
|
|
22
|
+
"message": "Invalid email",
|
|
20
23
|
"ref": {
|
|
21
24
|
"name": "email",
|
|
22
25
|
},
|
|
26
|
+
"type": "",
|
|
23
27
|
},
|
|
24
28
|
"enabled": {
|
|
25
|
-
"message": "
|
|
29
|
+
"message": "Required",
|
|
26
30
|
"ref": undefined,
|
|
31
|
+
"type": "",
|
|
27
32
|
},
|
|
28
33
|
"like": [
|
|
29
34
|
{
|
|
30
35
|
"id": {
|
|
31
|
-
"message": "
|
|
36
|
+
"message": "Expected number, received string",
|
|
32
37
|
"ref": undefined,
|
|
38
|
+
"type": "",
|
|
33
39
|
},
|
|
34
40
|
"name": {
|
|
35
|
-
"message": "
|
|
41
|
+
"message": "Required",
|
|
36
42
|
"ref": undefined,
|
|
43
|
+
"type": "",
|
|
37
44
|
},
|
|
38
45
|
},
|
|
39
46
|
],
|
|
40
47
|
"password": {
|
|
41
|
-
"message": "
|
|
48
|
+
"message": "One uppercase character",
|
|
42
49
|
"ref": {
|
|
43
50
|
"name": "password",
|
|
44
51
|
},
|
|
52
|
+
"type": "",
|
|
45
53
|
},
|
|
46
54
|
"repeatPassword": {
|
|
47
|
-
"message": "
|
|
55
|
+
"message": "Required",
|
|
48
56
|
"ref": undefined,
|
|
57
|
+
"type": "",
|
|
49
58
|
},
|
|
50
59
|
"tags": {
|
|
51
|
-
"message": "
|
|
60
|
+
"message": "Required",
|
|
52
61
|
"ref": undefined,
|
|
62
|
+
"type": "",
|
|
63
|
+
},
|
|
64
|
+
"url": {
|
|
65
|
+
"message": "Custom error url",
|
|
66
|
+
"ref": undefined,
|
|
67
|
+
"type": "",
|
|
68
|
+
},
|
|
69
|
+
"username": {
|
|
70
|
+
"message": "Required",
|
|
71
|
+
"ref": {
|
|
72
|
+
"name": "username",
|
|
73
|
+
},
|
|
74
|
+
"type": "",
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
"values": {},
|
|
78
|
+
}
|
|
79
|
+
`;
|
|
80
|
+
|
|
81
|
+
exports[`standardSchemaResolver > should return all the errors from standardSchemaResolver when validation fails with \`validateAllFieldCriteria\` set to true 1`] = `
|
|
82
|
+
{
|
|
83
|
+
"errors": {
|
|
84
|
+
"accessToken": {
|
|
85
|
+
"message": "Invalid input",
|
|
86
|
+
"ref": undefined,
|
|
87
|
+
"type": "",
|
|
88
|
+
"types": {
|
|
89
|
+
"0": "Invalid input",
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
"birthYear": {
|
|
93
|
+
"message": "Expected number, received string",
|
|
94
|
+
"ref": undefined,
|
|
95
|
+
"type": "",
|
|
96
|
+
"types": {
|
|
97
|
+
"0": "Expected number, received string",
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
"dateStr": {
|
|
101
|
+
"message": "Required",
|
|
102
|
+
"ref": undefined,
|
|
103
|
+
"type": "",
|
|
104
|
+
"types": {
|
|
105
|
+
"0": "Required",
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
"email": {
|
|
109
|
+
"message": "Invalid email",
|
|
110
|
+
"ref": {
|
|
111
|
+
"name": "email",
|
|
112
|
+
},
|
|
113
|
+
"type": "",
|
|
114
|
+
"types": {
|
|
115
|
+
"0": "Invalid email",
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
"enabled": {
|
|
119
|
+
"message": "Required",
|
|
120
|
+
"ref": undefined,
|
|
121
|
+
"type": "",
|
|
122
|
+
"types": {
|
|
123
|
+
"0": "Required",
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
"like": [
|
|
127
|
+
{
|
|
128
|
+
"id": {
|
|
129
|
+
"message": "Expected number, received string",
|
|
130
|
+
"ref": undefined,
|
|
131
|
+
"type": "",
|
|
132
|
+
"types": {
|
|
133
|
+
"0": "Expected number, received string",
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
"name": {
|
|
137
|
+
"message": "Required",
|
|
138
|
+
"ref": undefined,
|
|
139
|
+
"type": "",
|
|
140
|
+
"types": {
|
|
141
|
+
"0": "Required",
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
],
|
|
146
|
+
"password": {
|
|
147
|
+
"message": "One uppercase character",
|
|
148
|
+
"ref": {
|
|
149
|
+
"name": "password",
|
|
150
|
+
},
|
|
151
|
+
"type": "",
|
|
152
|
+
"types": {
|
|
153
|
+
"0": "One uppercase character",
|
|
154
|
+
"1": "One lowercase character",
|
|
155
|
+
"2": "One number",
|
|
156
|
+
"3": "Must be at least 8 characters in length",
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
"repeatPassword": {
|
|
160
|
+
"message": "Required",
|
|
161
|
+
"ref": undefined,
|
|
162
|
+
"type": "",
|
|
163
|
+
"types": {
|
|
164
|
+
"0": "Required",
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
"tags": {
|
|
168
|
+
"message": "Required",
|
|
169
|
+
"ref": undefined,
|
|
170
|
+
"type": "",
|
|
171
|
+
"types": {
|
|
172
|
+
"0": "Required",
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
"url": {
|
|
176
|
+
"message": "Custom error url",
|
|
177
|
+
"ref": undefined,
|
|
178
|
+
"type": "",
|
|
179
|
+
"types": {
|
|
180
|
+
"0": "Custom error url",
|
|
181
|
+
},
|
|
53
182
|
},
|
|
54
183
|
"username": {
|
|
55
|
-
"message": "
|
|
184
|
+
"message": "Required",
|
|
56
185
|
"ref": {
|
|
57
186
|
"name": "username",
|
|
58
187
|
},
|
|
188
|
+
"type": "",
|
|
189
|
+
"types": {
|
|
190
|
+
"0": "Required",
|
|
191
|
+
},
|
|
59
192
|
},
|
|
60
193
|
},
|
|
61
194
|
"values": {},
|
|
62
195
|
}
|
|
63
196
|
`;
|
|
197
|
+
|
|
198
|
+
exports[`standardSchemaResolver > should return values from standardSchemaResolver when validation pass & raw=true 1`] = `
|
|
199
|
+
{
|
|
200
|
+
"errors": {},
|
|
201
|
+
"values": {
|
|
202
|
+
"accessToken": "accessToken",
|
|
203
|
+
"birthYear": 2000,
|
|
204
|
+
"dateStr": "2020-01-01T00:00:00.000Z",
|
|
205
|
+
"email": "john@doe.com",
|
|
206
|
+
"enabled": true,
|
|
207
|
+
"like": [
|
|
208
|
+
{
|
|
209
|
+
"id": 1,
|
|
210
|
+
"name": "name",
|
|
211
|
+
},
|
|
212
|
+
],
|
|
213
|
+
"password": "Password123_",
|
|
214
|
+
"repeatPassword": "Password123_",
|
|
215
|
+
"tags": [
|
|
216
|
+
"tag1",
|
|
217
|
+
"tag2",
|
|
218
|
+
],
|
|
219
|
+
"url": "https://react-hook-form.com/",
|
|
220
|
+
"username": "Doe",
|
|
221
|
+
},
|
|
222
|
+
}
|
|
223
|
+
`;
|
|
224
|
+
|
|
225
|
+
exports[`standardSchemaResolver > should return values from standardSchemaResolver when validation pass 1`] = `
|
|
226
|
+
{
|
|
227
|
+
"errors": {},
|
|
228
|
+
"values": {
|
|
229
|
+
"accessToken": "accessToken",
|
|
230
|
+
"birthYear": 2000,
|
|
231
|
+
"dateStr": 2020-01-01T00:00:00.000Z,
|
|
232
|
+
"email": "john@doe.com",
|
|
233
|
+
"enabled": true,
|
|
234
|
+
"like": [
|
|
235
|
+
{
|
|
236
|
+
"id": 1,
|
|
237
|
+
"name": "name",
|
|
238
|
+
},
|
|
239
|
+
],
|
|
240
|
+
"password": "Password123_",
|
|
241
|
+
"repeatPassword": "Password123_",
|
|
242
|
+
"tags": [
|
|
243
|
+
"tag1",
|
|
244
|
+
"tag2",
|
|
245
|
+
],
|
|
246
|
+
"url": "https://react-hook-form.com/",
|
|
247
|
+
"username": "Doe",
|
|
248
|
+
},
|
|
249
|
+
}
|
|
250
|
+
`;
|
|
@@ -4,13 +4,13 @@ import { fields, invalidData, schema, validData } from './__fixtures__/data';
|
|
|
4
4
|
const shouldUseNativeValidation = false;
|
|
5
5
|
|
|
6
6
|
describe('standardSchemaResolver', () => {
|
|
7
|
-
it('should return values from standardSchemaResolver when validation pass
|
|
7
|
+
it('should return values from standardSchemaResolver when validation pass', async () => {
|
|
8
8
|
const result = await standardSchemaResolver(schema)(validData, undefined, {
|
|
9
9
|
fields,
|
|
10
10
|
shouldUseNativeValidation,
|
|
11
11
|
});
|
|
12
12
|
|
|
13
|
-
expect(result).
|
|
13
|
+
expect(result).toMatchSnapshot();
|
|
14
14
|
});
|
|
15
15
|
|
|
16
16
|
it('should return a single error from standardSchemaResolver when validation fails', async () => {
|
|
@@ -25,4 +25,32 @@ describe('standardSchemaResolver', () => {
|
|
|
25
25
|
|
|
26
26
|
expect(result).toMatchSnapshot();
|
|
27
27
|
});
|
|
28
|
+
|
|
29
|
+
it('should return all the errors from standardSchemaResolver when validation fails with `validateAllFieldCriteria` set to true', async () => {
|
|
30
|
+
const result = await standardSchemaResolver(schema)(
|
|
31
|
+
invalidData,
|
|
32
|
+
undefined,
|
|
33
|
+
{
|
|
34
|
+
fields,
|
|
35
|
+
criteriaMode: 'all',
|
|
36
|
+
shouldUseNativeValidation,
|
|
37
|
+
},
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
expect(result).toMatchSnapshot();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('should return values from standardSchemaResolver when validation pass & raw=true', async () => {
|
|
44
|
+
const validateSpy = vi.spyOn(schema['~standard'], 'validate');
|
|
45
|
+
|
|
46
|
+
const result = await standardSchemaResolver(schema, {
|
|
47
|
+
raw: true,
|
|
48
|
+
})(validData, undefined, {
|
|
49
|
+
fields,
|
|
50
|
+
shouldUseNativeValidation,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
expect(validateSpy).toHaveBeenCalledTimes(1);
|
|
54
|
+
expect(result).toMatchSnapshot();
|
|
55
|
+
});
|
|
28
56
|
});
|
|
@@ -1,34 +1,76 @@
|
|
|
1
1
|
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
|
|
2
2
|
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
3
|
-
import { FieldError } from 'react-hook-form';
|
|
4
|
-
import type { Resolver } from './types';
|
|
3
|
+
import { FieldError, FieldValues, Resolver } from 'react-hook-form';
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
function parseIssues(
|
|
6
|
+
issues: readonly StandardSchemaV1.Issue[],
|
|
7
|
+
validateAllFieldCriteria: boolean,
|
|
8
|
+
) {
|
|
7
9
|
const errors: Record<string, FieldError> = {};
|
|
8
10
|
|
|
9
11
|
for (let i = 0; i < issues.length; i++) {
|
|
10
|
-
const
|
|
11
|
-
const path =
|
|
12
|
+
const error = issues[i];
|
|
13
|
+
const path = error.path?.join('.');
|
|
12
14
|
|
|
13
15
|
if (path) {
|
|
14
16
|
if (!errors[path]) {
|
|
15
|
-
errors[path] = { message:
|
|
17
|
+
errors[path] = { message: error.message, type: '' };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (validateAllFieldCriteria) {
|
|
21
|
+
const types = errors[path].types || {};
|
|
22
|
+
|
|
23
|
+
errors[path].types = {
|
|
24
|
+
...types,
|
|
25
|
+
[Object.keys(types).length]: error.message,
|
|
26
|
+
};
|
|
16
27
|
}
|
|
17
28
|
}
|
|
18
29
|
}
|
|
19
30
|
|
|
20
31
|
return errors;
|
|
21
|
-
}
|
|
32
|
+
}
|
|
22
33
|
|
|
23
|
-
|
|
24
|
-
|
|
34
|
+
/**
|
|
35
|
+
* Creates a resolver for react-hook-form that validates data using a Standard Schema.
|
|
36
|
+
*
|
|
37
|
+
* @param {Schema} schema - The Standard Schema to validate against
|
|
38
|
+
* @param {Object} resolverOptions - Options for the resolver
|
|
39
|
+
* @param {boolean} [resolverOptions.raw=false] - Whether to return raw input values instead of parsed values
|
|
40
|
+
* @returns {Resolver} A resolver function compatible with react-hook-form
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const schema = z.object({
|
|
45
|
+
* name: z.string().min(2),
|
|
46
|
+
* age: z.number().min(18)
|
|
47
|
+
* });
|
|
48
|
+
*
|
|
49
|
+
* useForm({
|
|
50
|
+
* resolver: standardSchemaResolver(schema)
|
|
51
|
+
* });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export function standardSchemaResolver<
|
|
55
|
+
TFieldValues extends FieldValues,
|
|
56
|
+
Schema extends StandardSchemaV1<TFieldValues, any>,
|
|
57
|
+
>(
|
|
58
|
+
schema: Schema,
|
|
59
|
+
resolverOptions: {
|
|
60
|
+
raw?: boolean;
|
|
61
|
+
} = {},
|
|
62
|
+
): Resolver<NonNullable<(typeof schema)['~standard']['types']>['output']> {
|
|
63
|
+
return async (values: TFieldValues, _, options) => {
|
|
25
64
|
let result = schema['~standard'].validate(values);
|
|
26
65
|
if (result instanceof Promise) {
|
|
27
66
|
result = await result;
|
|
28
67
|
}
|
|
29
68
|
|
|
30
69
|
if (result.issues) {
|
|
31
|
-
const errors = parseIssues(
|
|
70
|
+
const errors = parseIssues(
|
|
71
|
+
result.issues,
|
|
72
|
+
!options.shouldUseNativeValidation && options.criteriaMode === 'all',
|
|
73
|
+
);
|
|
32
74
|
|
|
33
75
|
return {
|
|
34
76
|
values: {},
|
|
@@ -39,7 +81,8 @@ export const standardSchemaResolver: Resolver =
|
|
|
39
81
|
options.shouldUseNativeValidation && validateFieldsNatively({}, options);
|
|
40
82
|
|
|
41
83
|
return {
|
|
42
|
-
values: values,
|
|
84
|
+
values: resolverOptions.raw ? Object.assign({}, values) : result.value,
|
|
43
85
|
errors: {},
|
|
44
86
|
};
|
|
45
87
|
};
|
|
88
|
+
}
|
|
@@ -1,2 +1,22 @@
|
|
|
1
|
-
import { Resolver } from '
|
|
2
|
-
|
|
1
|
+
import { FieldValues, Resolver } from 'react-hook-form';
|
|
2
|
+
import { Infer, Struct, validate } from 'superstruct';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a resolver for react-hook-form using Superstruct schema validation
|
|
5
|
+
* @param {Struct<TFieldValues, any>} schema - The Superstruct schema to validate against
|
|
6
|
+
* @param {Parameters<typeof validate>[2]} [schemaOptions] - Optional Superstruct validation options
|
|
7
|
+
* @param {Object} resolverOptions - Additional resolver configuration
|
|
8
|
+
* @param {boolean} [resolverOptions.raw=false] - If true, returns raw values rather than validated results
|
|
9
|
+
* @returns {Resolver<Infer<typeof schema>>} A resolver function compatible with react-hook-form
|
|
10
|
+
* @example
|
|
11
|
+
* const schema = struct({
|
|
12
|
+
* name: string(),
|
|
13
|
+
* age: number()
|
|
14
|
+
* });
|
|
15
|
+
*
|
|
16
|
+
* useForm({
|
|
17
|
+
* resolver: superstructResolver(schema)
|
|
18
|
+
* });
|
|
19
|
+
*/
|
|
20
|
+
export declare function superstructResolver<TFieldValues extends FieldValues>(schema: Struct<TFieldValues, any>, schemaOptions?: Parameters<typeof validate>[2], resolverOptions?: {
|
|
21
|
+
raw?: boolean;
|
|
22
|
+
}): Resolver<Infer<typeof schema>>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"superstruct.js","sources":["../src/superstruct.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError } from 'react-hook-form';\
|
|
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/**\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<TFieldValues extends FieldValues>(\n schema: Struct<TFieldValues, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions: {\n raw?: boolean;\n } = {},\n): Resolver<Infer<typeof schema>> {\n return (values, _, 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":"mGAiCEA,EACAC,EACAC,GAIA,gBAJAA,IAAAA,EAEI,CAAE,GAEEC,SAAAA,EAAQC,EAAGC,GACjB,IApCsBC,EAoChBC,EAASC,EAAAA,SAASL,EAAQH,EAAQC,GAExC,OAAIM,EAAO,GACF,CACLJ,OAAQ,CAAA,EACRM,OAAQC,EAAYA,cAzCFJ,EAyCoBC,EAAO,GAxC5CD,EAAMK,WAAWC,OACtB,SAACC,EAAUP,GACT,OAACO,EAASP,EAAMQ,KAAKC,KAAK,MAAQ,CAChCC,QAASV,EAAMU,QACfC,KAAMX,EAAMW,QACRJ,CAAQ,EAChB,CAAA,IAkCsDR,KAItDA,EAAQa,2BAA6BC,yBAAuB,CAAA,EAAId,GAEzD,CACLF,OAAQD,EAAgBkB,IAAMC,OAAOC,OAAO,CAAE,EAAEnB,GAAUI,EAAO,GACjEE,OAAQ,CAAA,GAEZ,CACF"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";import{validate as o}from"superstruct";
|
|
1
|
+
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";import{validate as o}from"superstruct";function s(s,t,u){return void 0===u&&(u={}),function(a,i,n){var f,m=o(a,s,t);return m[0]?{values:{},errors:r((f=m[0],f.failures().reduce(function(r,e){return(r[e.path.join(".")]={message:e.message,type:e.type})&&r},{})),n)}:(n.shouldUseNativeValidation&&e({},n),{values:u.raw?Object.assign({},a):m[1],errors:{}})}}export{s as superstructResolver};
|
|
2
2
|
//# sourceMappingURL=superstruct.module.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";import{validate as s}from"superstruct";
|
|
1
|
+
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";import{validate as s}from"superstruct";function o(o,t,a={}){return(u,i,n)=>{const m=s(u,o,t);return m[0]?{values:{},errors:r((p=m[0],p.failures().reduce((r,e)=>(r[e.path.join(".")]={message:e.message,type:e.type})&&r,{})),n)}:(n.shouldUseNativeValidation&&e({},n),{values:a.raw?Object.assign({},u):m[1],errors:{}});var p}}export{o as superstructResolver};
|
|
2
2
|
//# sourceMappingURL=superstruct.modern.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"superstruct.modern.mjs","sources":["../src/superstruct.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError } from 'react-hook-form';\
|
|
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/**\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<TFieldValues extends FieldValues>(\n schema: Struct<TFieldValues, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions: {\n raw?: boolean;\n } = {},\n): Resolver<Infer<typeof schema>> {\n return (values, _, 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":"sHAgCgB,SAAAA,EACdC,EACAC,EACAC,EAEI,CAAE,GAEN,MAAO,CAACC,EAAQC,EAAGC,KACjB,MAAMC,EAASC,EAASJ,EAAQH,EAAQC,GAExC,OAAIK,EAAO,GACF,CACLH,OAAQ,CAAA,EACRK,OAAQC,GAzCUC,EAyCoBJ,EAAO,GAxC5CI,EAAMC,WAAWC,OACtB,CAACC,EAAUH,KACRG,EAASH,EAAMI,KAAKC,KAAK,MAAQ,CAChCC,QAASN,EAAMM,QACfC,KAAMP,EAAMO,QACRJ,EACR,KAkCsDR,KAItDA,EAAQa,2BAA6BC,EAAuB,GAAId,GAEzD,CACLF,OAAQD,EAAgBkB,IAAMC,OAAOC,OAAO,CAAA,EAAInB,GAAUG,EAAO,GACjEE,OAAQ,CAAA,IAjDd,IAA0BE,EAoD1B"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";import{validate as o}from"superstruct";
|
|
1
|
+
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";import{validate as o}from"superstruct";function s(s,t,u){return void 0===u&&(u={}),function(a,i,n){var f,m=o(a,s,t);return m[0]?{values:{},errors:r((f=m[0],f.failures().reduce(function(r,e){return(r[e.path.join(".")]={message:e.message,type:e.type})&&r},{})),n)}:(n.shouldUseNativeValidation&&e({},n),{values:u.raw?Object.assign({},a):m[1],errors:{}})}}export{s as superstructResolver};
|
|
2
2
|
//# sourceMappingURL=superstruct.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"superstruct.module.js","sources":["../src/superstruct.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError } from 'react-hook-form';\
|
|
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/**\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<TFieldValues extends FieldValues>(\n schema: Struct<TFieldValues, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions: {\n raw?: boolean;\n } = {},\n): Resolver<Infer<typeof schema>> {\n return (values, _, 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":"+HAgCgBA,EACdC,EACAC,EACAC,GAIA,gBAJAA,IAAAA,EAEI,CAAE,GAEEC,SAAAA,EAAQC,EAAGC,GACjB,IApCsBC,EAoChBC,EAASC,EAASL,EAAQH,EAAQC,GAExC,OAAIM,EAAO,GACF,CACLJ,OAAQ,CAAA,EACRM,OAAQC,GAzCUJ,EAyCoBC,EAAO,GAxC5CD,EAAMK,WAAWC,OACtB,SAACC,EAAUP,GACT,OAACO,EAASP,EAAMQ,KAAKC,KAAK,MAAQ,CAChCC,QAASV,EAAMU,QACfC,KAAMX,EAAMW,QACRJ,CAAQ,EAChB,CAAA,IAkCsDR,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 } from 'react-hook-form';\
|
|
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/**\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<TFieldValues extends FieldValues>(\n schema: Struct<TFieldValues, any>,\n schemaOptions?: Parameters<typeof validate>[2],\n resolverOptions: {\n raw?: boolean;\n } = {},\n): Resolver<Infer<typeof schema>> {\n return (values, _, 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":"qZAiCEA,EACAC,EACAC,GAIA,gBAJAA,IAAAA,EAEI,CAAE,GAEEC,SAAAA,EAAQC,EAAGC,GACjB,IApCsBC,EAoChBC,EAASC,EAAAA,SAASL,EAAQH,EAAQC,GAExC,OAAIM,EAAO,GACF,CACLJ,OAAQ,CAAA,EACRM,OAAQC,EAAYA,cAzCFJ,EAyCoBC,EAAO,GAxC5CD,EAAMK,WAAWC,OACtB,SAACC,EAAUP,GACT,OAACO,EAASP,EAAMQ,KAAKC,KAAK,MAAQ,CAChCC,QAASV,EAAMU,QACfC,KAAMX,EAAMW,QACRJ,CAAQ,EAChB,CAAA,IAkCsDR,KAItDA,EAAQa,2BAA6BC,yBAAuB,CAAA,EAAId,GAEzD,CACLF,OAAQD,EAAgBkB,IAAMC,OAAOC,OAAO,CAAE,EAAEnB,GAAUI,EAAO,GACjEE,OAAQ,CAAA,GAEZ,CACF"}
|
|
@@ -58,3 +58,29 @@ test("form's validation with Superstruct and TypeScript's integration", async ()
|
|
|
58
58
|
).toBeInTheDocument();
|
|
59
59
|
expect(handleSubmit).not.toHaveBeenCalled();
|
|
60
60
|
});
|
|
61
|
+
|
|
62
|
+
export function TestComponentManualType({
|
|
63
|
+
onSubmit,
|
|
64
|
+
}: {
|
|
65
|
+
onSubmit: (data: FormData) => void;
|
|
66
|
+
}) {
|
|
67
|
+
const {
|
|
68
|
+
register,
|
|
69
|
+
handleSubmit,
|
|
70
|
+
formState: { errors },
|
|
71
|
+
} = useForm<Infer<typeof schema>, undefined, FormData>({
|
|
72
|
+
resolver: superstructResolver(schema), // Useful to check TypeScript regressions
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
return (
|
|
76
|
+
<form onSubmit={handleSubmit(onSubmit)}>
|
|
77
|
+
<input {...register('username')} />
|
|
78
|
+
{errors.username && <span role="alert">{errors.username.message}</span>}
|
|
79
|
+
|
|
80
|
+
<input {...register('password')} />
|
|
81
|
+
{errors.password && <span role="alert">{errors.password.message}</span>}
|
|
82
|
+
|
|
83
|
+
<button type="submit">submit</button>
|
|
84
|
+
</form>
|
|
85
|
+
);
|
|
86
|
+
}
|
package/superstruct/src/index.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
|
|
2
|
-
import { FieldError } from 'react-hook-form';
|
|
2
|
+
import { FieldError, FieldValues, Resolver } from 'react-hook-form';
|
|
3
|
+
import { Infer, Struct, StructError, validate } from 'superstruct';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const parseErrorSchema = (error: StructError) =>
|
|
8
|
-
error.failures().reduce<Record<string, FieldError>>(
|
|
5
|
+
function parseErrorSchema(error: StructError) {
|
|
6
|
+
return error.failures().reduce<Record<string, FieldError>>(
|
|
9
7
|
(previous, error) =>
|
|
10
8
|
(previous[error.path.join('.')] = {
|
|
11
9
|
message: error.message,
|
|
@@ -13,10 +11,33 @@ const parseErrorSchema = (error: StructError) =>
|
|
|
13
11
|
}) && previous,
|
|
14
12
|
{},
|
|
15
13
|
);
|
|
14
|
+
}
|
|
16
15
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Creates a resolver for react-hook-form using Superstruct schema validation
|
|
18
|
+
* @param {Struct<TFieldValues, any>} schema - The Superstruct schema to validate against
|
|
19
|
+
* @param {Parameters<typeof validate>[2]} [schemaOptions] - Optional Superstruct validation options
|
|
20
|
+
* @param {Object} resolverOptions - Additional resolver configuration
|
|
21
|
+
* @param {boolean} [resolverOptions.raw=false] - If true, returns raw values rather than validated results
|
|
22
|
+
* @returns {Resolver<Infer<typeof schema>>} A resolver function compatible with react-hook-form
|
|
23
|
+
* @example
|
|
24
|
+
* const schema = struct({
|
|
25
|
+
* name: string(),
|
|
26
|
+
* age: number()
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* useForm({
|
|
30
|
+
* resolver: superstructResolver(schema)
|
|
31
|
+
* });
|
|
32
|
+
*/
|
|
33
|
+
export function superstructResolver<TFieldValues extends FieldValues>(
|
|
34
|
+
schema: Struct<TFieldValues, any>,
|
|
35
|
+
schemaOptions?: Parameters<typeof validate>[2],
|
|
36
|
+
resolverOptions: {
|
|
37
|
+
raw?: boolean;
|
|
38
|
+
} = {},
|
|
39
|
+
): Resolver<Infer<typeof schema>> {
|
|
40
|
+
return (values, _, options) => {
|
|
20
41
|
const result = validate(values, schema, schemaOptions);
|
|
21
42
|
|
|
22
43
|
if (result[0]) {
|
|
@@ -33,3 +54,4 @@ export const superstructResolver: Resolver =
|
|
|
33
54
|
errors: {},
|
|
34
55
|
};
|
|
35
56
|
};
|
|
57
|
+
}
|
package/typanion/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,18 @@
|
|
|
1
|
-
import type { Resolver } from '
|
|
2
|
-
|
|
1
|
+
import type { FieldValues, Resolver } from 'react-hook-form';
|
|
2
|
+
import * as t from 'typanion';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a resolver for react-hook-form using Typanion schema validation
|
|
5
|
+
* @param {t.StrictValidator<TFieldValues, TFieldValues>} schema - The Typanion schema to validate against
|
|
6
|
+
* @param {Pick<t.ValidationState, 'coercions' | 'coercion'>} [schemaOptions] - Optional Typanion validation options
|
|
7
|
+
* @returns {Resolver<t.InferType<typeof schema>>} A resolver function compatible with react-hook-form
|
|
8
|
+
* @example
|
|
9
|
+
* const schema = t.isObject({
|
|
10
|
+
* name: t.isString(),
|
|
11
|
+
* age: t.isInteger()
|
|
12
|
+
* });
|
|
13
|
+
*
|
|
14
|
+
* useForm({
|
|
15
|
+
* resolver: typanionResolver(schema)
|
|
16
|
+
* });
|
|
17
|
+
*/
|
|
18
|
+
export declare function typanionResolver<TFieldValues extends FieldValues>(schema: t.StrictValidator<TFieldValues, TFieldValues>, schemaOptions?: Pick<t.ValidationState, 'coercions' | 'coercion'>): Resolver<t.InferType<typeof schema>>;
|