@hookform/resolvers 2.8.1

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.
Files changed (227) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +420 -0
  3. package/class-validator/dist/class-validator.d.ts +2 -0
  4. package/class-validator/dist/class-validator.js +2 -0
  5. package/class-validator/dist/class-validator.js.map +1 -0
  6. package/class-validator/dist/class-validator.mjs +2 -0
  7. package/class-validator/dist/class-validator.modern.js +2 -0
  8. package/class-validator/dist/class-validator.modern.js.map +1 -0
  9. package/class-validator/dist/class-validator.module.js +2 -0
  10. package/class-validator/dist/class-validator.module.js.map +1 -0
  11. package/class-validator/dist/class-validator.umd.js +2 -0
  12. package/class-validator/dist/class-validator.umd.js.map +1 -0
  13. package/class-validator/dist/index.d.ts +2 -0
  14. package/class-validator/dist/types.d.ts +8 -0
  15. package/class-validator/package.json +19 -0
  16. package/class-validator/src/__tests__/Form-native-validation.tsx +83 -0
  17. package/class-validator/src/__tests__/Form.tsx +55 -0
  18. package/class-validator/src/__tests__/__fixtures__/data.ts +88 -0
  19. package/class-validator/src/__tests__/__snapshots__/class-validator.ts.snap +207 -0
  20. package/class-validator/src/__tests__/class-validator.ts +87 -0
  21. package/class-validator/src/class-validator.ts +62 -0
  22. package/class-validator/src/index.ts +2 -0
  23. package/class-validator/src/types.ts +22 -0
  24. package/computed-types/dist/computed-types.d.ts +2 -0
  25. package/computed-types/dist/computed-types.js +2 -0
  26. package/computed-types/dist/computed-types.js.map +1 -0
  27. package/computed-types/dist/computed-types.mjs +2 -0
  28. package/computed-types/dist/computed-types.modern.js +2 -0
  29. package/computed-types/dist/computed-types.modern.js.map +1 -0
  30. package/computed-types/dist/computed-types.module.js +2 -0
  31. package/computed-types/dist/computed-types.module.js.map +1 -0
  32. package/computed-types/dist/computed-types.umd.js +2 -0
  33. package/computed-types/dist/computed-types.umd.js.map +1 -0
  34. package/computed-types/dist/index.d.ts +2 -0
  35. package/computed-types/dist/types.d.ts +2 -0
  36. package/computed-types/package.json +17 -0
  37. package/computed-types/src/__tests__/Form-native-validation.tsx +85 -0
  38. package/computed-types/src/__tests__/Form.tsx +54 -0
  39. package/computed-types/src/__tests__/__fixtures__/data.ts +72 -0
  40. package/computed-types/src/__tests__/__snapshots__/computed-types.ts.snap +55 -0
  41. package/computed-types/src/__tests__/computed-types.ts +24 -0
  42. package/computed-types/src/computed-types.ts +43 -0
  43. package/computed-types/src/index.ts +2 -0
  44. package/computed-types/src/types.ts +14 -0
  45. package/dist/index.d.ts +2 -0
  46. package/dist/resolvers.js +2 -0
  47. package/dist/resolvers.js.map +1 -0
  48. package/dist/resolvers.mjs +2 -0
  49. package/dist/resolvers.mjs.map +1 -0
  50. package/dist/resolvers.module.js +2 -0
  51. package/dist/resolvers.module.js.map +1 -0
  52. package/dist/resolvers.umd.js +2 -0
  53. package/dist/resolvers.umd.js.map +1 -0
  54. package/dist/toNestError.d.ts +2 -0
  55. package/dist/validateFieldsNatively.d.ts +2 -0
  56. package/io-ts/dist/arrayToPath.d.ts +3 -0
  57. package/io-ts/dist/errorsToRecord.d.ts +5 -0
  58. package/io-ts/dist/index.d.ts +2 -0
  59. package/io-ts/dist/io-ts.d.ts +2 -0
  60. package/io-ts/dist/io-ts.js +2 -0
  61. package/io-ts/dist/io-ts.js.map +1 -0
  62. package/io-ts/dist/io-ts.mjs +2 -0
  63. package/io-ts/dist/io-ts.modern.js +2 -0
  64. package/io-ts/dist/io-ts.modern.js.map +1 -0
  65. package/io-ts/dist/io-ts.module.js +2 -0
  66. package/io-ts/dist/io-ts.module.js.map +1 -0
  67. package/io-ts/dist/io-ts.umd.js +2 -0
  68. package/io-ts/dist/io-ts.umd.js.map +1 -0
  69. package/io-ts/dist/types.d.ts +7 -0
  70. package/io-ts/package.json +19 -0
  71. package/io-ts/src/__tests__/Form-native-validation.tsx +89 -0
  72. package/io-ts/src/__tests__/Form.tsx +65 -0
  73. package/io-ts/src/__tests__/__fixtures__/data.ts +129 -0
  74. package/io-ts/src/__tests__/__snapshots__/io-ts.ts.snap +89 -0
  75. package/io-ts/src/__tests__/io-ts.ts +37 -0
  76. package/io-ts/src/arrayToPath.ts +18 -0
  77. package/io-ts/src/errorsToRecord.ts +131 -0
  78. package/io-ts/src/index.ts +2 -0
  79. package/io-ts/src/io-ts.ts +33 -0
  80. package/io-ts/src/types.ts +23 -0
  81. package/joi/dist/index.d.ts +2 -0
  82. package/joi/dist/joi.d.ts +2 -0
  83. package/joi/dist/joi.js +2 -0
  84. package/joi/dist/joi.js.map +1 -0
  85. package/joi/dist/joi.mjs +2 -0
  86. package/joi/dist/joi.modern.js +2 -0
  87. package/joi/dist/joi.modern.js.map +1 -0
  88. package/joi/dist/joi.module.js +2 -0
  89. package/joi/dist/joi.module.js.map +1 -0
  90. package/joi/dist/joi.umd.js +2 -0
  91. package/joi/dist/joi.umd.js.map +1 -0
  92. package/joi/dist/types.d.ts +5 -0
  93. package/joi/package.json +17 -0
  94. package/joi/src/__tests__/Form-native-validation.tsx +89 -0
  95. package/joi/src/__tests__/Form.tsx +61 -0
  96. package/joi/src/__tests__/__fixtures__/data.ts +85 -0
  97. package/joi/src/__tests__/__snapshots__/joi.ts.snap +293 -0
  98. package/joi/src/__tests__/joi.ts +98 -0
  99. package/joi/src/index.ts +2 -0
  100. package/joi/src/joi.ts +81 -0
  101. package/joi/src/types.ts +16 -0
  102. package/nope/dist/index.d.ts +2 -0
  103. package/nope/dist/nope.d.ts +2 -0
  104. package/nope/dist/nope.js +2 -0
  105. package/nope/dist/nope.js.map +1 -0
  106. package/nope/dist/nope.mjs +2 -0
  107. package/nope/dist/nope.modern.js +2 -0
  108. package/nope/dist/nope.modern.js.map +1 -0
  109. package/nope/dist/nope.module.js +2 -0
  110. package/nope/dist/nope.module.js.map +1 -0
  111. package/nope/dist/nope.umd.js +2 -0
  112. package/nope/dist/nope.umd.js.map +1 -0
  113. package/nope/dist/types.d.ts +6 -0
  114. package/nope/package.json +18 -0
  115. package/nope/src/__tests__/Form-native-validation.tsx +89 -0
  116. package/nope/src/__tests__/Form.tsx +57 -0
  117. package/nope/src/__tests__/__fixtures__/data.ts +70 -0
  118. package/nope/src/__tests__/__snapshots__/nope.ts.snap +43 -0
  119. package/nope/src/__tests__/nope.ts +28 -0
  120. package/nope/src/index.ts +2 -0
  121. package/nope/src/nope.ts +46 -0
  122. package/nope/src/types.ts +22 -0
  123. package/package.json +216 -0
  124. package/superstruct/dist/index.d.ts +2 -0
  125. package/superstruct/dist/superstruct.d.ts +2 -0
  126. package/superstruct/dist/superstruct.js +2 -0
  127. package/superstruct/dist/superstruct.js.map +1 -0
  128. package/superstruct/dist/superstruct.mjs +2 -0
  129. package/superstruct/dist/superstruct.modern.js +2 -0
  130. package/superstruct/dist/superstruct.modern.js.map +1 -0
  131. package/superstruct/dist/superstruct.module.js +2 -0
  132. package/superstruct/dist/superstruct.module.js.map +1 -0
  133. package/superstruct/dist/superstruct.umd.js +2 -0
  134. package/superstruct/dist/superstruct.umd.js.map +1 -0
  135. package/superstruct/dist/types.d.ts +5 -0
  136. package/superstruct/package.json +18 -0
  137. package/superstruct/src/__tests__/Form-native-validation.tsx +86 -0
  138. package/superstruct/src/__tests__/Form.tsx +62 -0
  139. package/superstruct/src/__tests__/__fixtures__/data.ts +75 -0
  140. package/superstruct/src/__tests__/__snapshots__/superstruct.ts.snap +64 -0
  141. package/superstruct/src/__tests__/superstruct.ts +24 -0
  142. package/superstruct/src/index.ts +2 -0
  143. package/superstruct/src/superstruct.ts +37 -0
  144. package/superstruct/src/types.ts +17 -0
  145. package/typanion/dist/index.d.ts +2 -0
  146. package/typanion/dist/typanion.d.ts +2 -0
  147. package/typanion/dist/typanion.js +2 -0
  148. package/typanion/dist/typanion.js.map +1 -0
  149. package/typanion/dist/typanion.mjs +2 -0
  150. package/typanion/dist/typanion.modern.js +2 -0
  151. package/typanion/dist/typanion.modern.js.map +1 -0
  152. package/typanion/dist/typanion.module.js +2 -0
  153. package/typanion/dist/typanion.module.js.map +1 -0
  154. package/typanion/dist/typanion.umd.js +2 -0
  155. package/typanion/dist/typanion.umd.js.map +1 -0
  156. package/typanion/dist/types.d.ts +6 -0
  157. package/typanion/package.json +18 -0
  158. package/typanion/src/__tests__/Form-native-validation.tsx +89 -0
  159. package/typanion/src/__tests__/Form.tsx +61 -0
  160. package/typanion/src/__tests__/__fixtures__/data.ts +77 -0
  161. package/typanion/src/__tests__/__snapshots__/typanion.ts.snap +67 -0
  162. package/typanion/src/__tests__/typanion.ts +31 -0
  163. package/typanion/src/index.ts +2 -0
  164. package/typanion/src/typanion.ts +44 -0
  165. package/typanion/src/types.ts +28 -0
  166. package/vest/dist/index.d.ts +1 -0
  167. package/vest/dist/vest.d.ts +2 -0
  168. package/vest/dist/vest.js +2 -0
  169. package/vest/dist/vest.js.map +1 -0
  170. package/vest/dist/vest.mjs +2 -0
  171. package/vest/dist/vest.modern.js +2 -0
  172. package/vest/dist/vest.modern.js.map +1 -0
  173. package/vest/dist/vest.module.js +2 -0
  174. package/vest/dist/vest.module.js.map +1 -0
  175. package/vest/dist/vest.umd.js +2 -0
  176. package/vest/dist/vest.umd.js.map +1 -0
  177. package/vest/package.json +18 -0
  178. package/vest/src/__tests__/Form-native-validation.tsx +93 -0
  179. package/vest/src/__tests__/Form.tsx +64 -0
  180. package/vest/src/__tests__/__fixtures__/data.ts +67 -0
  181. package/vest/src/__tests__/__snapshots__/vest.ts.snap +141 -0
  182. package/vest/src/__tests__/vest.ts +71 -0
  183. package/vest/src/index.ts +1 -0
  184. package/vest/src/types.ts +20 -0
  185. package/vest/src/vest.ts +51 -0
  186. package/yup/dist/index.d.ts +2 -0
  187. package/yup/dist/types.d.ts +8 -0
  188. package/yup/dist/yup.d.ts +2 -0
  189. package/yup/dist/yup.js +2 -0
  190. package/yup/dist/yup.js.map +1 -0
  191. package/yup/dist/yup.mjs +2 -0
  192. package/yup/dist/yup.modern.js +2 -0
  193. package/yup/dist/yup.modern.js.map +1 -0
  194. package/yup/dist/yup.module.js +2 -0
  195. package/yup/dist/yup.module.js.map +1 -0
  196. package/yup/dist/yup.umd.js +2 -0
  197. package/yup/dist/yup.umd.js.map +1 -0
  198. package/yup/package.json +17 -0
  199. package/yup/src/__tests__/Form-native-validation.tsx +85 -0
  200. package/yup/src/__tests__/Form.tsx +54 -0
  201. package/yup/src/__tests__/__fixtures__/data.ts +72 -0
  202. package/yup/src/__tests__/__snapshots__/yup.ts.snap +231 -0
  203. package/yup/src/__tests__/yup.ts +174 -0
  204. package/yup/src/index.ts +2 -0
  205. package/yup/src/types.ts +21 -0
  206. package/yup/src/yup.ts +75 -0
  207. package/zod/dist/index.d.ts +2 -0
  208. package/zod/dist/types.d.ts +5 -0
  209. package/zod/dist/zod.d.ts +2 -0
  210. package/zod/dist/zod.js +2 -0
  211. package/zod/dist/zod.js.map +1 -0
  212. package/zod/dist/zod.mjs +2 -0
  213. package/zod/dist/zod.modern.js +2 -0
  214. package/zod/dist/zod.modern.js.map +1 -0
  215. package/zod/dist/zod.module.js +2 -0
  216. package/zod/dist/zod.module.js.map +1 -0
  217. package/zod/dist/zod.umd.js +2 -0
  218. package/zod/dist/zod.umd.js.map +1 -0
  219. package/zod/package.json +17 -0
  220. package/zod/src/__tests__/Form-native-validation.tsx +85 -0
  221. package/zod/src/__tests__/Form.tsx +54 -0
  222. package/zod/src/__tests__/__fixtures__/data.ts +81 -0
  223. package/zod/src/__tests__/__snapshots__/zod.ts.snap +377 -0
  224. package/zod/src/__tests__/zod.ts +77 -0
  225. package/zod/src/index.ts +2 -0
  226. package/zod/src/types.ts +16 -0
  227. package/zod/src/zod.ts +85 -0
