@hookform/resolvers 2.0.0-beta.7 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/README.md +19 -10
  2. package/dist/index.d.ts +1 -1
  3. package/dist/resolvers.js +1 -1
  4. package/dist/resolvers.js.map +1 -1
  5. package/dist/resolvers.mjs +1 -1
  6. package/dist/resolvers.modern.js +1 -1
  7. package/dist/resolvers.modern.js.map +1 -1
  8. package/dist/resolvers.module.js +1 -1
  9. package/dist/resolvers.module.js.map +1 -1
  10. package/dist/resolvers.umd.js +1 -1
  11. package/dist/resolvers.umd.js.map +1 -1
  12. package/dist/toNestError.d.ts +2 -0
  13. package/joi/dist/joi.js +1 -1
  14. package/joi/dist/joi.js.map +1 -1
  15. package/joi/dist/joi.mjs +1 -1
  16. package/joi/dist/joi.modern.js +1 -1
  17. package/joi/dist/joi.modern.js.map +1 -1
  18. package/joi/dist/joi.module.js +1 -1
  19. package/joi/dist/joi.module.js.map +1 -1
  20. package/joi/dist/joi.umd.js +1 -1
  21. package/joi/dist/joi.umd.js.map +1 -1
  22. package/joi/dist/types.d.ts +1 -1
  23. package/joi/src/__tests__/__fixtures__/data.ts +76 -0
  24. package/joi/src/__tests__/__snapshots__/joi.ts.snap +118 -0
  25. package/joi/src/__tests__/joi.ts +35 -88
  26. package/joi/src/joi.ts +47 -64
  27. package/joi/src/types.ts +1 -1
  28. package/package.json +14 -15
  29. package/superstruct/dist/superstruct.js +1 -1
  30. package/superstruct/dist/superstruct.js.map +1 -1
  31. package/superstruct/dist/superstruct.mjs +1 -1
  32. package/superstruct/dist/superstruct.modern.js +1 -1
  33. package/superstruct/dist/superstruct.modern.js.map +1 -1
  34. package/superstruct/dist/superstruct.module.js +1 -1
  35. package/superstruct/dist/superstruct.module.js.map +1 -1
  36. package/superstruct/dist/superstruct.umd.js +1 -1
  37. package/superstruct/dist/superstruct.umd.js.map +1 -1
  38. package/superstruct/dist/types.d.ts +1 -1
  39. package/superstruct/package.json +2 -1
  40. package/superstruct/src/__tests__/__fixtures__/data.ts +73 -0
  41. package/superstruct/src/__tests__/__snapshots__/superstruct.ts.snap +26 -56
  42. package/superstruct/src/__tests__/superstruct.ts +6 -65
  43. package/superstruct/src/superstruct.ts +20 -53
  44. package/superstruct/src/types.ts +1 -1
  45. package/vest/dist/vest.js +1 -1
  46. package/vest/dist/vest.js.map +1 -1
  47. package/vest/dist/vest.mjs +1 -1
  48. package/vest/dist/vest.modern.js +1 -1
  49. package/vest/dist/vest.modern.js.map +1 -1
  50. package/vest/dist/vest.module.js +1 -1
  51. package/vest/dist/vest.module.js.map +1 -1
  52. package/vest/dist/vest.umd.js +1 -1
  53. package/vest/dist/vest.umd.js.map +1 -1
  54. package/vest/package.json +3 -1
  55. package/vest/src/__tests__/__fixtures__/data.ts +67 -0
  56. package/vest/src/__tests__/__snapshots__/vest.ts.snap +28 -0
  57. package/vest/src/__tests__/vest.ts +22 -95
  58. package/vest/src/types.ts +1 -1
  59. package/vest/src/vest.ts +31 -42
  60. package/yup/dist/types.d.ts +4 -3
  61. package/yup/dist/yup.js +1 -1
  62. package/yup/dist/yup.js.map +1 -1
  63. package/yup/dist/yup.mjs +1 -1
  64. package/yup/dist/yup.modern.js +1 -1
  65. package/yup/dist/yup.modern.js.map +1 -1
  66. package/yup/dist/yup.module.js +1 -1
  67. package/yup/dist/yup.module.js.map +1 -1
  68. package/yup/dist/yup.umd.js +1 -1
  69. package/yup/dist/yup.umd.js.map +1 -1
  70. package/yup/package.json +2 -1
  71. package/yup/src/__tests__/__fixtures__/data.ts +65 -0
  72. package/yup/src/__tests__/__snapshots__/yup.ts.snap +99 -13
  73. package/yup/src/__tests__/yup.ts +23 -97
  74. package/yup/src/types.ts +6 -3
  75. package/yup/src/yup.ts +30 -60
  76. package/zod/dist/types.d.ts +1 -1
  77. package/zod/dist/zod.js +1 -1
  78. package/zod/dist/zod.js.map +1 -1
  79. package/zod/dist/zod.mjs +1 -1
  80. package/zod/dist/zod.modern.js +1 -1
  81. package/zod/dist/zod.modern.js.map +1 -1
  82. package/zod/dist/zod.module.js +1 -1
  83. package/zod/dist/zod.module.js.map +1 -1
  84. package/zod/dist/zod.umd.js +1 -1
  85. package/zod/dist/zod.umd.js.map +1 -1
  86. package/zod/src/__tests__/__fixtures__/data.ts +68 -0
  87. package/zod/src/__tests__/__snapshots__/zod.ts.snap +146 -0
  88. package/zod/src/__tests__/zod.ts +19 -79
  89. package/zod/src/types.ts +1 -1
  90. package/zod/src/zod.ts +45 -50
  91. package/dist/convertArrayToPathName.d.ts +0 -1
