zod 4.3.5 → 4.3.6
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/mini/index.cjs +16 -1
- package/mini/index.d.cts +3 -1
- package/mini/index.d.ts +3 -1
- package/mini/index.js +3 -1
- package/package.json +1 -1
- package/src/mini/index.ts +3 -1
- package/src/v4/classic/tests/codec.test.ts +1 -1
- package/src/v4/classic/tests/record.test.ts +32 -0
- package/src/v4/classic/tests/to-json-schema.test.ts +15 -0
- package/src/v4/core/schemas.ts +3 -8
- package/src/v4/core/to-json-schema.ts +2 -2
- package/src/v4/core/versions.ts +1 -1
- package/src/v4-mini/index.ts +3 -1
- package/v4/core/schemas.cjs +3 -5
- package/v4/core/schemas.js +3 -5
- package/v4/core/to-json-schema.cjs +1 -1
- package/v4/core/to-json-schema.js +1 -1
- package/v4/core/versions.cjs +1 -1
- package/v4/core/versions.js +1 -1
- package/v4-mini/index.cjs +16 -1
- package/v4-mini/index.d.cts +3 -1
- package/v4-mini/index.d.ts +3 -1
- package/v4-mini/index.js +3 -1
package/mini/index.cjs
CHANGED
|
@@ -10,8 +10,23 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
10
10
|
if (k2 === undefined) k2 = k;
|
|
11
11
|
o[k2] = m[k];
|
|
12
12
|
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
13
25
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
26
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
27
|
};
|
|
16
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
|
|
29
|
+
exports.z = void 0;
|
|
30
|
+
const z = __importStar(require("../v4/mini/external.cjs"));
|
|
31
|
+
exports.z = z;
|
|
32
|
+
__exportStar(require("../v4/mini/external.cjs"), exports);
|
package/mini/index.d.cts
CHANGED
package/mini/index.d.ts
CHANGED
package/mini/index.js
CHANGED
package/package.json
CHANGED
package/src/mini/index.ts
CHANGED
|
@@ -503,7 +503,7 @@ test("codec type enforcement - complex types", () => {
|
|
|
503
503
|
);
|
|
504
504
|
});
|
|
505
505
|
|
|
506
|
-
test("
|
|
506
|
+
test("codec with overwrites", () => {
|
|
507
507
|
const stringPlusA = z.string().overwrite((val) => val + "a");
|
|
508
508
|
const A = z
|
|
509
509
|
.codec(stringPlusA, stringPlusA, {
|
|
@@ -487,6 +487,38 @@ test("partialRecord with z.literal([key, ...])", () => {
|
|
|
487
487
|
`);
|
|
488
488
|
});
|
|
489
489
|
|
|
490
|
+
test("partialRecord with numeric literal keys", () => {
|
|
491
|
+
const Keys = z.literal([1, 2, 3]);
|
|
492
|
+
const schema = z.partialRecord(Keys, z.string());
|
|
493
|
+
type Schema = z.infer<typeof schema>;
|
|
494
|
+
expectTypeOf<Schema>().toEqualTypeOf<Partial<Record<1 | 2 | 3, string>>>();
|
|
495
|
+
|
|
496
|
+
// Should parse valid partials with numeric keys (as strings in JS objects)
|
|
497
|
+
expect(schema.parse({})).toEqual({});
|
|
498
|
+
expect(schema.parse({ 1: "one" })).toEqual({ 1: "one" });
|
|
499
|
+
expect(schema.parse({ 2: "two", 3: "three" })).toEqual({ 2: "two", 3: "three" });
|
|
500
|
+
|
|
501
|
+
// Should fail with unrecognized key
|
|
502
|
+
expect(schema.safeParse({ 4: "four" }).success).toBe(false);
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
test("partialRecord with union of string and numeric literal keys", () => {
|
|
506
|
+
const StringKeys = z.literal(["a", "b", "c"]);
|
|
507
|
+
const NumericKeys = z.literal([1, 2, 3]);
|
|
508
|
+
const schema = z.partialRecord(z.union([StringKeys, NumericKeys]), z.string());
|
|
509
|
+
type Schema = z.infer<typeof schema>;
|
|
510
|
+
expectTypeOf<Schema>().toEqualTypeOf<Partial<Record<"a" | "b" | "c" | 1 | 2 | 3, string>>>();
|
|
511
|
+
|
|
512
|
+
// Should parse valid partials with mixed keys
|
|
513
|
+
expect(schema.parse({})).toEqual({});
|
|
514
|
+
expect(schema.parse({ a: "1", 2: "4" })).toEqual({ a: "1", 2: "4" });
|
|
515
|
+
expect(schema.parse({ a: "a", b: "b", 1: "1", 2: "2" })).toEqual({ a: "a", b: "b", 1: "1", 2: "2" });
|
|
516
|
+
|
|
517
|
+
// Should fail with unrecognized key
|
|
518
|
+
expect(schema.safeParse({ d: "d" }).success).toBe(false);
|
|
519
|
+
expect(schema.safeParse({ 4: "4" }).success).toBe(false);
|
|
520
|
+
});
|
|
521
|
+
|
|
490
522
|
test("looseRecord passes through non-matching keys", () => {
|
|
491
523
|
const schema = z.looseRecord(z.string().regex(/^S_/), z.string());
|
|
492
524
|
|
|
@@ -2491,6 +2491,21 @@ test("_ref", () => {
|
|
|
2491
2491
|
"type": "string",
|
|
2492
2492
|
}
|
|
2493
2493
|
`);
|
|
2494
|
+
|
|
2495
|
+
const d = z.toJSONSchema(z.string().meta({ id: "foo" }).describe("bar").optional());
|
|
2496
|
+
expect(d).toMatchInlineSnapshot(`
|
|
2497
|
+
{
|
|
2498
|
+
"$defs": {
|
|
2499
|
+
"foo": {
|
|
2500
|
+
"id": "foo",
|
|
2501
|
+
"type": "string",
|
|
2502
|
+
},
|
|
2503
|
+
},
|
|
2504
|
+
"$ref": "#/$defs/foo",
|
|
2505
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
2506
|
+
"description": "bar",
|
|
2507
|
+
}
|
|
2508
|
+
`);
|
|
2494
2509
|
});
|
|
2495
2510
|
|
|
2496
2511
|
test("defaults/prefaults", () => {
|
package/src/v4/core/schemas.ts
CHANGED
|
@@ -2804,14 +2804,9 @@ export const $ZodRecord: core.$constructor<$ZodRecord> = /*@__PURE__*/ core.$con
|
|
|
2804
2804
|
throw new Error("Async schemas not supported in object keys currently");
|
|
2805
2805
|
}
|
|
2806
2806
|
|
|
2807
|
-
// Numeric string fallback: if key
|
|
2808
|
-
|
|
2809
|
-
|
|
2810
|
-
regexes.number.test(key) &&
|
|
2811
|
-
keyResult.issues.length &&
|
|
2812
|
-
keyResult.issues.some(
|
|
2813
|
-
(iss) => iss.code === "invalid_type" && (iss as errors.$ZodIssueInvalidType).expected === "number"
|
|
2814
|
-
);
|
|
2807
|
+
// Numeric string fallback: if key is a numeric string and failed, retry with Number(key)
|
|
2808
|
+
// This handles z.number(), z.literal([1, 2, 3]), and unions containing numeric literals
|
|
2809
|
+
const checkNumericKey = typeof key === "string" && regexes.number.test(key) && keyResult.issues.length;
|
|
2815
2810
|
if (checkNumericKey) {
|
|
2816
2811
|
const retryResult = def.keyType._zod.run({ value: Number(key), issues: [] }, ctx);
|
|
2817
2812
|
if (retryResult instanceof Promise) {
|
|
@@ -406,10 +406,10 @@ export function finalize<T extends schemas.$ZodType>(
|
|
|
406
406
|
}
|
|
407
407
|
|
|
408
408
|
// When ref was extracted to $defs, remove properties that match the definition
|
|
409
|
-
if (refSchema.$ref) {
|
|
409
|
+
if (refSchema.$ref && refSeen.def) {
|
|
410
410
|
for (const key in schema) {
|
|
411
411
|
if (key === "$ref" || key === "allOf") continue;
|
|
412
|
-
if (key in refSeen.def
|
|
412
|
+
if (key in refSeen.def && JSON.stringify(schema[key]) === JSON.stringify(refSeen.def[key])) {
|
|
413
413
|
delete schema[key];
|
|
414
414
|
}
|
|
415
415
|
}
|
package/src/v4/core/versions.ts
CHANGED
package/src/v4-mini/index.ts
CHANGED
package/v4/core/schemas.cjs
CHANGED
|
@@ -1396,11 +1396,9 @@ exports.$ZodRecord = core.$constructor("$ZodRecord", (inst, def) => {
|
|
|
1396
1396
|
if (keyResult instanceof Promise) {
|
|
1397
1397
|
throw new Error("Async schemas not supported in object keys currently");
|
|
1398
1398
|
}
|
|
1399
|
-
// Numeric string fallback: if key
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
keyResult.issues.length &&
|
|
1403
|
-
keyResult.issues.some((iss) => iss.code === "invalid_type" && iss.expected === "number");
|
|
1399
|
+
// Numeric string fallback: if key is a numeric string and failed, retry with Number(key)
|
|
1400
|
+
// This handles z.number(), z.literal([1, 2, 3]), and unions containing numeric literals
|
|
1401
|
+
const checkNumericKey = typeof key === "string" && regexes.number.test(key) && keyResult.issues.length;
|
|
1404
1402
|
if (checkNumericKey) {
|
|
1405
1403
|
const retryResult = def.keyType._zod.run({ value: Number(key), issues: [] }, ctx);
|
|
1406
1404
|
if (retryResult instanceof Promise) {
|
package/v4/core/schemas.js
CHANGED
|
@@ -1365,11 +1365,9 @@ export const $ZodRecord = /*@__PURE__*/ core.$constructor("$ZodRecord", (inst, d
|
|
|
1365
1365
|
if (keyResult instanceof Promise) {
|
|
1366
1366
|
throw new Error("Async schemas not supported in object keys currently");
|
|
1367
1367
|
}
|
|
1368
|
-
// Numeric string fallback: if key
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
keyResult.issues.length &&
|
|
1372
|
-
keyResult.issues.some((iss) => iss.code === "invalid_type" && iss.expected === "number");
|
|
1368
|
+
// Numeric string fallback: if key is a numeric string and failed, retry with Number(key)
|
|
1369
|
+
// This handles z.number(), z.literal([1, 2, 3]), and unions containing numeric literals
|
|
1370
|
+
const checkNumericKey = typeof key === "string" && regexes.number.test(key) && keyResult.issues.length;
|
|
1373
1371
|
if (checkNumericKey) {
|
|
1374
1372
|
const retryResult = def.keyType._zod.run({ value: Number(key), issues: [] }, ctx);
|
|
1375
1373
|
if (retryResult instanceof Promise) {
|
|
@@ -260,7 +260,7 @@ function finalize(ctx, schema) {
|
|
|
260
260
|
}
|
|
261
261
|
}
|
|
262
262
|
// When ref was extracted to $defs, remove properties that match the definition
|
|
263
|
-
if (refSchema.$ref) {
|
|
263
|
+
if (refSchema.$ref && refSeen.def) {
|
|
264
264
|
for (const key in schema) {
|
|
265
265
|
if (key === "$ref" || key === "allOf")
|
|
266
266
|
continue;
|
|
@@ -253,7 +253,7 @@ export function finalize(ctx, schema) {
|
|
|
253
253
|
}
|
|
254
254
|
}
|
|
255
255
|
// When ref was extracted to $defs, remove properties that match the definition
|
|
256
|
-
if (refSchema.$ref) {
|
|
256
|
+
if (refSchema.$ref && refSeen.def) {
|
|
257
257
|
for (const key in schema) {
|
|
258
258
|
if (key === "$ref" || key === "allOf")
|
|
259
259
|
continue;
|
package/v4/core/versions.cjs
CHANGED
package/v4/core/versions.js
CHANGED
package/v4-mini/index.cjs
CHANGED
|
@@ -10,8 +10,23 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
10
10
|
if (k2 === undefined) k2 = k;
|
|
11
11
|
o[k2] = m[k];
|
|
12
12
|
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
13
25
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
26
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
27
|
};
|
|
16
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
|
|
29
|
+
exports.z = void 0;
|
|
30
|
+
const z = __importStar(require("../v4/mini/external.cjs"));
|
|
31
|
+
exports.z = z;
|
|
32
|
+
__exportStar(require("../v4/mini/external.cjs"), exports);
|
package/v4-mini/index.d.cts
CHANGED
package/v4-mini/index.d.ts
CHANGED
package/v4-mini/index.js
CHANGED