@hookform/resolvers 5.4.1 → 5.4.3

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 (64) hide show
  1. package/arktype/src/__tests__/__snapshots__/arktype.ts.snap +11 -11
  2. package/arktype/src/__tests__/arktype.ts +15 -6
  3. package/computed-types/src/__tests__/computed-types.ts +15 -6
  4. package/effect-ts/src/__tests__/effect-ts.ts +15 -6
  5. package/io-ts/src/__tests__/io-ts.ts +15 -6
  6. package/package.json +1 -1
  7. package/standard-schema/dist/standard-schema.js +1 -1
  8. package/standard-schema/dist/standard-schema.js.map +1 -1
  9. package/standard-schema/dist/standard-schema.mjs +1 -1
  10. package/standard-schema/dist/standard-schema.modern.mjs +1 -1
  11. package/standard-schema/dist/standard-schema.modern.mjs.map +1 -1
  12. package/standard-schema/dist/standard-schema.module.js +1 -1
  13. package/standard-schema/dist/standard-schema.module.js.map +1 -1
  14. package/standard-schema/dist/standard-schema.umd.js +1 -1
  15. package/standard-schema/dist/standard-schema.umd.js.map +1 -1
  16. package/standard-schema/src/__tests__/__snapshots__/standard-schema.ts.snap +31 -0
  17. package/standard-schema/src/__tests__/standard-schema.ts +126 -13
  18. package/standard-schema/src/standard-schema.ts +62 -14
  19. package/superstruct/dist/superstruct.d.ts +5 -0
  20. package/superstruct/dist/superstruct.js.map +1 -1
  21. package/superstruct/dist/superstruct.modern.mjs.map +1 -1
  22. package/superstruct/dist/superstruct.module.js.map +1 -1
  23. package/superstruct/dist/superstruct.umd.js.map +1 -1
  24. package/superstruct/src/__tests__/superstruct.ts +22 -8
  25. package/superstruct/src/superstruct.ts +13 -0
  26. package/typanion/src/__tests__/typanion.ts +8 -3
  27. package/typebox/src/__tests__/typebox.ts +31 -18
  28. package/typeschema/dist/typeschema.js +1 -1
  29. package/typeschema/dist/typeschema.js.map +1 -1
  30. package/typeschema/dist/typeschema.mjs +1 -1
  31. package/typeschema/dist/typeschema.modern.mjs +1 -1
  32. package/typeschema/dist/typeschema.modern.mjs.map +1 -1
  33. package/typeschema/dist/typeschema.module.js +1 -1
  34. package/typeschema/dist/typeschema.module.js.map +1 -1
  35. package/typeschema/dist/typeschema.umd.js +1 -1
  36. package/typeschema/dist/typeschema.umd.js.map +1 -1
  37. package/typeschema/src/__tests__/typeschema.ts +23 -15
  38. package/typeschema/src/typeschema.ts +6 -6
  39. package/valibot/src/__tests__/valibot.ts +15 -6
  40. package/vine/src/__tests__/vine.ts +15 -6
  41. package/yup/dist/yup.d.ts +2 -2
  42. package/yup/dist/yup.js.map +1 -1
  43. package/yup/dist/yup.modern.mjs.map +1 -1
  44. package/yup/dist/yup.module.js.map +1 -1
  45. package/yup/dist/yup.umd.js.map +1 -1
  46. package/yup/src/__tests__/yup.ts +15 -6
  47. package/yup/src/yup.ts +10 -2
  48. package/zod/dist/zod.d.ts +8 -2
  49. package/zod/dist/zod.js +1 -1
  50. package/zod/dist/zod.js.map +1 -1
  51. package/zod/dist/zod.mjs +1 -1
  52. package/zod/dist/zod.modern.mjs +1 -1
  53. package/zod/dist/zod.modern.mjs.map +1 -1
  54. package/zod/dist/zod.module.js +1 -1
  55. package/zod/dist/zod.module.js.map +1 -1
  56. package/zod/dist/zod.umd.js +1 -1
  57. package/zod/dist/zod.umd.js.map +1 -1
  58. package/zod/src/__tests__/__snapshots__/zod-v3.ts.snap +430 -0
  59. package/zod/src/__tests__/__snapshots__/zod-v4-mini.ts.snap +472 -0
  60. package/zod/src/__tests__/__snapshots__/zod-v4.ts.snap +434 -0
  61. package/zod/src/__tests__/zod-v3.ts +45 -6
  62. package/zod/src/__tests__/zod-v4-mini.ts +21 -11
  63. package/zod/src/__tests__/zod-v4.ts +47 -11
  64. package/zod/src/zod.ts +28 -5
