Repository navigation
Commit 4e1720c
authored
fix(v4): align record keys and intersection strictness with TypeScript (#6412)
* fix(record): classify an out-of-set key by whether the key schema is enumerable
An enumerable key schema (z.enum, z.literal([...])) declares which keys the
record owns, so a key outside that set is unrecognized. A non-enumerable one
(regex, refine) is a constraint every key must satisfy, so a failing key is
invalid. Only the first is reconcilable against the other side of an
intersection, which handleIntersectionResults already does for
unrecognized_keys.
The branch that emitted the issue keyed off def.partial rather than the key
schema, so partialRecord(z.enum([...])) reported invalid_key and its keys were
never reconciled -- the object side of an intersection could not claim its own
keys. Fixes #2200, #2573.
* fix(record): a record's key schema governs its own keys, not every key
A record's key schema declares which keys that record is responsible for. It
is not a predicate every key in the result must satisfy. TypeScript agrees:
in `{name: string} & Record<\`S_\${string}\`, string>` the index signature
constrains only the keys matching it, so `name` is accepted, while a matching
key with the wrong value type is still an error.
Zod's inferred types already match TypeScript here, but the runtime rejected
the object's own keys with invalid_key, so z.infer accepted values the parser
threw on. Two changes:
- partialRecord with an enumerable key schema now reports an out-of-set key as
unrecognized_keys, like z.record(z.enum([...])) already did. The branch keyed
off def.partial rather than off the key schema.
- handleIntersectionResults reconciles a record's invalid_key the same way it
already reconciles unrecognized_keys, so a key one side does not govern is
reported only when neither side governs it.
Standalone records are unchanged: a key outside the key schema is still an
error, matching TypeScript's excess-property check on a fresh literal.
Fixes #2200, #2573.
* fix(v4): let an unrecognized key continue, so operands run their own logic
unrecognized_keys describes the shape of the input, not the validity of the
parsed value, so it no longer aborts the schema it came from. A strict operand
inside an intersection now runs its own checks, refinements and transforms, a
nested default still applies, and an extra key no longer suppresses the other
errors on the same object. The parse still fails.
A pipe keeps its rule that any issue stops it, so a failing refinement never
feeds its transform -- the one exception is unrecognized_keys, where the value
handed downstream is identical whether or not the extra key was present.1 parent 4cc4053 commit 4e1720c
3 files changed
Lines changed: 182 additions & 38 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
214 | 214 | | |
215 | 215 | | |
216 | 216 | | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
561 | 561 | | |
562 | 562 | | |
563 | 563 | | |
564 | | - | |
565 | | - | |
566 | | - | |
567 | | - | |
568 | | - | |
569 | | - | |
570 | | - | |
571 | | - | |
572 | | - | |
573 | | - | |
574 | | - | |
575 | | - | |
576 | | - | |
577 | | - | |
578 | | - | |
| 564 | + | |
| 565 | + | |
579 | 566 | | |
580 | 567 | | |
581 | | - | |
| 568 | + | |
| 569 | + | |
582 | 570 | | |
583 | 571 | | |
584 | 572 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1957 | 1957 | | |
1958 | 1958 | | |
1959 | 1959 | | |
| 1960 | + | |
| 1961 | + | |
| 1962 | + | |
| 1963 | + | |
1960 | 1964 | | |
1961 | 1965 | | |
1962 | 1966 | | |
| |||
2624 | 2628 | | |
2625 | 2629 | | |
2626 | 2630 | | |
2627 | | - | |
| 2631 | + | |
| 2632 | + | |
| 2633 | + | |
| 2634 | + | |
2628 | 2635 | | |
2629 | 2636 | | |
| 2637 | + | |
2630 | 2638 | | |
2631 | | - | |
2632 | | - | |
| 2639 | + | |
| 2640 | + | |
| 2641 | + | |
2633 | 2642 | | |
2634 | | - | |
2635 | | - | |
2636 | | - | |
2637 | | - | |
| 2643 | + | |
| 2644 | + | |
| 2645 | + | |
| 2646 | + | |
| 2647 | + | |
2638 | 2648 | | |
2639 | | - | |
| 2649 | + | |
2640 | 2650 | | |
| 2651 | + | |
| 2652 | + | |
| 2653 | + | |
| 2654 | + | |
| 2655 | + | |
| 2656 | + | |
| 2657 | + | |
| 2658 | + | |
| 2659 | + | |
2641 | 2660 | | |
2642 | 2661 | | |
2643 | 2662 | | |
2644 | | - | |
2645 | | - | |
2646 | | - | |
2647 | | - | |
2648 | | - | |
2649 | | - | |
2650 | | - | |
2651 | | - | |
| 2663 | + | |
2652 | 2664 | | |
2653 | 2665 | | |
2654 | | - | |
| 2666 | + | |
2655 | 2667 | | |
2656 | | - | |
2657 | | - | |
| 2668 | + | |
| 2669 | + | |
| 2670 | + | |
| 2671 | + | |
| 2672 | + | |
| 2673 | + | |
2658 | 2674 | | |
2659 | 2675 | | |
2660 | | - | |
2661 | | - | |
2662 | 2676 | | |
2663 | 2677 | | |
2664 | 2678 | | |
| 2679 | + | |
2665 | 2680 | | |
2666 | 2681 | | |
2667 | 2682 | | |
| |||
3032 | 3047 | | |
3033 | 3048 | | |
3034 | 3049 | | |
| 3050 | + | |
3035 | 3051 | | |
3036 | 3052 | | |
3037 | 3053 | | |
3038 | 3054 | | |
| 3055 | + | |
| 3056 | + | |
| 3057 | + | |
| 3058 | + | |
| 3059 | + | |
3039 | 3060 | | |
3040 | 3061 | | |
3041 | 3062 | | |
| |||
3062 | 3083 | | |
3063 | 3084 | | |
3064 | 3085 | | |
| 3086 | + | |
| 3087 | + | |
| 3088 | + | |
3065 | 3089 | | |
3066 | 3090 | | |
3067 | 3091 | | |
| |||
3099 | 3123 | | |
3100 | 3124 | | |
3101 | 3125 | | |
| 3126 | + | |
| 3127 | + | |
| 3128 | + | |
| 3129 | + | |
| 3130 | + | |
| 3131 | + | |
| 3132 | + | |
| 3133 | + | |
| 3134 | + | |
| 3135 | + | |
| 3136 | + | |
3102 | 3137 | | |
3103 | 3138 | | |
3104 | 3139 | | |
| |||
4125 | 4160 | | |
4126 | 4161 | | |
4127 | 4162 | | |
4128 | | - | |
| 4163 | + | |
| 4164 | + | |
| 4165 | + | |
| 4166 | + | |
4129 | 4167 | | |
4130 | 4168 | | |
4131 | 4169 | | |
| |||
0 commit comments