@hookform/resolvers 3.5.0 → 3.7.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 +47 -33
- package/package.json +17 -5
- package/valibot/dist/types.d.ts +2 -2
- 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 +2 -2
- package/valibot/src/__tests__/Form-native-validation.tsx +14 -14
- package/valibot/src/__tests__/Form.tsx +11 -11
- package/valibot/src/__tests__/__fixtures__/data.ts +43 -53
- package/valibot/src/__tests__/__snapshots__/valibot.ts.snap +50 -50
- package/valibot/src/__tests__/valibot.ts +13 -11
- package/valibot/src/types.ts +15 -3
- package/valibot/src/valibot.ts +55 -77
- package/vine/dist/index.d.ts +2 -0
- package/vine/dist/types.d.ts +6 -0
- package/vine/dist/vine.d.ts +2 -0
- package/vine/dist/vine.js +2 -0
- package/vine/dist/vine.js.map +1 -0
- package/vine/dist/vine.mjs +2 -0
- package/vine/dist/vine.modern.mjs +2 -0
- package/vine/dist/vine.modern.mjs.map +1 -0
- package/vine/dist/vine.module.js +2 -0
- package/vine/dist/vine.module.js.map +1 -0
- package/vine/dist/vine.umd.js +2 -0
- package/vine/dist/vine.umd.js.map +1 -0
- package/vine/package.json +18 -0
- package/vine/src/__tests__/Form-native-validation.tsx +81 -0
- package/vine/src/__tests__/Form.tsx +55 -0
- package/vine/src/__tests__/__fixtures__/data.ts +74 -0
- package/vine/src/__tests__/__snapshots__/vine.ts.snap +180 -0
- package/vine/src/__tests__/vine.ts +53 -0
- package/vine/src/index.ts +2 -0
- package/vine/src/types.ts +13 -0
- package/vine/src/vine.ts +66 -0
- 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,81 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { useForm } from 'react-hook-form';
|
|
3
|
+
import { render, screen } from '@testing-library/react';
|
|
4
|
+
import user from '@testing-library/user-event';
|
|
5
|
+
import { vineResolver } from '..';
|
|
6
|
+
import vine from '@vinejs/vine';
|
|
7
|
+
import { Infer } from '@vinejs/vine/build/src/types';
|
|
8
|
+
|
|
9
|
+
const schema = vine.compile(
|
|
10
|
+
vine.object({
|
|
11
|
+
username: vine.string().minLength(1),
|
|
12
|
+
password: vine.string().minLength(1),
|
|
13
|
+
}),
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
type FormData = Infer<typeof schema> & { unusedProperty: string };
|
|
17
|
+
|
|
18
|
+
interface Props {
|
|
19
|
+
onSubmit: (data: FormData) => void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function TestComponent({ onSubmit }: Props) {
|
|
23
|
+
const { register, handleSubmit } = useForm<FormData>({
|
|
24
|
+
resolver: vineResolver(schema),
|
|
25
|
+
shouldUseNativeValidation: true,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<form onSubmit={handleSubmit(onSubmit)}>
|
|
30
|
+
<input {...register('username')} placeholder="username" />
|
|
31
|
+
|
|
32
|
+
<input {...register('password')} placeholder="password" />
|
|
33
|
+
|
|
34
|
+
<button type="submit">submit</button>
|
|
35
|
+
</form>
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
test("form's native validation with Zod", async () => {
|
|
40
|
+
const handleSubmit = vi.fn();
|
|
41
|
+
render(<TestComponent onSubmit={handleSubmit} />);
|
|
42
|
+
|
|
43
|
+
// username
|
|
44
|
+
let usernameField = screen.getByPlaceholderText(
|
|
45
|
+
/username/i,
|
|
46
|
+
) as HTMLInputElement;
|
|
47
|
+
expect(usernameField.validity.valid).toBe(true);
|
|
48
|
+
expect(usernameField.validationMessage).toBe('');
|
|
49
|
+
|
|
50
|
+
// password
|
|
51
|
+
let passwordField = screen.getByPlaceholderText(
|
|
52
|
+
/password/i,
|
|
53
|
+
) as HTMLInputElement;
|
|
54
|
+
expect(passwordField.validity.valid).toBe(true);
|
|
55
|
+
expect(passwordField.validationMessage).toBe('');
|
|
56
|
+
|
|
57
|
+
await user.click(screen.getByText(/submit/i));
|
|
58
|
+
|
|
59
|
+
// username
|
|
60
|
+
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement;
|
|
61
|
+
expect(usernameField.validity.valid).toBe(false);
|
|
62
|
+
expect(usernameField.validationMessage).toBe('The username field must have at least 1 characters');
|
|
63
|
+
|
|
64
|
+
// password
|
|
65
|
+
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
|
|
66
|
+
expect(passwordField.validity.valid).toBe(false);
|
|
67
|
+
expect(passwordField.validationMessage).toBe('The password field must have at least 1 characters');
|
|
68
|
+
|
|
69
|
+
await user.type(screen.getByPlaceholderText(/username/i), 'joe');
|
|
70
|
+
await user.type(screen.getByPlaceholderText(/password/i), 'password');
|
|
71
|
+
|
|
72
|
+
// username
|
|
73
|
+
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement;
|
|
74
|
+
expect(usernameField.validity.valid).toBe(true);
|
|
75
|
+
expect(usernameField.validationMessage).toBe('');
|
|
76
|
+
|
|
77
|
+
// password
|
|
78
|
+
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
|
|
79
|
+
expect(passwordField.validity.valid).toBe(true);
|
|
80
|
+
expect(passwordField.validationMessage).toBe('');
|
|
81
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render, screen } from '@testing-library/react';
|
|
3
|
+
import user from '@testing-library/user-event';
|
|
4
|
+
import { useForm } from 'react-hook-form';
|
|
5
|
+
import { vineResolver } from '..';
|
|
6
|
+
import vine from '@vinejs/vine';
|
|
7
|
+
import { Infer } from '@vinejs/vine/build/src/types';
|
|
8
|
+
|
|
9
|
+
const schema = vine.compile(
|
|
10
|
+
vine.object({
|
|
11
|
+
username: vine.string().minLength(1),
|
|
12
|
+
password: vine.string().minLength(1),
|
|
13
|
+
}),
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
type FormData = Infer<typeof schema> & { unusedProperty: string };
|
|
17
|
+
|
|
18
|
+
interface Props {
|
|
19
|
+
onSubmit: (data: FormData) => void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function TestComponent({ onSubmit }: Props) {
|
|
23
|
+
const {
|
|
24
|
+
register,
|
|
25
|
+
handleSubmit,
|
|
26
|
+
formState: { errors },
|
|
27
|
+
} = useForm<FormData>({
|
|
28
|
+
resolver: vineResolver(schema), // Useful to check TypeScript regressions
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<form onSubmit={handleSubmit(onSubmit)}>
|
|
33
|
+
<input {...register('username')} />
|
|
34
|
+
{errors.username && <span role="alert">{errors.username.message}</span>}
|
|
35
|
+
|
|
36
|
+
<input {...register('password')} />
|
|
37
|
+
{errors.password && <span role="alert">{errors.password.message}</span>}
|
|
38
|
+
|
|
39
|
+
<button type="submit">submit</button>
|
|
40
|
+
</form>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
test("form's validation with Vine and TypeScript's integration", async () => {
|
|
45
|
+
const handleSubmit = vi.fn();
|
|
46
|
+
render(<TestComponent onSubmit={handleSubmit} />);
|
|
47
|
+
|
|
48
|
+
expect(screen.queryAllByRole('alert')).toHaveLength(0);
|
|
49
|
+
|
|
50
|
+
await user.click(screen.getByText(/submit/i));
|
|
51
|
+
|
|
52
|
+
expect(screen.getByText(/The username field must have at least 1 characters/i)).toBeInTheDocument();
|
|
53
|
+
expect(screen.getByText(/The password field must have at least 1 characters/i)).toBeInTheDocument();
|
|
54
|
+
expect(handleSubmit).not.toHaveBeenCalled();
|
|
55
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Field, InternalFieldName } from 'react-hook-form';
|
|
2
|
+
import vine from '@vinejs/vine';
|
|
3
|
+
import { Infer } from '@vinejs/vine/build/src/types';
|
|
4
|
+
|
|
5
|
+
export const schema = vine.compile(
|
|
6
|
+
vine.object({
|
|
7
|
+
username: vine.string().regex(/^\w+$/).minLength(3).maxLength(30),
|
|
8
|
+
password: vine
|
|
9
|
+
.string()
|
|
10
|
+
.regex(new RegExp('.*[A-Z].*'))
|
|
11
|
+
.regex(new RegExp('.*[a-z].*'))
|
|
12
|
+
.regex(new RegExp('.*\\d.*'))
|
|
13
|
+
.regex(new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*'))
|
|
14
|
+
.minLength(8)
|
|
15
|
+
.confirmed({ confirmationField: 'repeatPassword' }),
|
|
16
|
+
repeatPassword: vine.string().sameAs('password'),
|
|
17
|
+
accessToken: vine.unionOfTypes([vine.string(), vine.number()]),
|
|
18
|
+
birthYear: vine.number().min(1900).max(2013),
|
|
19
|
+
email: vine.string().email().optional(),
|
|
20
|
+
tags: vine.array(vine.string()),
|
|
21
|
+
enabled: vine.boolean(),
|
|
22
|
+
like: vine.array(
|
|
23
|
+
vine.object({
|
|
24
|
+
id: vine.number(),
|
|
25
|
+
name: vine.string().fixedLength(4),
|
|
26
|
+
}),
|
|
27
|
+
),
|
|
28
|
+
dateStr: vine.string().transform((value: string) => new Date(value).toISOString()),
|
|
29
|
+
}),
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
export const validData: Infer<typeof schema> = {
|
|
33
|
+
username: 'Doe',
|
|
34
|
+
password: 'Password123_',
|
|
35
|
+
repeatPassword: 'Password123_',
|
|
36
|
+
birthYear: 2000,
|
|
37
|
+
email: 'john@doe.com',
|
|
38
|
+
tags: ['tag1', 'tag2'],
|
|
39
|
+
enabled: true,
|
|
40
|
+
accessToken: 'accessToken',
|
|
41
|
+
like: [
|
|
42
|
+
{
|
|
43
|
+
id: 1,
|
|
44
|
+
name: 'name',
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
dateStr: '2020-01-01T00:00:00.000Z',
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export const invalidData = {
|
|
51
|
+
password: '___',
|
|
52
|
+
email: '',
|
|
53
|
+
birthYear: 'birthYear',
|
|
54
|
+
like: [{ id: 'z' }],
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export const fields: Record<InternalFieldName, Field['_f']> = {
|
|
58
|
+
username: {
|
|
59
|
+
ref: { name: 'username' },
|
|
60
|
+
name: 'username',
|
|
61
|
+
},
|
|
62
|
+
password: {
|
|
63
|
+
ref: { name: 'password' },
|
|
64
|
+
name: 'password',
|
|
65
|
+
},
|
|
66
|
+
email: {
|
|
67
|
+
ref: { name: 'email' },
|
|
68
|
+
name: 'email',
|
|
69
|
+
},
|
|
70
|
+
birthday: {
|
|
71
|
+
ref: { name: 'birthday' },
|
|
72
|
+
name: 'birthday',
|
|
73
|
+
},
|
|
74
|
+
};
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
|
+
|
|
3
|
+
exports[`vineResolver > should return a single error from vineResolver when validation fails 1`] = `
|
|
4
|
+
{
|
|
5
|
+
"errors": {
|
|
6
|
+
"accessToken": {
|
|
7
|
+
"message": "Invalid value provided for accessToken field",
|
|
8
|
+
"ref": undefined,
|
|
9
|
+
"type": "unionOfTypes",
|
|
10
|
+
},
|
|
11
|
+
"birthYear": {
|
|
12
|
+
"message": "The birthYear field must be a number",
|
|
13
|
+
"ref": undefined,
|
|
14
|
+
"type": "number",
|
|
15
|
+
},
|
|
16
|
+
"dateStr": {
|
|
17
|
+
"message": "The dateStr field must be defined",
|
|
18
|
+
"ref": undefined,
|
|
19
|
+
"type": "required",
|
|
20
|
+
},
|
|
21
|
+
"email": {
|
|
22
|
+
"message": "The email field must be a valid email address",
|
|
23
|
+
"ref": {
|
|
24
|
+
"name": "email",
|
|
25
|
+
},
|
|
26
|
+
"type": "email",
|
|
27
|
+
},
|
|
28
|
+
"enabled": {
|
|
29
|
+
"message": "The enabled field must be defined",
|
|
30
|
+
"ref": undefined,
|
|
31
|
+
"type": "required",
|
|
32
|
+
},
|
|
33
|
+
"like": [
|
|
34
|
+
{
|
|
35
|
+
"id": {
|
|
36
|
+
"message": "The id field must be a number",
|
|
37
|
+
"ref": undefined,
|
|
38
|
+
"type": "number",
|
|
39
|
+
},
|
|
40
|
+
"name": {
|
|
41
|
+
"message": "The name field must be defined",
|
|
42
|
+
"ref": undefined,
|
|
43
|
+
"type": "required",
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
"password": {
|
|
48
|
+
"message": "The password field format is invalid",
|
|
49
|
+
"ref": {
|
|
50
|
+
"name": "password",
|
|
51
|
+
},
|
|
52
|
+
"type": "regex",
|
|
53
|
+
},
|
|
54
|
+
"repeatPassword": {
|
|
55
|
+
"message": "The repeatPassword field must be defined",
|
|
56
|
+
"ref": undefined,
|
|
57
|
+
"type": "required",
|
|
58
|
+
},
|
|
59
|
+
"tags": {
|
|
60
|
+
"message": "The tags field must be defined",
|
|
61
|
+
"ref": undefined,
|
|
62
|
+
"type": "required",
|
|
63
|
+
},
|
|
64
|
+
"username": {
|
|
65
|
+
"message": "The username field must be defined",
|
|
66
|
+
"ref": {
|
|
67
|
+
"name": "username",
|
|
68
|
+
},
|
|
69
|
+
"type": "required",
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
"values": {},
|
|
73
|
+
}
|
|
74
|
+
`;
|
|
75
|
+
|
|
76
|
+
exports[`vineResolver > should return all the errors from vineResolver when validation fails with \`validateAllFieldCriteria\` set to true 1`] = `
|
|
77
|
+
{
|
|
78
|
+
"errors": {
|
|
79
|
+
"accessToken": {
|
|
80
|
+
"message": "Invalid value provided for accessToken field",
|
|
81
|
+
"ref": undefined,
|
|
82
|
+
"type": "unionOfTypes",
|
|
83
|
+
"types": {
|
|
84
|
+
"unionOfTypes": "Invalid value provided for accessToken field",
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
"birthYear": {
|
|
88
|
+
"message": "The birthYear field must be a number",
|
|
89
|
+
"ref": undefined,
|
|
90
|
+
"type": "number",
|
|
91
|
+
"types": {
|
|
92
|
+
"number": "The birthYear field must be a number",
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
"dateStr": {
|
|
96
|
+
"message": "The dateStr field must be defined",
|
|
97
|
+
"ref": undefined,
|
|
98
|
+
"type": "required",
|
|
99
|
+
"types": {
|
|
100
|
+
"required": "The dateStr field must be defined",
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
"email": {
|
|
104
|
+
"message": "The email field must be a valid email address",
|
|
105
|
+
"ref": {
|
|
106
|
+
"name": "email",
|
|
107
|
+
},
|
|
108
|
+
"type": "email",
|
|
109
|
+
"types": {
|
|
110
|
+
"email": "The email field must be a valid email address",
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
"enabled": {
|
|
114
|
+
"message": "The enabled field must be defined",
|
|
115
|
+
"ref": undefined,
|
|
116
|
+
"type": "required",
|
|
117
|
+
"types": {
|
|
118
|
+
"required": "The enabled field must be defined",
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
"like": [
|
|
122
|
+
{
|
|
123
|
+
"id": {
|
|
124
|
+
"message": "The id field must be a number",
|
|
125
|
+
"ref": undefined,
|
|
126
|
+
"type": "number",
|
|
127
|
+
"types": {
|
|
128
|
+
"number": "The id field must be a number",
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
"name": {
|
|
132
|
+
"message": "The name field must be defined",
|
|
133
|
+
"ref": undefined,
|
|
134
|
+
"type": "required",
|
|
135
|
+
"types": {
|
|
136
|
+
"required": "The name field must be defined",
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
"password": {
|
|
142
|
+
"message": "The password field format is invalid",
|
|
143
|
+
"ref": {
|
|
144
|
+
"name": "password",
|
|
145
|
+
},
|
|
146
|
+
"type": "regex",
|
|
147
|
+
"types": {
|
|
148
|
+
"regex": "The password field format is invalid",
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
"repeatPassword": {
|
|
152
|
+
"message": "The repeatPassword field must be defined",
|
|
153
|
+
"ref": undefined,
|
|
154
|
+
"type": "required",
|
|
155
|
+
"types": {
|
|
156
|
+
"required": "The repeatPassword field must be defined",
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
"tags": {
|
|
160
|
+
"message": "The tags field must be defined",
|
|
161
|
+
"ref": undefined,
|
|
162
|
+
"type": "required",
|
|
163
|
+
"types": {
|
|
164
|
+
"required": "The tags field must be defined",
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
"username": {
|
|
168
|
+
"message": "The username field must be defined",
|
|
169
|
+
"ref": {
|
|
170
|
+
"name": "username",
|
|
171
|
+
},
|
|
172
|
+
"type": "required",
|
|
173
|
+
"types": {
|
|
174
|
+
"required": "The username field must be defined",
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
"values": {},
|
|
179
|
+
}
|
|
180
|
+
`;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { vineResolver } from '..';
|
|
2
|
+
import { schema, validData, fields, invalidData } from './__fixtures__/data';
|
|
3
|
+
|
|
4
|
+
const shouldUseNativeValidation = false;
|
|
5
|
+
|
|
6
|
+
describe('vineResolver', () => {
|
|
7
|
+
it('should return values from vineResolver when validation pass', async () => {
|
|
8
|
+
const schemaSpy = vi.spyOn(schema, 'validate');
|
|
9
|
+
|
|
10
|
+
const result = await vineResolver(schema)(validData, undefined, {
|
|
11
|
+
fields,
|
|
12
|
+
shouldUseNativeValidation,
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
expect(schemaSpy).toHaveBeenCalledTimes(1);
|
|
16
|
+
expect(result).toEqual({ errors: {}, values: validData });
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('should return a single error from vineResolver when validation fails', async () => {
|
|
20
|
+
const result = await vineResolver(schema)(invalidData, undefined, {
|
|
21
|
+
fields,
|
|
22
|
+
shouldUseNativeValidation,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
expect(result).toMatchSnapshot();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('should return all the errors from vineResolver when validation fails with `validateAllFieldCriteria` set to true', async () => {
|
|
29
|
+
const result = await vineResolver(schema)(invalidData, undefined, {
|
|
30
|
+
fields,
|
|
31
|
+
criteriaMode: 'all',
|
|
32
|
+
shouldUseNativeValidation,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
expect(result).toMatchSnapshot();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('should return values from vineResolver when validation pass & raw=true', async () => {
|
|
39
|
+
const schemaSpy = vi.spyOn(schema, 'validate');
|
|
40
|
+
|
|
41
|
+
const result = await vineResolver(schema, undefined, { raw: true })(
|
|
42
|
+
validData,
|
|
43
|
+
undefined,
|
|
44
|
+
{
|
|
45
|
+
fields,
|
|
46
|
+
shouldUseNativeValidation,
|
|
47
|
+
},
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
expect(schemaSpy).toHaveBeenCalledTimes(1);
|
|
51
|
+
expect(result).toEqual({ errors: {}, values: validData });
|
|
52
|
+
});
|
|
53
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { FieldValues, ResolverResult, ResolverOptions } from 'react-hook-form';
|
|
2
|
+
import { VineValidator } from '@vinejs/vine';
|
|
3
|
+
import { ValidationOptions } from '@vinejs/vine/build/src/types';
|
|
4
|
+
|
|
5
|
+
export type Resolver = <T extends VineValidator<any, any>>(
|
|
6
|
+
schema: T,
|
|
7
|
+
schemaOptions?: ValidationOptions<any>,
|
|
8
|
+
resolverOptions?: { raw?: boolean },
|
|
9
|
+
) => <TFieldValues extends FieldValues, TContext>(
|
|
10
|
+
values: TFieldValues,
|
|
11
|
+
context: TContext | undefined,
|
|
12
|
+
options: ResolverOptions<TFieldValues>,
|
|
13
|
+
) => Promise<ResolverResult<TFieldValues>>;
|
package/vine/src/vine.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { appendErrors, FieldError, FieldErrors } from 'react-hook-form';
|
|
2
|
+
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
|
|
3
|
+
import { SimpleErrorReporter, errors } from '@vinejs/vine';
|
|
4
|
+
import type { Resolver } from './types';
|
|
5
|
+
|
|
6
|
+
const parseErrorSchema = (
|
|
7
|
+
vineErrors: SimpleErrorReporter['errors'],
|
|
8
|
+
validateAllFieldCriteria: boolean,
|
|
9
|
+
) => {
|
|
10
|
+
const schemaErrors: Record<string, FieldError> = {};
|
|
11
|
+
|
|
12
|
+
for (const error of vineErrors) {
|
|
13
|
+
const { field, rule, message } = error;
|
|
14
|
+
const path = field;
|
|
15
|
+
|
|
16
|
+
if (!(path in schemaErrors)) {
|
|
17
|
+
schemaErrors[path] = { message, type: rule };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (validateAllFieldCriteria) {
|
|
21
|
+
const { types } = schemaErrors[path];
|
|
22
|
+
const messages = types && types[rule];
|
|
23
|
+
|
|
24
|
+
schemaErrors[path] = appendErrors(
|
|
25
|
+
path,
|
|
26
|
+
validateAllFieldCriteria,
|
|
27
|
+
schemaErrors,
|
|
28
|
+
rule,
|
|
29
|
+
messages ? [...(messages as string[]), message] : message,
|
|
30
|
+
) as FieldError;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return schemaErrors;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export const vineResolver: Resolver =
|
|
38
|
+
(schema, schemaOptions, resolverOptions = {}) =>
|
|
39
|
+
async (values, _, options) => {
|
|
40
|
+
try {
|
|
41
|
+
const data = await schema.validate(values, schemaOptions);
|
|
42
|
+
|
|
43
|
+
options.shouldUseNativeValidation && validateFieldsNatively({}, options);
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
errors: {} as FieldErrors,
|
|
47
|
+
values: resolverOptions.raw ? values : data,
|
|
48
|
+
};
|
|
49
|
+
} catch (error: any) {
|
|
50
|
+
if (error instanceof errors.E_VALIDATION_ERROR) {
|
|
51
|
+
return {
|
|
52
|
+
values: {},
|
|
53
|
+
errors: toNestErrors(
|
|
54
|
+
parseErrorSchema(
|
|
55
|
+
error.messages,
|
|
56
|
+
!options.shouldUseNativeValidation &&
|
|
57
|
+
options.criteriaMode === 'all',
|
|
58
|
+
),
|
|
59
|
+
options,
|
|
60
|
+
),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
};
|
package/zod/dist/zod.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var r=require("react-hook-form"),e=require("@hookform/resolvers"),o=function(e,o){for(var n={};e.length;){var s=e[0],t=s.code,i=s.message,a=s.path.join(".");if(!n[a])if("unionErrors"in s){var u=s.unionErrors[0].errors[0];n[a]={message:u.message,type:u.code}}else n[a]={message:i,type:t};if("unionErrors"in s&&s.unionErrors.forEach(function(r){return r.errors.forEach(function(r){return e.push(r)})}),o){var c=n[a].types,f=c&&c[s.code];n[a]=r.appendErrors(a,o,n,t,f?[].concat(f,s.message):s.message)}e.shift()}return n};exports.zodResolver=function(r,n,s){return void 0===s&&(s={}),function(t,i,a){try{return Promise.resolve(function(o,i){try{var u=Promise.resolve(r["sync"===s.mode?"parse":"parseAsync"](t,n)).then(function(r){return a.shouldUseNativeValidation&&e.validateFieldsNatively({},a),{errors:{},values:s.raw?t:r}})}catch(r){return i(r)}return u&&u.then?u.then(void 0,i):u}(0,function(r){if(function(r){return null
|
|
1
|
+
var r=require("react-hook-form"),e=require("@hookform/resolvers"),o=function(e,o){for(var n={};e.length;){var s=e[0],t=s.code,i=s.message,a=s.path.join(".");if(!n[a])if("unionErrors"in s){var u=s.unionErrors[0].errors[0];n[a]={message:u.message,type:u.code}}else n[a]={message:i,type:t};if("unionErrors"in s&&s.unionErrors.forEach(function(r){return r.errors.forEach(function(r){return e.push(r)})}),o){var c=n[a].types,f=c&&c[s.code];n[a]=r.appendErrors(a,o,n,t,f?[].concat(f,s.message):s.message)}e.shift()}return n};exports.zodResolver=function(r,n,s){return void 0===s&&(s={}),function(t,i,a){try{return Promise.resolve(function(o,i){try{var u=Promise.resolve(r["sync"===s.mode?"parse":"parseAsync"](t,n)).then(function(r){return a.shouldUseNativeValidation&&e.validateFieldsNatively({},a),{errors:{},values:s.raw?t:r}})}catch(r){return i(r)}return u&&u.then?u.then(void 0,i):u}(0,function(r){if(function(r){return Array.isArray(null==r?void 0:r.errors)}(r))return{values:{},errors:e.toNestErrors(o(r.errors,!a.shouldUseNativeValidation&&"all"===a.criteriaMode),a)};throw r}))}catch(r){return Promise.reject(r)}}};
|
|
2
2
|
//# sourceMappingURL=zod.js.map
|
package/zod/dist/zod.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zod.js","sources":["../src/zod.ts"],"sourcesContent":["import { appendErrors, FieldError, FieldErrors } from 'react-hook-form';\nimport { z, ZodError } from 'zod';\nimport { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { Resolver } from './types';\n\nconst isZodError = (error: any): error is ZodError => error
|
|
1
|
+
{"version":3,"file":"zod.js","sources":["../src/zod.ts"],"sourcesContent":["import { appendErrors, FieldError, FieldErrors } from 'react-hook-form';\nimport { z, ZodError } from 'zod';\nimport { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { Resolver } from './types';\n\nconst isZodError = (error: any): error is ZodError => Array.isArray(error?.errors);\n\nconst parseErrorSchema = (\n zodErrors: z.ZodIssue[],\n validateAllFieldCriteria: boolean,\n) => {\n const errors: Record<string, FieldError> = {};\n for (; zodErrors.length; ) {\n const error = zodErrors[0];\n const { code, message, path } = error;\n const _path = path.join('.');\n\n if (!errors[_path]) {\n if ('unionErrors' in error) {\n const unionError = error.unionErrors[0].errors[0];\n\n errors[_path] = {\n message: unionError.message,\n type: unionError.code,\n };\n } else {\n errors[_path] = { message, type: code };\n }\n }\n\n if ('unionErrors' in error) {\n error.unionErrors.forEach((unionError) =>\n unionError.errors.forEach((e) => zodErrors.push(e)),\n );\n }\n\n if (validateAllFieldCriteria) {\n const types = errors[_path].types;\n const messages = types && types[error.code];\n\n errors[_path] = appendErrors(\n _path,\n validateAllFieldCriteria,\n errors,\n code,\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n zodErrors.shift();\n }\n\n return errors;\n};\n\nexport const zodResolver: Resolver =\n (schema, schemaOptions, resolverOptions = {}) =>\n async (values, _, options) => {\n try {\n const data = await schema[\n resolverOptions.mode === 'sync' ? 'parse' : 'parseAsync'\n ](values, schemaOptions);\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n errors: {} as FieldErrors,\n values: resolverOptions.raw ? values : data,\n };\n } catch (error: any) {\n if (isZodError(error)) {\n return {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n error.errors,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n }\n\n throw error;\n }\n };\n"],"names":["parseErrorSchema","zodErrors","validateAllFieldCriteria","errors","length","error","code","message","_path","path","join","unionError","unionErrors","type","forEach","e","push","types","messages","appendErrors","concat","shift","schema","schemaOptions","resolverOptions","values","_","options","Promise","resolve","mode","then","data","shouldUseNativeValidation","validateFieldsNatively","raw","_catch","Array","isArray","isZodError","toNestErrors","criteriaMode","reject"],"mappings":"kEAOMA,EAAmB,SACvBC,EACAC,GAGA,IADA,IAAMC,EAAqC,CAAE,EACtCF,EAAUG,QAAU,CACzB,IAAMC,EAAQJ,EAAU,GAChBK,EAAwBD,EAAxBC,KAAMC,EAAkBF,EAAlBE,QACRC,EAD0BH,EAATI,KACJC,KAAK,KAExB,IAAKP,EAAOK,GACV,GAAI,gBAAiBH,EAAO,CAC1B,IAAMM,EAAaN,EAAMO,YAAY,GAAGT,OAAO,GAE/CA,EAAOK,GAAS,CACdD,QAASI,EAAWJ,QACpBM,KAAMF,EAAWL,KAEpB,MACCH,EAAOK,GAAS,CAAED,QAAAA,EAASM,KAAMP,GAUrC,GANI,gBAAiBD,GACnBA,EAAMO,YAAYE,QAAQ,SAACH,GAAU,OACnCA,EAAWR,OAAOW,QAAQ,SAACC,GAAM,OAAAd,EAAUe,KAAKD,EAAE,EAAC,GAInDb,EAA0B,CAC5B,IAAMe,EAAQd,EAAOK,GAAOS,MACtBC,EAAWD,GAASA,EAAMZ,EAAMC,MAEtCH,EAAOK,GAASW,EAAAA,aACdX,EACAN,EACAC,EACAG,EACAY,EACK,GAAgBE,OAAOF,EAAsBb,EAAME,SACpDF,EAAME,QAEb,CAEDN,EAAUoB,OACX,CAED,OAAOlB,CACT,sBAGE,SAACmB,EAAQC,EAAeC,GACjBC,YADgC,IAAfD,IAAAA,EAAkB,CAAE,GACrCC,SAAAA,EAAQC,EAAGC,GAAW,IAAA,OAAAC,QAAAC,gCACvBD,QAAAC,QACiBP,EACQ,SAAzBE,EAAgBM,KAAkB,QAAU,cAC5CL,EAAQF,IAAcQ,KAFlBC,SAAAA,GAMN,OAFAL,EAAQM,2BAA6BC,yBAAuB,CAAA,EAAIP,GAEzD,CACLxB,OAAQ,CAAiB,EACzBsB,OAAQD,EAAgBW,IAAMV,EAASO,EACvC,4DAXuBI,CACvB,EAWH,SAAQ/B,GACP,GAnEa,SAACA,GAAkC,OAAAgC,MAAMC,QAAa,MAALjC,OAAK,EAALA,EAAOF,OAAO,CAmExEoC,CAAWlC,GACb,MAAO,CACLoB,OAAQ,CAAA,EACRtB,OAAQqC,EAAYA,aAClBxC,EACEK,EAAMF,QACLwB,EAAQM,2BACkB,QAAzBN,EAAQc,cAEZd,IAKN,MAAMtB,CACP,GACH,CAAC,MAAAU,GAAA,OAAAa,QAAAc,OAAA3B,EAAA,CAAA,CAAA"}
|
package/zod/dist/zod.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{appendErrors as r}from"react-hook-form";import{validateFieldsNatively as e,toNestErrors as o}from"@hookform/resolvers";var n=function(e,o){for(var n={};e.length;){var t=e[0],s=t.code,i=t.message,a=t.path.join(".");if(!n[a])if("unionErrors"in t){var u=t.unionErrors[0].errors[0];n[a]={message:u.message,type:u.code}}else n[a]={message:i,type:s};if("unionErrors"in t&&t.unionErrors.forEach(function(r){return r.errors.forEach(function(r){return e.push(r)})}),o){var c=n[a].types,f=c&&c[t.code];n[a]=r(a,o,n,s,f?[].concat(f,t.message):t.message)}e.shift()}return n},t=function(r,t,s){return void 0===s&&(s={}),function(i,a,u){try{return Promise.resolve(function(o,n){try{var a=Promise.resolve(r["sync"===s.mode?"parse":"parseAsync"](i,t)).then(function(r){return u.shouldUseNativeValidation&&e({},u),{errors:{},values:s.raw?i:r}})}catch(r){return n(r)}return a&&a.then?a.then(void 0,n):a}(0,function(r){if(function(r){return null
|
|
1
|
+
import{appendErrors as r}from"react-hook-form";import{validateFieldsNatively as e,toNestErrors as o}from"@hookform/resolvers";var n=function(e,o){for(var n={};e.length;){var t=e[0],s=t.code,i=t.message,a=t.path.join(".");if(!n[a])if("unionErrors"in t){var u=t.unionErrors[0].errors[0];n[a]={message:u.message,type:u.code}}else n[a]={message:i,type:s};if("unionErrors"in t&&t.unionErrors.forEach(function(r){return r.errors.forEach(function(r){return e.push(r)})}),o){var c=n[a].types,f=c&&c[t.code];n[a]=r(a,o,n,s,f?[].concat(f,t.message):t.message)}e.shift()}return n},t=function(r,t,s){return void 0===s&&(s={}),function(i,a,u){try{return Promise.resolve(function(o,n){try{var a=Promise.resolve(r["sync"===s.mode?"parse":"parseAsync"](i,t)).then(function(r){return u.shouldUseNativeValidation&&e({},u),{errors:{},values:s.raw?i:r}})}catch(r){return n(r)}return a&&a.then?a.then(void 0,n):a}(0,function(r){if(function(r){return Array.isArray(null==r?void 0:r.errors)}(r))return{values:{},errors:o(n(r.errors,!u.shouldUseNativeValidation&&"all"===u.criteriaMode),u)};throw r}))}catch(r){return Promise.reject(r)}}};export{t as zodResolver};
|
|
2
2
|
//# sourceMappingURL=zod.module.js.map
|
package/zod/dist/zod.modern.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{appendErrors as r}from"react-hook-form";import{validateFieldsNatively as o,toNestErrors as
|
|
1
|
+
import{appendErrors as r}from"react-hook-form";import{validateFieldsNatively as o,toNestErrors as s}from"@hookform/resolvers";const e=(o,s)=>{const e={};for(;o.length;){const a=o[0],{code:t,message:n,path:i}=a,c=i.join(".");if(!e[c])if("unionErrors"in a){const r=a.unionErrors[0].errors[0];e[c]={message:r.message,type:r.code}}else e[c]={message:n,type:t};if("unionErrors"in a&&a.unionErrors.forEach(r=>r.errors.forEach(r=>o.push(r))),s){const o=e[c].types,n=o&&o[a.code];e[c]=r(c,s,e,t,n?[].concat(n,a.message):a.message)}o.shift()}return e},a=(r,a,t={})=>async(n,i,c)=>{try{const s=await r["sync"===t.mode?"parse":"parseAsync"](n,a);return c.shouldUseNativeValidation&&o({},c),{errors:{},values:t.raw?n:s}}catch(r){if((r=>Array.isArray(null==r?void 0:r.errors))(r))return{values:{},errors:s(e(r.errors,!c.shouldUseNativeValidation&&"all"===c.criteriaMode),c)};throw r}};export{a as zodResolver};
|
|
2
2
|
//# sourceMappingURL=zod.modern.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zod.modern.mjs","sources":["../src/zod.ts"],"sourcesContent":["import { appendErrors, FieldError, FieldErrors } from 'react-hook-form';\nimport { z, ZodError } from 'zod';\nimport { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { Resolver } from './types';\n\nconst isZodError = (error: any): error is ZodError => error
|
|
1
|
+
{"version":3,"file":"zod.modern.mjs","sources":["../src/zod.ts"],"sourcesContent":["import { appendErrors, FieldError, FieldErrors } from 'react-hook-form';\nimport { z, ZodError } from 'zod';\nimport { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { Resolver } from './types';\n\nconst isZodError = (error: any): error is ZodError => Array.isArray(error?.errors);\n\nconst parseErrorSchema = (\n zodErrors: z.ZodIssue[],\n validateAllFieldCriteria: boolean,\n) => {\n const errors: Record<string, FieldError> = {};\n for (; zodErrors.length; ) {\n const error = zodErrors[0];\n const { code, message, path } = error;\n const _path = path.join('.');\n\n if (!errors[_path]) {\n if ('unionErrors' in error) {\n const unionError = error.unionErrors[0].errors[0];\n\n errors[_path] = {\n message: unionError.message,\n type: unionError.code,\n };\n } else {\n errors[_path] = { message, type: code };\n }\n }\n\n if ('unionErrors' in error) {\n error.unionErrors.forEach((unionError) =>\n unionError.errors.forEach((e) => zodErrors.push(e)),\n );\n }\n\n if (validateAllFieldCriteria) {\n const types = errors[_path].types;\n const messages = types && types[error.code];\n\n errors[_path] = appendErrors(\n _path,\n validateAllFieldCriteria,\n errors,\n code,\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n zodErrors.shift();\n }\n\n return errors;\n};\n\nexport const zodResolver: Resolver =\n (schema, schemaOptions, resolverOptions = {}) =>\n async (values, _, options) => {\n try {\n const data = await schema[\n resolverOptions.mode === 'sync' ? 'parse' : 'parseAsync'\n ](values, schemaOptions);\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n errors: {} as FieldErrors,\n values: resolverOptions.raw ? values : data,\n };\n } catch (error: any) {\n if (isZodError(error)) {\n return {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n error.errors,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n }\n\n throw error;\n }\n };\n"],"names":["parseErrorSchema","zodErrors","validateAllFieldCriteria","errors","length","error","code","message","path","_path","join","unionError","unionErrors","type","forEach","e","push","types","messages","appendErrors","concat","shift","zodResolver","schema","schemaOptions","resolverOptions","async","values","_","options","data","mode","shouldUseNativeValidation","validateFieldsNatively","raw","Array","isArray","isZodError","toNestErrors","criteriaMode"],"mappings":"8HAKA,MAEMA,EAAmBA,CACvBC,EACAC,KAEA,MAAMC,EAAqC,CAAA,EAC3C,KAAOF,EAAUG,QAAU,CACzB,MAAMC,EAAQJ,EAAU,IAClBK,KAAEA,EAAIC,QAAEA,EAAOC,KAAEA,GAASH,EAC1BI,EAAQD,EAAKE,KAAK,KAExB,IAAKP,EAAOM,GACV,GAAI,gBAAiBJ,EAAO,CAC1B,MAAMM,EAAaN,EAAMO,YAAY,GAAGT,OAAO,GAE/CA,EAAOM,GAAS,CACdF,QAASI,EAAWJ,QACpBM,KAAMF,EAAWL,KAEpB,MACCH,EAAOM,GAAS,CAAEF,UAASM,KAAMP,GAUrC,GANI,gBAAiBD,GACnBA,EAAMO,YAAYE,QAASH,GACzBA,EAAWR,OAAOW,QAASC,GAAMd,EAAUe,KAAKD,KAIhDb,EAA0B,CAC5B,MAAMe,EAAQd,EAAOM,GAAOQ,MACtBC,EAAWD,GAASA,EAAMZ,EAAMC,MAEtCH,EAAOM,GAASU,EACdV,EACAP,EACAC,EACAG,EACAY,EACK,GAAgBE,OAAOF,EAAsBb,EAAME,SACpDF,EAAME,QAEb,CAEDN,EAAUoB,OACX,CAED,OAAOlB,GAGImB,EACXA,CAACC,EAAQC,EAAeC,EAAkB,KAC1CC,MAAOC,EAAQC,EAAGC,KAChB,IACE,MAAMC,QAAaP,EACQ,SAAzBE,EAAgBM,KAAkB,QAAU,cAC5CJ,EAAQH,GAIV,OAFAK,EAAQG,2BAA6BC,EAAuB,CAAE,EAAEJ,GAEzD,CACL1B,OAAQ,CAAA,EACRwB,OAAQF,EAAgBS,IAAMP,EAASG,EAE1C,CAAC,MAAOzB,GACP,GAnEcA,IAAkC8B,MAAMC,QAAQ/B,MAAAA,OAAAA,EAAAA,EAAOF,QAmEjEkC,CAAWhC,GACb,MAAO,CACLsB,OAAQ,GACRxB,OAAQmC,EACNtC,EACEK,EAAMF,QACL0B,EAAQG,2BACkB,QAAzBH,EAAQU,cAEZV,IAKN,MAAMxB,CACP"}
|
package/zod/dist/zod.module.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{appendErrors as r}from"react-hook-form";import{validateFieldsNatively as e,toNestErrors as o}from"@hookform/resolvers";var n=function(e,o){for(var n={};e.length;){var t=e[0],s=t.code,i=t.message,a=t.path.join(".");if(!n[a])if("unionErrors"in t){var u=t.unionErrors[0].errors[0];n[a]={message:u.message,type:u.code}}else n[a]={message:i,type:s};if("unionErrors"in t&&t.unionErrors.forEach(function(r){return r.errors.forEach(function(r){return e.push(r)})}),o){var c=n[a].types,f=c&&c[t.code];n[a]=r(a,o,n,s,f?[].concat(f,t.message):t.message)}e.shift()}return n},t=function(r,t,s){return void 0===s&&(s={}),function(i,a,u){try{return Promise.resolve(function(o,n){try{var a=Promise.resolve(r["sync"===s.mode?"parse":"parseAsync"](i,t)).then(function(r){return u.shouldUseNativeValidation&&e({},u),{errors:{},values:s.raw?i:r}})}catch(r){return n(r)}return a&&a.then?a.then(void 0,n):a}(0,function(r){if(function(r){return null
|
|
1
|
+
import{appendErrors as r}from"react-hook-form";import{validateFieldsNatively as e,toNestErrors as o}from"@hookform/resolvers";var n=function(e,o){for(var n={};e.length;){var t=e[0],s=t.code,i=t.message,a=t.path.join(".");if(!n[a])if("unionErrors"in t){var u=t.unionErrors[0].errors[0];n[a]={message:u.message,type:u.code}}else n[a]={message:i,type:s};if("unionErrors"in t&&t.unionErrors.forEach(function(r){return r.errors.forEach(function(r){return e.push(r)})}),o){var c=n[a].types,f=c&&c[t.code];n[a]=r(a,o,n,s,f?[].concat(f,t.message):t.message)}e.shift()}return n},t=function(r,t,s){return void 0===s&&(s={}),function(i,a,u){try{return Promise.resolve(function(o,n){try{var a=Promise.resolve(r["sync"===s.mode?"parse":"parseAsync"](i,t)).then(function(r){return u.shouldUseNativeValidation&&e({},u),{errors:{},values:s.raw?i:r}})}catch(r){return n(r)}return a&&a.then?a.then(void 0,n):a}(0,function(r){if(function(r){return Array.isArray(null==r?void 0:r.errors)}(r))return{values:{},errors:o(n(r.errors,!u.shouldUseNativeValidation&&"all"===u.criteriaMode),u)};throw r}))}catch(r){return Promise.reject(r)}}};export{t as zodResolver};
|
|
2
2
|
//# sourceMappingURL=zod.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zod.module.js","sources":["../src/zod.ts"],"sourcesContent":["import { appendErrors, FieldError, FieldErrors } from 'react-hook-form';\nimport { z, ZodError } from 'zod';\nimport { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { Resolver } from './types';\n\nconst isZodError = (error: any): error is ZodError => error
|
|
1
|
+
{"version":3,"file":"zod.module.js","sources":["../src/zod.ts"],"sourcesContent":["import { appendErrors, FieldError, FieldErrors } from 'react-hook-form';\nimport { z, ZodError } from 'zod';\nimport { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { Resolver } from './types';\n\nconst isZodError = (error: any): error is ZodError => Array.isArray(error?.errors);\n\nconst parseErrorSchema = (\n zodErrors: z.ZodIssue[],\n validateAllFieldCriteria: boolean,\n) => {\n const errors: Record<string, FieldError> = {};\n for (; zodErrors.length; ) {\n const error = zodErrors[0];\n const { code, message, path } = error;\n const _path = path.join('.');\n\n if (!errors[_path]) {\n if ('unionErrors' in error) {\n const unionError = error.unionErrors[0].errors[0];\n\n errors[_path] = {\n message: unionError.message,\n type: unionError.code,\n };\n } else {\n errors[_path] = { message, type: code };\n }\n }\n\n if ('unionErrors' in error) {\n error.unionErrors.forEach((unionError) =>\n unionError.errors.forEach((e) => zodErrors.push(e)),\n );\n }\n\n if (validateAllFieldCriteria) {\n const types = errors[_path].types;\n const messages = types && types[error.code];\n\n errors[_path] = appendErrors(\n _path,\n validateAllFieldCriteria,\n errors,\n code,\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n zodErrors.shift();\n }\n\n return errors;\n};\n\nexport const zodResolver: Resolver =\n (schema, schemaOptions, resolverOptions = {}) =>\n async (values, _, options) => {\n try {\n const data = await schema[\n resolverOptions.mode === 'sync' ? 'parse' : 'parseAsync'\n ](values, schemaOptions);\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n errors: {} as FieldErrors,\n values: resolverOptions.raw ? values : data,\n };\n } catch (error: any) {\n if (isZodError(error)) {\n return {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n error.errors,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n }\n\n throw error;\n }\n };\n"],"names":["parseErrorSchema","zodErrors","validateAllFieldCriteria","errors","length","error","code","message","_path","path","join","unionError","unionErrors","type","forEach","e","push","types","messages","appendErrors","concat","shift","zodResolver","schema","schemaOptions","resolverOptions","values","_","options","Promise","resolve","mode","then","data","shouldUseNativeValidation","validateFieldsNatively","raw","_catch","Array","isArray","isZodError","toNestErrors","criteriaMode","reject"],"mappings":"8HAKA,IAEMA,EAAmB,SACvBC,EACAC,GAGA,IADA,IAAMC,EAAqC,CAAE,EACtCF,EAAUG,QAAU,CACzB,IAAMC,EAAQJ,EAAU,GAChBK,EAAwBD,EAAxBC,KAAMC,EAAkBF,EAAlBE,QACRC,EAD0BH,EAATI,KACJC,KAAK,KAExB,IAAKP,EAAOK,GACV,GAAI,gBAAiBH,EAAO,CAC1B,IAAMM,EAAaN,EAAMO,YAAY,GAAGT,OAAO,GAE/CA,EAAOK,GAAS,CACdD,QAASI,EAAWJ,QACpBM,KAAMF,EAAWL,KAEpB,MACCH,EAAOK,GAAS,CAAED,QAAAA,EAASM,KAAMP,GAUrC,GANI,gBAAiBD,GACnBA,EAAMO,YAAYE,QAAQ,SAACH,GAAU,OACnCA,EAAWR,OAAOW,QAAQ,SAACC,GAAM,OAAAd,EAAUe,KAAKD,EAAE,EAAC,GAInDb,EAA0B,CAC5B,IAAMe,EAAQd,EAAOK,GAAOS,MACtBC,EAAWD,GAASA,EAAMZ,EAAMC,MAEtCH,EAAOK,GAASW,EACdX,EACAN,EACAC,EACAG,EACAY,EACK,GAAgBE,OAAOF,EAAsBb,EAAME,SACpDF,EAAME,QAEb,CAEDN,EAAUoB,OACX,CAED,OAAOlB,CACT,EAEamB,EACX,SAACC,EAAQC,EAAeC,GACjBC,YADgC,IAAfD,IAAAA,EAAkB,CAAE,GACrCC,SAAAA,EAAQC,EAAGC,GAAW,IAAA,OAAAC,QAAAC,gCACvBD,QAAAC,QACiBP,EACQ,SAAzBE,EAAgBM,KAAkB,QAAU,cAC5CL,EAAQF,IAAcQ,KAFlBC,SAAAA,GAMN,OAFAL,EAAQM,2BAA6BC,EAAuB,CAAA,EAAIP,GAEzD,CACLzB,OAAQ,CAAiB,EACzBuB,OAAQD,EAAgBW,IAAMV,EAASO,EACvC,4DAXuBI,CACvB,EAWH,SAAQhC,GACP,GAnEa,SAACA,GAAkC,OAAAiC,MAAMC,QAAa,MAALlC,OAAK,EAALA,EAAOF,OAAO,CAmExEqC,CAAWnC,GACb,MAAO,CACLqB,OAAQ,CAAA,EACRvB,OAAQsC,EACNzC,EACEK,EAAMF,QACLyB,EAAQM,2BACkB,QAAzBN,EAAQc,cAEZd,IAKN,MAAMvB,CACP,GACH,CAAC,MAAAU,GAAA,OAAAc,QAAAc,OAAA5B,EAAA,CAAA,CAAA"}
|
package/zod/dist/zod.umd.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react-hook-form"),require("@hookform/resolvers")):"function"==typeof define&&define.amd?define(["exports","react-hook-form","@hookform/resolvers"],e):e((r||self).hookformResolversZod={},r.ReactHookForm,r.hookformResolvers)}(this,function(r,e,o){var n=function(r,o){for(var n={};r.length;){var s=r[0],t=s.code,i=s.message,a=s.path.join(".");if(!n[a])if("unionErrors"in s){var u=s.unionErrors[0].errors[0];n[a]={message:u.message,type:u.code}}else n[a]={message:i,type:t};if("unionErrors"in s&&s.unionErrors.forEach(function(e){return e.errors.forEach(function(e){return r.push(e)})}),o){var f=n[a].types,c=f&&f[s.code];n[a]=e.appendErrors(a,o,n,t,c?[].concat(c,s.message):s.message)}r.shift()}return n};r.zodResolver=function(r,e,s){return void 0===s&&(s={}),function(t,i,a){try{return Promise.resolve(function(n,i){try{var u=Promise.resolve(r["sync"===s.mode?"parse":"parseAsync"](t,e)).then(function(r){return a.shouldUseNativeValidation&&o.validateFieldsNatively({},a),{errors:{},values:s.raw?t:r}})}catch(r){return i(r)}return u&&u.then?u.then(void 0,i):u}(0,function(r){if(function(r){return null
|
|
1
|
+
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react-hook-form"),require("@hookform/resolvers")):"function"==typeof define&&define.amd?define(["exports","react-hook-form","@hookform/resolvers"],e):e((r||self).hookformResolversZod={},r.ReactHookForm,r.hookformResolvers)}(this,function(r,e,o){var n=function(r,o){for(var n={};r.length;){var s=r[0],t=s.code,i=s.message,a=s.path.join(".");if(!n[a])if("unionErrors"in s){var u=s.unionErrors[0].errors[0];n[a]={message:u.message,type:u.code}}else n[a]={message:i,type:t};if("unionErrors"in s&&s.unionErrors.forEach(function(e){return e.errors.forEach(function(e){return r.push(e)})}),o){var f=n[a].types,c=f&&f[s.code];n[a]=e.appendErrors(a,o,n,t,c?[].concat(c,s.message):s.message)}r.shift()}return n};r.zodResolver=function(r,e,s){return void 0===s&&(s={}),function(t,i,a){try{return Promise.resolve(function(n,i){try{var u=Promise.resolve(r["sync"===s.mode?"parse":"parseAsync"](t,e)).then(function(r){return a.shouldUseNativeValidation&&o.validateFieldsNatively({},a),{errors:{},values:s.raw?t:r}})}catch(r){return i(r)}return u&&u.then?u.then(void 0,i):u}(0,function(r){if(function(r){return Array.isArray(null==r?void 0:r.errors)}(r))return{values:{},errors:o.toNestErrors(n(r.errors,!a.shouldUseNativeValidation&&"all"===a.criteriaMode),a)};throw r}))}catch(r){return Promise.reject(r)}}}});
|
|
2
2
|
//# sourceMappingURL=zod.umd.js.map
|