zod 4.3.1 → 4.3.2
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/package.json
CHANGED
|
@@ -27,17 +27,44 @@ test("object intersection: loose", () => {
|
|
|
27
27
|
expect(() => C.parse({ a: "foo" })).toThrow();
|
|
28
28
|
});
|
|
29
29
|
|
|
30
|
-
test("object intersection: strict", () => {
|
|
30
|
+
test("object intersection: strict + strip", () => {
|
|
31
31
|
const A = z.strictObject({ a: z.string() });
|
|
32
32
|
const B = z.object({ b: z.string() });
|
|
33
33
|
|
|
34
|
-
const C = z.intersection(A, B);
|
|
34
|
+
const C = z.intersection(A, B);
|
|
35
35
|
type C = z.infer<typeof C>;
|
|
36
36
|
expectTypeOf<C>().toEqualTypeOf<{ a: string } & { b: string }>();
|
|
37
|
-
const data = { a: "foo", b: "foo", c: "extra" };
|
|
38
37
|
|
|
39
|
-
|
|
40
|
-
expect(
|
|
38
|
+
// Keys recognized by either side should work
|
|
39
|
+
expect(C.parse({ a: "foo", b: "bar" })).toEqual({ a: "foo", b: "bar" });
|
|
40
|
+
|
|
41
|
+
// Extra keys are stripped (follows strip behavior from B)
|
|
42
|
+
expect(C.parse({ a: "foo", b: "bar", c: "extra" })).toEqual({ a: "foo", b: "bar" });
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("object intersection: strict + strict", () => {
|
|
46
|
+
const A = z.strictObject({ a: z.string() });
|
|
47
|
+
const B = z.strictObject({ b: z.string() });
|
|
48
|
+
|
|
49
|
+
const C = z.intersection(A, B);
|
|
50
|
+
|
|
51
|
+
// Keys recognized by either side should work
|
|
52
|
+
expect(C.parse({ a: "foo", b: "bar" })).toEqual({ a: "foo", b: "bar" });
|
|
53
|
+
|
|
54
|
+
// Keys unrecognized by BOTH sides should error
|
|
55
|
+
const result = C.safeParse({ a: "foo", b: "bar", c: "extra" });
|
|
56
|
+
expect(result.error?.issues).toMatchInlineSnapshot(`
|
|
57
|
+
[
|
|
58
|
+
{
|
|
59
|
+
"code": "unrecognized_keys",
|
|
60
|
+
"keys": [
|
|
61
|
+
"c",
|
|
62
|
+
],
|
|
63
|
+
"message": "Unrecognized key: "c"",
|
|
64
|
+
"path": [],
|
|
65
|
+
},
|
|
66
|
+
]
|
|
67
|
+
`);
|
|
41
68
|
});
|
|
42
69
|
|
|
43
70
|
test("deep intersection", () => {
|
package/src/v4/core/schemas.ts
CHANGED
|
@@ -2460,12 +2460,39 @@ function mergeValues(
|
|
|
2460
2460
|
}
|
|
2461
2461
|
|
|
2462
2462
|
function handleIntersectionResults(result: ParsePayload, left: ParsePayload, right: ParsePayload): ParsePayload {
|
|
2463
|
-
|
|
2464
|
-
|
|
2463
|
+
// Track which side(s) report each key as unrecognized
|
|
2464
|
+
const unrecKeys = new Map<string, { l?: true; r?: true }>();
|
|
2465
|
+
let unrecIssue: errors.$ZodRawIssue | undefined;
|
|
2466
|
+
|
|
2467
|
+
for (const iss of left.issues) {
|
|
2468
|
+
if (iss.code === "unrecognized_keys") {
|
|
2469
|
+
unrecIssue ??= iss;
|
|
2470
|
+
for (const k of iss.keys) {
|
|
2471
|
+
if (!unrecKeys.has(k)) unrecKeys.set(k, {});
|
|
2472
|
+
unrecKeys.get(k)!.l = true;
|
|
2473
|
+
}
|
|
2474
|
+
} else {
|
|
2475
|
+
result.issues.push(iss);
|
|
2476
|
+
}
|
|
2465
2477
|
}
|
|
2466
|
-
|
|
2467
|
-
|
|
2478
|
+
|
|
2479
|
+
for (const iss of right.issues) {
|
|
2480
|
+
if (iss.code === "unrecognized_keys") {
|
|
2481
|
+
for (const k of iss.keys) {
|
|
2482
|
+
if (!unrecKeys.has(k)) unrecKeys.set(k, {});
|
|
2483
|
+
unrecKeys.get(k)!.r = true;
|
|
2484
|
+
}
|
|
2485
|
+
} else {
|
|
2486
|
+
result.issues.push(iss);
|
|
2487
|
+
}
|
|
2468
2488
|
}
|
|
2489
|
+
|
|
2490
|
+
// Report only keys unrecognized by BOTH sides
|
|
2491
|
+
const bothKeys = [...unrecKeys].filter(([, f]) => f.l && f.r).map(([k]) => k);
|
|
2492
|
+
if (bothKeys.length && unrecIssue) {
|
|
2493
|
+
result.issues.push({ ...unrecIssue, keys: bothKeys });
|
|
2494
|
+
}
|
|
2495
|
+
|
|
2469
2496
|
if (util.aborted(result)) return result;
|
|
2470
2497
|
|
|
2471
2498
|
const merged = mergeValues(left.value, right.value);
|
package/src/v4/core/versions.ts
CHANGED
package/v4/core/schemas.cjs
CHANGED
|
@@ -1214,11 +1214,38 @@ function mergeValues(a, b) {
|
|
|
1214
1214
|
return { valid: false, mergeErrorPath: [] };
|
|
1215
1215
|
}
|
|
1216
1216
|
function handleIntersectionResults(result, left, right) {
|
|
1217
|
-
|
|
1218
|
-
|
|
1217
|
+
// Track which side(s) report each key as unrecognized
|
|
1218
|
+
const unrecKeys = new Map();
|
|
1219
|
+
let unrecIssue;
|
|
1220
|
+
for (const iss of left.issues) {
|
|
1221
|
+
if (iss.code === "unrecognized_keys") {
|
|
1222
|
+
unrecIssue ?? (unrecIssue = iss);
|
|
1223
|
+
for (const k of iss.keys) {
|
|
1224
|
+
if (!unrecKeys.has(k))
|
|
1225
|
+
unrecKeys.set(k, {});
|
|
1226
|
+
unrecKeys.get(k).l = true;
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
else {
|
|
1230
|
+
result.issues.push(iss);
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
for (const iss of right.issues) {
|
|
1234
|
+
if (iss.code === "unrecognized_keys") {
|
|
1235
|
+
for (const k of iss.keys) {
|
|
1236
|
+
if (!unrecKeys.has(k))
|
|
1237
|
+
unrecKeys.set(k, {});
|
|
1238
|
+
unrecKeys.get(k).r = true;
|
|
1239
|
+
}
|
|
1240
|
+
}
|
|
1241
|
+
else {
|
|
1242
|
+
result.issues.push(iss);
|
|
1243
|
+
}
|
|
1219
1244
|
}
|
|
1220
|
-
|
|
1221
|
-
|
|
1245
|
+
// Report only keys unrecognized by BOTH sides
|
|
1246
|
+
const bothKeys = [...unrecKeys].filter(([, f]) => f.l && f.r).map(([k]) => k);
|
|
1247
|
+
if (bothKeys.length && unrecIssue) {
|
|
1248
|
+
result.issues.push({ ...unrecIssue, keys: bothKeys });
|
|
1222
1249
|
}
|
|
1223
1250
|
if (util.aborted(result))
|
|
1224
1251
|
return result;
|
package/v4/core/schemas.js
CHANGED
|
@@ -1183,11 +1183,38 @@ function mergeValues(a, b) {
|
|
|
1183
1183
|
return { valid: false, mergeErrorPath: [] };
|
|
1184
1184
|
}
|
|
1185
1185
|
function handleIntersectionResults(result, left, right) {
|
|
1186
|
-
|
|
1187
|
-
|
|
1186
|
+
// Track which side(s) report each key as unrecognized
|
|
1187
|
+
const unrecKeys = new Map();
|
|
1188
|
+
let unrecIssue;
|
|
1189
|
+
for (const iss of left.issues) {
|
|
1190
|
+
if (iss.code === "unrecognized_keys") {
|
|
1191
|
+
unrecIssue ?? (unrecIssue = iss);
|
|
1192
|
+
for (const k of iss.keys) {
|
|
1193
|
+
if (!unrecKeys.has(k))
|
|
1194
|
+
unrecKeys.set(k, {});
|
|
1195
|
+
unrecKeys.get(k).l = true;
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
else {
|
|
1199
|
+
result.issues.push(iss);
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
for (const iss of right.issues) {
|
|
1203
|
+
if (iss.code === "unrecognized_keys") {
|
|
1204
|
+
for (const k of iss.keys) {
|
|
1205
|
+
if (!unrecKeys.has(k))
|
|
1206
|
+
unrecKeys.set(k, {});
|
|
1207
|
+
unrecKeys.get(k).r = true;
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
else {
|
|
1211
|
+
result.issues.push(iss);
|
|
1212
|
+
}
|
|
1188
1213
|
}
|
|
1189
|
-
|
|
1190
|
-
|
|
1214
|
+
// Report only keys unrecognized by BOTH sides
|
|
1215
|
+
const bothKeys = [...unrecKeys].filter(([, f]) => f.l && f.r).map(([k]) => k);
|
|
1216
|
+
if (bothKeys.length && unrecIssue) {
|
|
1217
|
+
result.issues.push({ ...unrecIssue, keys: bothKeys });
|
|
1191
1218
|
}
|
|
1192
1219
|
if (util.aborted(result))
|
|
1193
1220
|
return result;
|
package/v4/core/versions.cjs
CHANGED
package/v4/core/versions.js
CHANGED