@@ -0,0 +1,93 @@
1
+ import React from 'react';
2
+ import { render, screen, act } from '@testing-library/react';
3
+ import user from '@testing-library/user-event';
4
+ import { useForm } from 'react-hook-form';
5
+ import vest from 'vest';
6
+ import { vestResolver } from '..';
7
+
8
+ const USERNAME_REQUIRED_MESSAGE = 'username field is required';
9
+ const PASSWORD_SYMBOL_MESSAGE = 'password must contain a symbol';
10
+
11
+ interface FormData {
12
+ username: string;
13
+ password: string;
14
+ }
15
+
16
+ const validationSuite = vest.create('form', (data: FormData) => {
17
+ vest.test('username', USERNAME_REQUIRED_MESSAGE, () => {
18
+ vest.enforce(data.username).isNotEmpty();
19
+ });
20
+
21
+ vest.test('password', PASSWORD_SYMBOL_MESSAGE, () => {
22
+ vest.enforce(data.password).isNotEmpty();
23
+ });
24
+ });
25
+
26
+ interface Props {
27
+ onSubmit: (data: FormData) => void;
28
+ }
29
+
30
+ function TestComponent({ onSubmit }: Props) {
31
+ const { register, handleSubmit } = useForm<FormData>({
32
+ resolver: vestResolver(validationSuite),
33
+ shouldUseNativeValidation: true,
34
+ });
35
+
36
+ return (
37
+ <form onSubmit={handleSubmit(onSubmit)}>
38
+ <input {...register('username')} placeholder="username" />
39
+
40
+ <input {...register('password')} placeholder="password" />
41
+
42
+ <button type="submit">submit</button>
43
+ </form>
44
+ );
45
+ }
46
+
47
+ test("form's native validation with Vest", async () => {
48
+ const handleSubmit = jest.fn();
49
+ render(<TestComponent onSubmit={handleSubmit} />);
50
+
51
+ // username
52
+ let usernameField = screen.getByPlaceholderText(
53
+ /username/i,
54
+ ) as HTMLInputElement;
55
+ expect(usernameField.validity.valid).toBe(true);
56
+ expect(usernameField.validationMessage).toBe('');
57
+
58
+ // password
59
+ let passwordField = screen.getByPlaceholderText(
60
+ /password/i,
61
+ ) as HTMLInputElement;
62
+ expect(passwordField.validity.valid).toBe(true);
63
+ expect(passwordField.validationMessage).toBe('');
64
+
65
+ await act(async () => {
66
+ user.click(screen.getByText(/submit/i));
67
+ });
68
+
69
+ // username
70
+ usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement;
71
+ expect(usernameField.validity.valid).toBe(false);
72
+ expect(usernameField.validationMessage).toBe(USERNAME_REQUIRED_MESSAGE);
73
+
74
+ // password
75
+ passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
76
+ expect(passwordField.validity.valid).toBe(false);
77
+ expect(passwordField.validationMessage).toBe(PASSWORD_SYMBOL_MESSAGE);
78
+
79
+ await act(async () => {
80
+ user.type(screen.getByPlaceholderText(/username/i), 'joe');
81
+ user.type(screen.getByPlaceholderText(/password/i), 'password');
82
+ });
83
+
84
+ // username
85
+ usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement;
86
+ expect(usernameField.validity.valid).toBe(true);
87
+ expect(usernameField.validationMessage).toBe('');
88
+
89
+ // password
90
+ passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
91
+ expect(passwordField.validity.valid).toBe(true);
92
+ expect(passwordField.validationMessage).toBe('');
93
+ });
@@ -0,0 +1,64 @@
1
+ import React from 'react';
2
+ import { render, screen, act } from '@testing-library/react';
3
+ import user from '@testing-library/user-event';
4
+ import { useForm } from 'react-hook-form';
5
+ import vest from 'vest';
6
+ import { vestResolver } from '..';
7
+
8
+ interface FormData {
9
+ username: string;
10
+ password: string;
11
+ }
12
+
13
+ const validationSuite = vest.create('form', (data: FormData) => {
14
+ vest.test('username', 'Username is required', () => {
15
+ vest.enforce(data.username).isNotEmpty();
16
+ });
17
+
18
+ vest.test('password', 'Password must contain a symbol', () => {
19
+ vest.enforce(data.password).matches(/[^A-Za-z0-9]/);
20
+ });
21
+ });
22
+
23
+ interface Props {
24
+ onSubmit: (data: FormData) => void;
25
+ }
26
+
27
+ function TestComponent({ onSubmit }: Props) {
28
+ const {
29
+ register,
30
+ formState: { errors },
31
+ handleSubmit,
32
+ } = useForm<FormData>({
33
+ resolver: vestResolver(validationSuite), // Useful to check TypeScript regressions
34
+ });
35
+
36
+ return (
37
+ <form onSubmit={handleSubmit(onSubmit)}>
38
+ <input {...register('username')} />
39
+ {errors.username && <span role="alert">{errors.username.message}</span>}
40
+
41
+ <input {...register('password')} />
42
+ {errors.password && <span role="alert">{errors.password.message}</span>}
43
+
44
+ <button type="submit">submit</button>
45
+ </form>
46
+ );
47
+ }
48
+
49
+ test("form's validation with Vest and TypeScript's integration", async () => {
50
+ const handleSubmit = jest.fn();
51
+ render(<TestComponent onSubmit={handleSubmit} />);
52
+
53
+ expect(screen.queryAllByRole(/alert/i)).toHaveLength(0);
54
+
55
+ await act(async () => {
56
+ user.click(screen.getByText(/submit/i));
57
+ });
58
+
59
+ expect(screen.getByText(/Username is required/i)).toBeInTheDocument();
60
+ expect(
61
+ screen.getByText(/Password must contain a symbol/i),
62
+ ).toBeInTheDocument();
63
+ expect(handleSubmit).not.toHaveBeenCalled();
64
+ });
@@ -0,0 +1,67 @@
1
+ import { Field, InternalFieldName } from 'react-hook-form';
2
+ import * as vest from 'vest';
3
+
4
+ export const validationSuite = vest.create('form', (data: any = {}) => {
5
+ vest.test('username', 'Username is required', () => {
6
+ vest.enforce(data.username).isNotEmpty();
7
+ });
8
+
9
+ vest.test('username', 'Must be longer than 3 chars', () => {
10
+ vest.enforce(data.username).longerThan(3);
11
+ });
12
+
13
+ vest.test('deepObject.data', 'deepObject.data is required', () => {
14
+ vest.enforce(data.deepObject.data).isNotEmpty();
15
+ });
16
+
17
+ vest.test('password', 'Password is required', () => {
18
+ vest.enforce(data.password).isNotEmpty();
19
+ });
20
+
21
+ vest.test('password', 'Password must be at least 5 chars', () => {
22
+ vest.enforce(data.password).longerThanOrEquals(5);
23
+ });
24
+
25
+ vest.test('password', 'Password must contain a digit', () => {
26
+ vest.enforce(data.password).matches(/[0-9]/);
27
+ });
28
+
29
+ vest.test('password', 'Password must contain a symbol', () => {
30
+ vest.enforce(data.password).matches(/[^A-Za-z0-9]/);
31
+ });
32
+ });
33
+
34
+ export const validData = {
35
+ username: 'asdda',
36
+ password: 'asddfg123!',
37
+ deepObject: {
38
+ data: 'test',
39
+ },
40
+ };
41
+
42
+ export const invalidData = {
43
+ username: '',
44
+ password: 'a',
45
+ deepObject: {
46
+ data: '',
47
+ },
48
+ };
49
+
50
+ export const fields: Record<InternalFieldName, Field['_f']> = {
51
+ username: {
52
+ ref: { name: 'username' },
53
+ name: 'username',
54
+ },
55
+ password: {
56
+ ref: { name: 'password' },
57
+ name: 'password',
58
+ },
59
+ email: {
60
+ ref: { name: 'email' },
61
+ name: 'email',
62
+ },
63
+ birthday: {
64
+ ref: { name: 'birthday' },
65
+ name: 'birthday',
66
+ },
67
+ };
@@ -0,0 +1,141 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`vestResolver should return all the error messages from vestResolver when validation fails and validateAllFieldCriteria set to true 1`] = `
4
+ Object {
5
+ "errors": Object {
6
+ "deepObject": Object {
7
+ "data": Object {
8
+ "message": "deepObject.data is required",
9
+ "ref": undefined,
10
+ "type": "",
11
+ "types": Object {
12
+ "0": "deepObject.data is required",
13
+ },
14
+ },
15
+ },
16
+ "password": Object {
17
+ "message": "Password must be at least 5 chars",
18
+ "ref": Object {
19
+ "name": "password",
20
+ },
21
+ "type": "",
22
+ "types": Object {
23
+ "0": "Password must be at least 5 chars",
24
+ "1": "Password must contain a digit",
25
+ "2": "Password must contain a symbol",
26
+ },
27
+ },
28
+ "username": Object {
29
+ "message": "Username is required",
30
+ "ref": Object {
31
+ "name": "username",
32
+ },
33
+ "type": "",
34
+ "types": Object {
35
+ "0": "Username is required",
36
+ "1": "Must be longer than 3 chars",
37
+ },
38
+ },
39
+ },
40
+ "values": Object {},
41
+ }
42
+ `;
43
+
44
+ exports[`vestResolver should return all the error messages from vestResolver when validation fails and validateAllFieldCriteria set to true and \`mode: sync\` 1`] = `
45
+ Object {
46
+ "errors": Object {
47
+ "deepObject": Object {
48
+ "data": Object {
49
+ "message": "deepObject.data is required",
50
+ "ref": undefined,
51
+ "type": "",
52
+ "types": Object {
53
+ "0": "deepObject.data is required",
54
+ },
55
+ },
56
+ },
57
+ "password": Object {
58
+ "message": "Password must be at least 5 chars",
59
+ "ref": Object {
60
+ "name": "password",
61
+ },
62
+ "type": "",
63
+ "types": Object {
64
+ "0": "Password must be at least 5 chars",
65
+ "1": "Password must contain a digit",
66
+ "2": "Password must contain a symbol",
67
+ },
68
+ },
69
+ "username": Object {
70
+ "message": "Username is required",
71
+ "ref": Object {
72
+ "name": "username",
73
+ },
74
+ "type": "",
75
+ "types": Object {
76
+ "0": "Username is required",
77
+ "1": "Must be longer than 3 chars",
78
+ },
79
+ },
80
+ },
81
+ "values": Object {},
82
+ }
83
+ `;
84
+
85
+ exports[`vestResolver should return single error message from vestResolver when validation fails and validateAllFieldCriteria set to false 1`] = `
86
+ Object {
87
+ "errors": Object {
88
+ "deepObject": Object {
89
+ "data": Object {
90
+ "message": "deepObject.data is required",
91
+ "ref": undefined,
92
+ "type": "",
93
+ },
94
+ },
95
+ "password": Object {
96
+ "message": "Password must be at least 5 chars",
97
+ "ref": Object {
98
+ "name": "password",
99
+ },
100
+ "type": "",
101
+ },
102
+ "username": Object {
103
+ "message": "Username is required",
104
+ "ref": Object {
105
+ "name": "username",
106
+ },
107
+ "type": "",
108
+ },
109
+ },
110
+ "values": Object {},
111
+ }
112
+ `;
113
+
114
+ exports[`vestResolver should return single error message from vestResolver when validation fails and validateAllFieldCriteria set to false and \`mode: sync\` 1`] = `
115
+ Object {
116
+ "errors": Object {
117
+ "deepObject": Object {
118
+ "data": Object {
119
+ "message": "deepObject.data is required",
120
+ "ref": undefined,
121
+ "type": "",
122
+ },
123
+ },
124
+ "password": Object {
125
+ "message": "Password must be at least 5 chars",
126
+ "ref": Object {
127
+ "name": "password",
128
+ },
129
+ "type": "",
130
+ },
131
+ "username": Object {
132
+ "message": "Username is required",
133
+ "ref": Object {
134
+ "name": "username",
135
+ },
136
+ "type": "",
137
+ },
138
+ },
139
+ "values": Object {},
140
+ }
141
+ `;
@@ -0,0 +1,71 @@
1
+ import { vestResolver } from '..';
2
+ import {
3
+ invalidData,
4
+ validationSuite,
5
+ validData,
6
+ fields,
7
+ } from './__fixtures__/data';
8
+
9
+ const shouldUseNativeValidation = false;
10
+
11
+ describe('vestResolver', () => {
12
+ it('should return values from vestResolver when validation pass', async () => {
13
+ expect(
14
+ await vestResolver(validationSuite)(validData, undefined, {
15
+ fields,
16
+ shouldUseNativeValidation,
17
+ }),
18
+ ).toEqual({
19
+ values: validData,
20
+ errors: {},
21
+ });
22
+ });
23
+
24
+ it('should return values from vestResolver with `mode: sync` when validation pass', async () => {
25
+ expect(
26
+ await vestResolver(validationSuite, undefined, {
27
+ mode: 'sync',
28
+ })(validData, undefined, { fields, shouldUseNativeValidation }),
29
+ ).toEqual({
30
+ values: validData,
31
+ errors: {},
32
+ });
33
+ });
34
+
35
+ it('should return single error message from vestResolver when validation fails and validateAllFieldCriteria set to false', async () => {
36
+ expect(
37
+ await vestResolver(validationSuite)(invalidData, undefined, {
38
+ fields,
39
+ shouldUseNativeValidation,
40
+ }),
41
+ ).toMatchSnapshot();
42
+ });
43
+
44
+ it('should return single error message from vestResolver when validation fails and validateAllFieldCriteria set to false and `mode: sync`', async () => {
45
+ expect(
46
+ await vestResolver(validationSuite, undefined, {
47
+ mode: 'sync',
48
+ })(invalidData, undefined, { fields, shouldUseNativeValidation }),
49
+ ).toMatchSnapshot();
50
+ });
51
+
52
+ it('should return all the error messages from vestResolver when validation fails and validateAllFieldCriteria set to true', async () => {
53
+ expect(
54
+ await vestResolver(validationSuite)(
55
+ invalidData,
56
+ {},
57
+ { fields, criteriaMode: 'all', shouldUseNativeValidation },
58
+ ),
59
+ ).toMatchSnapshot();
60
+ });
61
+
62
+ it('should return all the error messages from vestResolver when validation fails and validateAllFieldCriteria set to true and `mode: sync`', async () => {
63
+ expect(
64
+ await vestResolver(validationSuite, undefined, { mode: 'sync' })(
65
+ invalidData,
66
+ {},
67
+ { fields, criteriaMode: 'all', shouldUseNativeValidation },
68
+ ),
69
+ ).toMatchSnapshot();
70
+ });
71
+ });
@@ -0,0 +1 @@
1
+ export * from './vest';
@@ -0,0 +1,20 @@
1
+ import {
2
+ ResolverOptions,
3
+ ResolverResult,
4
+ UnpackNestedValue,
5
+ } from 'react-hook-form';
6
+ import * as Vest from 'vest';
7
+
8
+ export type ICreateResult = ReturnType<typeof Vest.create>;
9
+
10
+ export type Resolver = <TFieldValues, TContext>(
11
+ schema: ICreateResult,
12
+ schemaOptions?: never,
13
+ factoryOptions?: { mode?: 'async' | 'sync' },
14
+ ) => (
15
+ values: UnpackNestedValue<TFieldValues>,
16
+ context: TContext | undefined,
17
+ options: ResolverOptions<TFieldValues>,
18
+ ) => Promise<ResolverResult<TFieldValues>>;
19
+
20
+ export type VestErrors = Record<string, string[]>;
@@ -0,0 +1,51 @@
1
+ import { toNestError, validateFieldsNatively } from '@hookform/resolvers';
2
+ import { FieldError } from 'react-hook-form';
3
+ import promisify from 'vest/promisify';
4
+ import type { VestErrors, Resolver } from './types';
5
+
6
+ const parseErrorSchema = (
7
+ vestError: VestErrors,
8
+ validateAllFieldCriteria: boolean,
9
+ ) => {
10
+ const errors: Record<string, FieldError> = {};
11
+ for (const path in vestError) {
12
+ if (!errors[path]) {
13
+ errors[path] = { message: vestError[path][0], type: '' };
14
+ }
15
+
16
+ if (validateAllFieldCriteria) {
17
+ errors[path].types = vestError[path].reduce<Record<number, string>>(
18
+ (acc, message, index) => (acc[index] = message) && acc,
19
+ {},
20
+ );
21
+ }
22
+ }
23
+ return errors;
24
+ };
25
+
26
+ export const vestResolver: Resolver =
27
+ (schema, _, resolverOptions = {}) =>
28
+ async (values, _context, options) => {
29
+ const result =
30
+ resolverOptions.mode === 'sync'
31
+ ? schema(values)
32
+ : await promisify(schema)(values);
33
+
34
+ if (result.hasErrors()) {
35
+ return {
36
+ values: {},
37
+ errors: toNestError(
38
+ parseErrorSchema(
39
+ result.getErrors(),
40
+ !options.shouldUseNativeValidation &&
41
+ options.criteriaMode === 'all',
42
+ ),
43
+ options,
44
+ ),
45
+ };
46
+ }
47
+
48
+ options.shouldUseNativeValidation && validateFieldsNatively({}, options);
49
+
50
+ return { values, errors: {} };
51
+ };
@@ -0,0 +1,2 @@
1
+ export * from './yup';
2
+ export * from './types';
@@ -0,0 +1,8 @@
1
+ import { ResolverOptions, ResolverResult } from 'react-hook-form';
2
+ import * as Yup from 'yup';
3
+ import type Lazy from 'yup/lib/Lazy';
4
+ declare type Options<T extends Yup.AnyObjectSchema | Lazy<any>> = Parameters<T['validate']>[1];
5
+ export declare type Resolver = <T extends Yup.AnyObjectSchema | Lazy<any>, TContext extends object = object>(schema: T, schemaOptions?: Options<T>, factoryOptions?: {
6
+ mode?: 'async' | 'sync';
7
+ }) => (values: unknown, context: TContext | undefined, options: ResolverOptions<Yup.InferType<T>>) => Promise<ResolverResult<Yup.InferType<T>>>;
8
+ export {};
@@ -0,0 +1,2 @@
1
+ import { Resolver } from './types';
2
+ export declare const yupResolver: Resolver;
@@ -0,0 +1,2 @@
1
+ var e=require("@hookform/resolvers"),t=require("react-hook-form");exports.yupResolver=function(r,o,n){return void 0===o&&(o={}),void 0===n&&(n={}),function(s,a,i){try{return Promise.resolve(function(t,u){try{var c=(o.context&&"development"===process.env.NODE_ENV&&console.warn("You should not used the yup options context. Please, use the 'useForm' context object instead"),Promise.resolve(r["sync"===n.mode?"validateSync":"validate"](s,Object.assign({abortEarly:!1},o,{context:a}))).then(function(t){return i.shouldUseNativeValidation&&e.validateFieldsNatively({},i),{values:t,errors:{}}}))}catch(e){return u(e)}return c&&c.then?c.then(void 0,u):c}(0,function(r){return{values:{},errors:e.toNestError((o=r,n=!i.shouldUseNativeValidation&&"all"===i.criteriaMode,o.inner.reduce(function(e,r){if(e[r.path]||(e[r.path]={message:r.message,type:r.type}),n){var o=e[r.path].types,s=o&&o[r.type];e[r.path]=t.appendErrors(r.path,n,e,r.type,s?[].concat(s,r.message):r.message)}return e},{})),i)};var o,n}))}catch(e){return Promise.reject(e)}}};
2
+ //# sourceMappingURL=yup.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"yup.js","sources":["../src/yup.ts"],"sourcesContent":["import Yup, { ValidationError } from 'yup';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport { appendErrors, FieldError } from 'react-hook-form';\nimport { Resolver } from './types';\n\n/**\n * Why `path!` ? because it could be `undefined` in some case\n * https://github.com/jquense/yup#validationerrorerrors-string--arraystring-value-any-path-string\n */\nconst parseErrorSchema = (\n error: Yup.ValidationError,\n validateAllFieldCriteria: boolean,\n) => {\n return error.inner.reduce<Record<string, FieldError>>((previous, error) => {\n if (!previous[error.path!]) {\n previous[error.path!] = { message: error.message, type: error.type! };\n }\n\n if (validateAllFieldCriteria) {\n const types = previous[error.path!].types;\n const messages = types && types[error.type!];\n\n previous[error.path!] = appendErrors(\n error.path!,\n validateAllFieldCriteria,\n previous,\n error.type!,\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n return previous;\n }, {});\n};\n\nexport const yupResolver: Resolver =\n (schema, schemaOptions = {}, resolverOptions = {}) =>\n async (values, context, options) => {\n try {\n if (schemaOptions.context && process.env.NODE_ENV === 'development') {\n // eslint-disable-next-line no-console\n console.warn(\n \"You should not used the yup options context. Please, use the 'useForm' context object instead\",\n );\n }\n\n const result = await schema[\n resolverOptions.mode === 'sync' ? 'validateSync' : 'validate'\n ](\n values,\n Object.assign({ abortEarly: false }, schemaOptions, { context }),\n );\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: result,\n errors: {},\n };\n } catch (e) {\n return {\n values: {},\n errors: toNestError(\n parseErrorSchema(\n e as ValidationError,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n }\n };\n"],"names":["schema","schemaOptions","resolverOptions","values","context","options","process","env","NODE_ENV","console","warn","mode","Object","assign","abortEarly","result","shouldUseNativeValidation","validateFieldsNatively","errors","e","toNestError","error","validateAllFieldCriteria","criteriaMode","inner","reduce","previous","path","message","type","types","messages","appendErrors","concat"],"mappings":"sFAsCE,SAACA,EAAQC,EAAoBC,mBAApBD,IAAAA,EAAgB,aAAIC,IAAAA,EAAkB,aACxCC,EAAQC,EAASC,uDAEhBJ,EAAcG,SAAoC,gBAAzBE,QAAQC,IAAIC,UAEvCC,QAAQC,KACN,iHAIiBV,EACM,SAAzBE,EAAgBS,KAAkB,eAAiB,YAEnDR,EACAS,OAAOC,OAAO,CAAEC,YAAY,GAASb,EAAe,CAAEG,QAAAA,oBAJlDW,GASN,OAFAV,EAAQW,2BAA6BC,yBAAuB,GAAIZ,GAEzD,CACLF,OAAQY,EACRG,OAAQ,4EAEHC,GACP,MAAO,CACLhB,OAAQ,GACRe,OAAQE,eAtDdC,EAwDUF,EAvDVG,GAwDWjB,EAAQW,2BACkB,QAAzBX,EAAQkB,aAvDbF,EAAMG,MAAMC,OAAmC,SAACC,EAAUL,GAK/D,GAJKK,EAASL,EAAMM,QAClBD,EAASL,EAAMM,MAAS,CAAEC,QAASP,EAAMO,QAASC,KAAMR,EAAMQ,OAG5DP,EAA0B,CAC5B,IAAMQ,EAAQJ,EAASL,EAAMM,MAAOG,MAC9BC,EAAWD,GAASA,EAAMT,EAAMQ,MAEtCH,EAASL,EAAMM,MAASK,eACtBX,EAAMM,KACNL,EACAI,EACAL,EAAMQ,KACNE,EACK,GAAgBE,OAAOF,EAAsBV,EAAMO,SACpDP,EAAMO,SAId,OAAOF,GACN,KAoCKrB,IA7De,IACvBgB,EACAC,KA2BA"}
@@ -0,0 +1,2 @@
1
+ import{validateFieldsNatively as e,toNestError as t}from"@hookform/resolvers";import{appendErrors as r}from"react-hook-form";var o=function(o,n,a){return void 0===n&&(n={}),void 0===a&&(a={}),function(s,i,c){try{return Promise.resolve(function(t,r){try{var u=(n.context&&"development"===process.env.NODE_ENV&&console.warn("You should not used the yup options context. Please, use the 'useForm' context object instead"),Promise.resolve(o["sync"===a.mode?"validateSync":"validate"](s,Object.assign({abortEarly:!1},n,{context:i}))).then(function(t){return c.shouldUseNativeValidation&&e({},c),{values:t,errors:{}}}))}catch(e){return r(e)}return u&&u.then?u.then(void 0,r):u}(0,function(e){return{values:{},errors:t((o=e,n=!c.shouldUseNativeValidation&&"all"===c.criteriaMode,o.inner.reduce(function(e,t){if(e[t.path]||(e[t.path]={message:t.message,type:t.type}),n){var o=e[t.path].types,a=o&&o[t.type];e[t.path]=r(t.path,n,e,t.type,a?[].concat(a,t.message):t.message)}return e},{})),c)};var o,n}))}catch(e){return Promise.reject(e)}}};export{o as yupResolver};
2
+ //# sourceMappingURL=yup.module.js.map
@@ -0,0 +1,2 @@
1
+ import{validateFieldsNatively as e,toNestError as t}from"@hookform/resolvers";import{appendErrors as o}from"react-hook-form";const a=(a,s={},r={})=>async(n,c,i)=>{try{s.context&&"development"===process.env.NODE_ENV&&console.warn("You should not used the yup options context. Please, use the 'useForm' context object instead");const t=await a["sync"===r.mode?"validateSync":"validate"](n,Object.assign({abortEarly:!1},s,{context:c}));return i.shouldUseNativeValidation&&e({},i),{values:t,errors:{}}}catch(e){return{values:{},errors:t((p=e,l=!i.shouldUseNativeValidation&&"all"===i.criteriaMode,p.inner.reduce((e,t)=>{if(e[t.path]||(e[t.path]={message:t.message,type:t.type}),l){const a=e[t.path].types,s=a&&a[t.type];e[t.path]=o(t.path,l,e,t.type,s?[].concat(s,t.message):t.message)}return e},{})),i)}}var p,l};export{a as yupResolver};
2
+ //# sourceMappingURL=yup.modern.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"yup.modern.js","sources":["../src/yup.ts"],"sourcesContent":["import Yup, { ValidationError } from 'yup';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport { appendErrors, FieldError } from 'react-hook-form';\nimport { Resolver } from './types';\n\n/**\n * Why `path!` ? because it could be `undefined` in some case\n * https://github.com/jquense/yup#validationerrorerrors-string--arraystring-value-any-path-string\n */\nconst parseErrorSchema = (\n error: Yup.ValidationError,\n validateAllFieldCriteria: boolean,\n) => {\n return error.inner.reduce<Record<string, FieldError>>((previous, error) => {\n if (!previous[error.path!]) {\n previous[error.path!] = { message: error.message, type: error.type! };\n }\n\n if (validateAllFieldCriteria) {\n const types = previous[error.path!].types;\n const messages = types && types[error.type!];\n\n previous[error.path!] = appendErrors(\n error.path!,\n validateAllFieldCriteria,\n previous,\n error.type!,\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n return previous;\n }, {});\n};\n\nexport const yupResolver: Resolver =\n (schema, schemaOptions = {}, resolverOptions = {}) =>\n async (values, context, options) => {\n try {\n if (schemaOptions.context && process.env.NODE_ENV === 'development') {\n // eslint-disable-next-line no-console\n console.warn(\n \"You should not used the yup options context. Please, use the 'useForm' context object instead\",\n );\n }\n\n const result = await schema[\n resolverOptions.mode === 'sync' ? 'validateSync' : 'validate'\n ](\n values,\n Object.assign({ abortEarly: false }, schemaOptions, { context }),\n );\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: result,\n errors: {},\n };\n } catch (e) {\n return {\n values: {},\n errors: toNestError(\n parseErrorSchema(\n e as ValidationError,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n }\n };\n"],"names":["yupResolver","schema","schemaOptions","resolverOptions","async","values","context","options","process","env","NODE_ENV","console","warn","result","mode","Object","assign","abortEarly","shouldUseNativeValidation","validateFieldsNatively","errors","e","toNestError","error","validateAllFieldCriteria","criteriaMode","inner","reduce","previous","path","message","type","types","messages","appendErrors","concat"],"mappings":"6HASA,MA4BaA,EACX,CAACC,EAAQC,EAAgB,GAAIC,EAAkB,KAC/CC,MAAOC,EAAQC,EAASC,KACtB,IACML,EAAcI,SAAoC,gBAAzBE,QAAQC,IAAIC,UAEvCC,QAAQC,KACN,iGAIJ,MAAMC,QAAeZ,EACM,SAAzBE,EAAgBW,KAAkB,eAAiB,YAEnDT,EACAU,OAAOC,OAAO,CAAEC,YAAY,GAASf,EAAe,CAAEI,QAAAA,KAKxD,OAFAC,EAAQW,2BAA6BC,EAAuB,GAAIZ,GAEzD,CACLF,OAAQQ,EACRO,OAAQ,IAEV,MAAOC,GACP,MAAO,CACLhB,OAAQ,GACRe,OAAQE,GAtDdC,EAwDUF,EAvDVG,GAwDWjB,EAAQW,2BACkB,QAAzBX,EAAQkB,aAvDbF,EAAMG,MAAMC,OAAmC,CAACC,EAAUL,KAK/D,GAJKK,EAASL,EAAMM,QAClBD,EAASL,EAAMM,MAAS,CAAEC,QAASP,EAAMO,QAASC,KAAMR,EAAMQ,OAG5DP,EAA0B,CAC5B,MAAMQ,EAAQJ,EAASL,EAAMM,MAAOG,MAC9BC,EAAWD,GAASA,EAAMT,EAAMQ,MAEtCH,EAASL,EAAMM,MAASK,EACtBX,EAAMM,KACNL,EACAI,EACAL,EAAMQ,KACNE,EACK,GAAgBE,OAAOF,EAAsBV,EAAMO,SACpDP,EAAMO,SAId,OAAOF,GACN,KAoCKrB,IA7De,IACvBgB,EACAC"}
@@ -0,0 +1,2 @@
1
+ import{validateFieldsNatively as e,toNestError as t}from"@hookform/resolvers";import{appendErrors as r}from"react-hook-form";var o=function(o,n,a){return void 0===n&&(n={}),void 0===a&&(a={}),function(s,i,c){try{return Promise.resolve(function(t,r){try{var u=(n.context&&"development"===process.env.NODE_ENV&&console.warn("You should not used the yup options context. Please, use the 'useForm' context object instead"),Promise.resolve(o["sync"===a.mode?"validateSync":"validate"](s,Object.assign({abortEarly:!1},n,{context:i}))).then(function(t){return c.shouldUseNativeValidation&&e({},c),{values:t,errors:{}}}))}catch(e){return r(e)}return u&&u.then?u.then(void 0,r):u}(0,function(e){return{values:{},errors:t((o=e,n=!c.shouldUseNativeValidation&&"all"===c.criteriaMode,o.inner.reduce(function(e,t){if(e[t.path]||(e[t.path]={message:t.message,type:t.type}),n){var o=e[t.path].types,a=o&&o[t.type];e[t.path]=r(t.path,n,e,t.type,a?[].concat(a,t.message):t.message)}return e},{})),c)};var o,n}))}catch(e){return Promise.reject(e)}}};export{o as yupResolver};
2
+ //# sourceMappingURL=yup.module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"yup.module.js","sources":["../src/yup.ts"],"sourcesContent":["import Yup, { ValidationError } from 'yup';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport { appendErrors, FieldError } from 'react-hook-form';\nimport { Resolver } from './types';\n\n/**\n * Why `path!` ? because it could be `undefined` in some case\n * https://github.com/jquense/yup#validationerrorerrors-string--arraystring-value-any-path-string\n */\nconst parseErrorSchema = (\n error: Yup.ValidationError,\n validateAllFieldCriteria: boolean,\n) => {\n return error.inner.reduce<Record<string, FieldError>>((previous, error) => {\n if (!previous[error.path!]) {\n previous[error.path!] = { message: error.message, type: error.type! };\n }\n\n if (validateAllFieldCriteria) {\n const types = previous[error.path!].types;\n const messages = types && types[error.type!];\n\n previous[error.path!] = appendErrors(\n error.path!,\n validateAllFieldCriteria,\n previous,\n error.type!,\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n return previous;\n }, {});\n};\n\nexport const yupResolver: Resolver =\n (schema, schemaOptions = {}, resolverOptions = {}) =>\n async (values, context, options) => {\n try {\n if (schemaOptions.context && process.env.NODE_ENV === 'development') {\n // eslint-disable-next-line no-console\n console.warn(\n \"You should not used the yup options context. Please, use the 'useForm' context object instead\",\n );\n }\n\n const result = await schema[\n resolverOptions.mode === 'sync' ? 'validateSync' : 'validate'\n ](\n values,\n Object.assign({ abortEarly: false }, schemaOptions, { context }),\n );\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: result,\n errors: {},\n };\n } catch (e) {\n return {\n values: {},\n errors: toNestError(\n parseErrorSchema(\n e as ValidationError,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n }\n };\n"],"names":["yupResolver","schema","schemaOptions","resolverOptions","values","context","options","process","env","NODE_ENV","console","warn","mode","Object","assign","abortEarly","result","shouldUseNativeValidation","validateFieldsNatively","errors","e","toNestError","error","validateAllFieldCriteria","criteriaMode","inner","reduce","previous","path","message","type","types","messages","appendErrors","concat"],"mappings":"6HASA,IA4BaA,EACX,SAACC,EAAQC,EAAoBC,mBAApBD,IAAAA,EAAgB,aAAIC,IAAAA,EAAkB,aACxCC,EAAQC,EAASC,uDAEhBJ,EAAcG,SAAoC,gBAAzBE,QAAQC,IAAIC,UAEvCC,QAAQC,KACN,iHAIiBV,EACM,SAAzBE,EAAgBS,KAAkB,eAAiB,YAEnDR,EACAS,OAAOC,OAAO,CAAEC,YAAY,GAASb,EAAe,CAAEG,QAAAA,oBAJlDW,GASN,OAFAV,EAAQW,2BAA6BC,EAAuB,GAAIZ,GAEzD,CACLF,OAAQY,EACRG,OAAQ,4EAEHC,GACP,MAAO,CACLhB,OAAQ,GACRe,OAAQE,GAtDdC,EAwDUF,EAvDVG,GAwDWjB,EAAQW,2BACkB,QAAzBX,EAAQkB,aAvDbF,EAAMG,MAAMC,OAAmC,SAACC,EAAUL,GAK/D,GAJKK,EAASL,EAAMM,QAClBD,EAASL,EAAMM,MAAS,CAAEC,QAASP,EAAMO,QAASC,KAAMR,EAAMQ,OAG5DP,EAA0B,CAC5B,IAAMQ,EAAQJ,EAASL,EAAMM,MAAOG,MAC9BC,EAAWD,GAASA,EAAMT,EAAMQ,MAEtCH,EAASL,EAAMM,MAASK,EACtBX,EAAMM,KACNL,EACAI,EACAL,EAAMQ,KACNE,EACK,GAAgBE,OAAOF,EAAsBV,EAAMO,SACpDP,EAAMO,SAId,OAAOF,GACN,KAoCKrB,IA7De,IACvBgB,EACAC,KA2BA"}
@@ -0,0 +1,2 @@
1
+ !function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports,require("@hookform/resolvers"),require("react-hook-form")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers","react-hook-form"],o):o((e||self).hookformResolversYup={},e.hookformResolvers,e.reactHookForm)}(this,function(e,o,t){e.yupResolver=function(e,r,n){return void 0===r&&(r={}),void 0===n&&(n={}),function(s,a,i){try{return Promise.resolve(function(t,u){try{var c=(r.context&&"development"===process.env.NODE_ENV&&console.warn("You should not used the yup options context. Please, use the 'useForm' context object instead"),Promise.resolve(e["sync"===n.mode?"validateSync":"validate"](s,Object.assign({abortEarly:!1},r,{context:a}))).then(function(e){return i.shouldUseNativeValidation&&o.validateFieldsNatively({},i),{values:e,errors:{}}}))}catch(e){return u(e)}return c&&c.then?c.then(void 0,u):c}(0,function(e){return{values:{},errors:o.toNestError((r=e,n=!i.shouldUseNativeValidation&&"all"===i.criteriaMode,r.inner.reduce(function(e,o){if(e[o.path]||(e[o.path]={message:o.message,type:o.type}),n){var r=e[o.path].types,s=r&&r[o.type];e[o.path]=t.appendErrors(o.path,n,e,o.type,s?[].concat(s,o.message):o.message)}return e},{})),i)};var r,n}))}catch(e){return Promise.reject(e)}}}});
2
+ //# sourceMappingURL=yup.umd.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"yup.umd.js","sources":["../src/yup.ts"],"sourcesContent":["import Yup, { ValidationError } from 'yup';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport { appendErrors, FieldError } from 'react-hook-form';\nimport { Resolver } from './types';\n\n/**\n * Why `path!` ? because it could be `undefined` in some case\n * https://github.com/jquense/yup#validationerrorerrors-string--arraystring-value-any-path-string\n */\nconst parseErrorSchema = (\n error: Yup.ValidationError,\n validateAllFieldCriteria: boolean,\n) => {\n return error.inner.reduce<Record<string, FieldError>>((previous, error) => {\n if (!previous[error.path!]) {\n previous[error.path!] = { message: error.message, type: error.type! };\n }\n\n if (validateAllFieldCriteria) {\n const types = previous[error.path!].types;\n const messages = types && types[error.type!];\n\n previous[error.path!] = appendErrors(\n error.path!,\n validateAllFieldCriteria,\n previous,\n error.type!,\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n return previous;\n }, {});\n};\n\nexport const yupResolver: Resolver =\n (schema, schemaOptions = {}, resolverOptions = {}) =>\n async (values, context, options) => {\n try {\n if (schemaOptions.context && process.env.NODE_ENV === 'development') {\n // eslint-disable-next-line no-console\n console.warn(\n \"You should not used the yup options context. Please, use the 'useForm' context object instead\",\n );\n }\n\n const result = await schema[\n resolverOptions.mode === 'sync' ? 'validateSync' : 'validate'\n ](\n values,\n Object.assign({ abortEarly: false }, schemaOptions, { context }),\n );\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: result,\n errors: {},\n };\n } catch (e) {\n return {\n values: {},\n errors: toNestError(\n parseErrorSchema(\n e as ValidationError,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n }\n };\n"],"names":["schema","schemaOptions","resolverOptions","values","context","options","process","env","NODE_ENV","console","warn","mode","Object","assign","abortEarly","result","shouldUseNativeValidation","validateFieldsNatively","errors","e","toNestError","error","validateAllFieldCriteria","criteriaMode","inner","reduce","previous","path","message","type","types","messages","appendErrors","concat"],"mappings":"sYAsCE,SAACA,EAAQC,EAAoBC,mBAApBD,IAAAA,EAAgB,aAAIC,IAAAA,EAAkB,aACxCC,EAAQC,EAASC,uDAEhBJ,EAAcG,SAAoC,gBAAzBE,QAAQC,IAAIC,UAEvCC,QAAQC,KACN,iHAIiBV,EACM,SAAzBE,EAAgBS,KAAkB,eAAiB,YAEnDR,EACAS,OAAOC,OAAO,CAAEC,YAAY,GAASb,EAAe,CAAEG,QAAAA,oBAJlDW,GASN,OAFAV,EAAQW,2BAA6BC,yBAAuB,GAAIZ,GAEzD,CACLF,OAAQY,EACRG,OAAQ,4EAEHC,GACP,MAAO,CACLhB,OAAQ,GACRe,OAAQE,eAtDdC,EAwDUF,EAvDVG,GAwDWjB,EAAQW,2BACkB,QAAzBX,EAAQkB,aAvDbF,EAAMG,MAAMC,OAAmC,SAACC,EAAUL,GAK/D,GAJKK,EAASL,EAAMM,QAClBD,EAASL,EAAMM,MAAS,CAAEC,QAASP,EAAMO,QAASC,KAAMR,EAAMQ,OAG5DP,EAA0B,CAC5B,IAAMQ,EAAQJ,EAASL,EAAMM,MAAOG,MAC9BC,EAAWD,GAASA,EAAMT,EAAMQ,MAEtCH,EAASL,EAAMM,MAASK,eACtBX,EAAMM,KACNL,EACAI,EACAL,EAAMQ,KACNE,EACK,GAAgBE,OAAOF,EAAsBV,EAAMO,SACpDP,EAAMO,SAId,OAAOF,GACN,KAoCKrB,IA7De,IACvBgB,EACAC,KA2BA"}
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "yup",
3
+ "amdName": "hookformResolversYup",
4
+ "version": "1.0.0",
5
+ "private": true,
6
+ "description": "React Hook Form validation resolver: yup",
7
+ "main": "dist/yup.js",
8
+ "module": "dist/yup.module.js",
9
+ "umd:main": "dist/yup.umd.js",
10
+ "source": "src/index.ts",
11
+ "types": "dist/index.d.ts",
12
+ "license": "MIT",
13
+ "peerDependencies": {
14
+ "react-hook-form": "^7.0.0",
15
+ "@hookform/resolvers": "^2.0.0"
16
+ }
17
+ }