package/zod/src/zod.ts CHANGED
@@ -1,70 +1,65 @@
1
- import { appendErrors, transformToNestObject } from 'react-hook-form';
1
+ import { appendErrors, FieldError } from 'react-hook-form';
2
2
  import * as z from 'zod';
3
- // @ts-expect-error maybe fixed after the first publish ?
4
- import { convertArrayToPathName } from '@hookform/resolvers';
3
+ import { toNestError } from '@hookform/resolvers';
5
4
  import type { Resolver } from './types';
6
5
 
7
6
  const parseErrorSchema = (
8
- zodError: z.ZodError,
7
+ zodErrors: z.ZodSuberror[],
9
8
  validateAllFieldCriteria: boolean,
10
9
  ) => {
11
- if (zodError.isEmpty) {
12
- return {};
13
- }
10
+ const errors: Record<string, FieldError> = {};
11
+ for (; zodErrors.length; ) {
12
+ const error = zodErrors[0];
13
+ const { code, message, path } = error;
14
+ const _path = path.join('.');
15
+
16
+ if (!errors[_path]) {
17
+ errors[_path] = { message, type: code };
18
+ }
19
+
20
+ if ('unionErrors' in error) {
21
+ error.unionErrors.forEach((unionError) =>
22
+ unionError.errors.forEach((e) => zodErrors.push(e)),
23
+ );
24
+ }
25
+
26
+ if (validateAllFieldCriteria) {
27
+ errors[_path] = appendErrors(
28
+ _path,
29
+ validateAllFieldCriteria,
30
+ errors,
31
+ code,
32
+ message,
33
+ ) as FieldError;
34
+ }
14
35
 
15
- return zodError.errors.reduce<Record<string, any>>(
16
- (previous, { path, message, code: type }) => {
17
- const currentPath = convertArrayToPathName(path);
36
+ zodErrors.shift();
37
+ }
18
38
 
19
- return {
20
- ...previous,
21
- ...(path
22
- ? previous[currentPath] && validateAllFieldCriteria
23
- ? {
24
- [currentPath]: appendErrors(
25
- currentPath,
26
- validateAllFieldCriteria,
27
- previous,
28
- type,
29
- message,
30
- ),
31
- }
32
- : {
33
- [currentPath]: previous[currentPath] || {
34
- message,
35
- type,
36
- ...(validateAllFieldCriteria
37
- ? {
38
- types: { [type]: message || true },
39
- }
40
- : {}),
41
- },
42
- }
43
- : {}),
44
- };
45
- },
46
- {},
47
- );
39
+ return errors;
48
40
  };
49
41
 
50
42
  export const zodResolver: Resolver = (
51
43
  schema,
52
44
  schemaOptions,
53
- { mode } = { mode: 'async' },
54
- ) => async (values, _, { criteriaMode }) => {
45
+ resolverOptions = {},
46
+ ) => async (values, _, options) => {
55
47
  try {
56
- const result =
57
- mode === 'async'
58
- ? await schema.parseAsync(values, schemaOptions)
59
- : schema.parse(values, schemaOptions);
60
-
61
- return { values: result, errors: {} };
48
+ return {
49
+ errors: {},
50
+ values: await schema[
51
+ resolverOptions.mode === 'sync' ? 'parse' : 'parseAsync'
52
+ ](values, schemaOptions),
53
+ };
62
54
  } catch (error) {
63
55
  return {
64
56
  values: {},
65
- errors: transformToNestObject(
66
- parseErrorSchema(error, criteriaMode === 'all'),
67
- ),
57
+ errors: error.isEmpty
58
+ ? {}
59
+ : toNestError(
60
+ parseErrorSchema(error.errors, options.criteriaMode === 'all'),
61
+ options.fields,
62
+ ),
68
63
  };
69
64
  }
70
65
  };
@@ -1 +0,0 @@
1
- export declare const convertArrayToPathName: (paths: (string | number)[]) => string;