package/zod/src/zod.ts CHANGED
@@ -40,7 +40,14 @@ function parseZod3Issues(
40
40
 
41
41
  if (!errors[_path]) {
42
42
  if ('unionErrors' in error) {
43
- const unionError = error.unionErrors[0].errors[0];
43
+ // Surface the error from whichever union member came closest to
44
+ // matching (fewest issues), rather than always the first member —
45
+ // the first member isn't necessarily the one the input was meant for.
46
+ const closestUnionError = error.unionErrors.reduce(
47
+ (closest, current) =>
48
+ current.errors.length < closest.errors.length ? current : closest,
49
+ );
50
+ const unionError = closestUnionError.errors[0];
44
51
 
45
52
  errors[_path] = {
46
53
  message: unionError.message,
@@ -91,7 +98,13 @@ function parseZod4Issues(
91
98
 
92
99
  if (!errors[_path]) {
93
100
  if (error.code === 'invalid_union' && error.errors.length > 0) {
94
- const unionError = error.errors[0][0];
101
+ // Surface the error from whichever union member came closest to
102
+ // matching (fewest issues), rather than always the first member —
103
+ // the first member isn't necessarily the one the input was meant for.
104
+ const closestBranch = error.errors.reduce((closest, branch) =>
105
+ branch.length < closest.length ? branch : closest,
106
+ );
107
+ const unionError = closestBranch[0];
95
108
 
96
109
  errors[_path] = {
97
110
  message: unionError.message,
@@ -149,6 +162,16 @@ interface Zod3Type<O = unknown, I = unknown> {
149
162
  };
150
163
  }
151
164
 
165
+ // minimal interface to avoid assignability issues between zod v4 patch/minor
166
+ // releases, which brand the full `$ZodType` shape with an exact version literal
167
+ // (e.g. `_zod.version.minor`)
168
+ interface Zod4Type<Output = unknown, Input = unknown> {
169
+ _zod: {
170
+ output: Output;
171
+ input: Input;
172
+ };
173
+ }
174
+
152
175
  // some type magic to make versions pre-3.25.0 still work
153
176
  type IsUnresolved<T> = PropertyKey extends keyof T ? true : false;
154
177
  type UnresolvedFallback<T, Fallback> = IsUnresolved<typeof z3> extends true
@@ -201,7 +224,7 @@ export function zodResolver<
201
224
  Input extends FieldValues,
202
225
  Context,
203
226
  Output,
204
- T extends z4.$ZodType<Output, Input> = z4.$ZodType<Output, Input>,
227
+ T extends Zod4Type<Output, Input> = Zod4Type<Output, Input>,
205
228
  >(
206
229
  schema: T,
207
230
  schemaOptions?: Zod4ParseParams, // already partial
@@ -211,9 +234,9 @@ export function zodResolver<
211
234
  Input extends FieldValues,
212
235
  Context,
213
236
  Output,
214
- T extends z4.$ZodType<Output, Input> = z4.$ZodType<Output, Input>,
237
+ T extends Zod4Type<Output, Input> = Zod4Type<Output, Input>,
215
238
  >(
216
- schema: z4.$ZodType<Output, Input>,
239
+ schema: Zod4Type<Output, Input>,
217
240
  schemaOptions: Zod4ParseParams | undefined, // already partial
218
241
  resolverOptions: RawResolverOptions,
219
242
  ): Resolver<z4.input<T>, Context, z4.input<T>>;