zod 3.26.0-canary.20250703T214020 → 3.26.0-canary.20250707T201657
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/index.cjs +2 -2
- package/index.d.cts +2 -2
- package/index.d.ts +2 -2
- package/index.js +2 -2
- package/package.json +1 -1
- package/src/index.ts +2 -2
- package/src/v4/classic/schemas.ts +4 -2
- package/src/v4/classic/tests/brand.test.ts +1 -3
- package/src/v4/classic/tests/discriminated-unions.test.ts +27 -0
- package/src/v4/classic/tests/index.test.ts +1 -1
- package/src/v4/classic/tests/object.test.ts +11 -1
- package/src/v4/classic/tests/optional.test.ts +20 -0
- package/src/v4/classic/tests/record.test.ts +11 -1
- package/src/v4/classic/tests/recursive-types.test.ts +3 -2
- package/src/v4/classic/tests/to-json-schema.test.ts +95 -2
- package/src/v4/core/registries.ts +4 -0
- package/src/v4/core/schemas.ts +93 -32
- package/src/v4/core/to-json-schema.ts +23 -11
- package/src/v4/mini/schemas.ts +2 -2
- package/src/v4/mini/tests/string.test.ts +6 -0
- package/v4/classic/schemas.d.cts +2 -1
- package/v4/classic/schemas.d.ts +2 -1
- package/v4/core/registries.cjs +4 -0
- package/v4/core/registries.js +4 -0
- package/v4/core/schemas.cjs +18 -5
- package/v4/core/schemas.d.cts +35 -9
- package/v4/core/schemas.d.ts +35 -9
- package/v4/core/schemas.js +18 -5
- package/v4/core/to-json-schema.cjs +10 -8
- package/v4/core/to-json-schema.d.cts +5 -1
- package/v4/core/to-json-schema.d.ts +5 -1
- package/v4/core/to-json-schema.js +10 -8
- package/v4/mini/schemas.d.cts +1 -1
- package/v4/mini/schemas.d.ts +1 -1
package/src/v4/core/schemas.ts
CHANGED
|
@@ -90,7 +90,7 @@ export interface _$ZodTypeInternals {
|
|
|
90
90
|
// types: Types;
|
|
91
91
|
|
|
92
92
|
/** @internal Randomly generated ID for this schema. */
|
|
93
|
-
id: string;
|
|
93
|
+
// id: string;
|
|
94
94
|
|
|
95
95
|
/** @internal List of deferred initializers. */
|
|
96
96
|
deferred: util.AnyFunc[] | undefined;
|
|
@@ -171,8 +171,7 @@ export interface _$ZodType<T extends $ZodTypeInternals = $ZodTypeInternals>
|
|
|
171
171
|
|
|
172
172
|
export const $ZodType: core.$constructor<$ZodType> = /*@__PURE__*/ core.$constructor("$ZodType", (inst, def) => {
|
|
173
173
|
inst ??= {} as any;
|
|
174
|
-
|
|
175
|
-
util.defineLazy(inst._zod, "id", () => def.type + "_" + util.randomString(10));
|
|
174
|
+
|
|
176
175
|
inst._zod.def = def; // set _def property
|
|
177
176
|
inst._zod.bag = inst._zod.bag || {}; // initialize _bag object
|
|
178
177
|
inst._zod.version = version;
|
|
@@ -428,7 +427,9 @@ export const $ZodURL: core.$constructor<$ZodURL> = /*@__PURE__*/ core.$construct
|
|
|
428
427
|
$ZodStringFormat.init(inst, def);
|
|
429
428
|
inst._zod.check = (payload) => {
|
|
430
429
|
try {
|
|
431
|
-
const
|
|
430
|
+
const orig = payload.value;
|
|
431
|
+
const url = new URL(orig);
|
|
432
|
+
const href = url.href;
|
|
432
433
|
|
|
433
434
|
if (def.hostname) {
|
|
434
435
|
def.hostname.lastIndex = 0;
|
|
@@ -460,6 +461,13 @@ export const $ZodURL: core.$constructor<$ZodURL> = /*@__PURE__*/ core.$construct
|
|
|
460
461
|
}
|
|
461
462
|
}
|
|
462
463
|
|
|
464
|
+
// payload.value = url.href;
|
|
465
|
+
if (!orig.endsWith("/") && href.endsWith("/")) {
|
|
466
|
+
payload.value = href.slice(0, -1);
|
|
467
|
+
} else {
|
|
468
|
+
payload.value = href;
|
|
469
|
+
}
|
|
470
|
+
|
|
463
471
|
return;
|
|
464
472
|
} catch (_) {
|
|
465
473
|
payload.issues.push({
|
|
@@ -1238,6 +1246,8 @@ export const $ZodUndefined: core.$constructor<$ZodUndefined> = /*@__PURE__*/ cor
|
|
|
1238
1246
|
$ZodType.init(inst, def);
|
|
1239
1247
|
inst._zod.pattern = regexes.undefined;
|
|
1240
1248
|
inst._zod.values = new Set([undefined]);
|
|
1249
|
+
inst._zod.optin = "optional";
|
|
1250
|
+
inst._zod.optout = "optional";
|
|
1241
1251
|
|
|
1242
1252
|
inst._zod.parse = (payload, _ctx) => {
|
|
1243
1253
|
const input = payload.value;
|
|
@@ -1374,7 +1384,6 @@ export interface $ZodNever extends $ZodType {
|
|
|
1374
1384
|
|
|
1375
1385
|
export const $ZodNever: core.$constructor<$ZodNever> = /*@__PURE__*/ core.$constructor("$ZodNever", (inst, def) => {
|
|
1376
1386
|
$ZodType.init(inst, def);
|
|
1377
|
-
|
|
1378
1387
|
inst._zod.parse = (payload, _ctx) => {
|
|
1379
1388
|
payload.issues.push({
|
|
1380
1389
|
expected: "never",
|
|
@@ -1488,14 +1497,15 @@ export interface $ZodArrayDef<T extends SomeType = $ZodType> extends $ZodTypeDef
|
|
|
1488
1497
|
element: T;
|
|
1489
1498
|
}
|
|
1490
1499
|
|
|
1491
|
-
export interface $ZodArrayInternals<T extends SomeType = $ZodType>
|
|
1492
|
-
|
|
1500
|
+
export interface $ZodArrayInternals<T extends SomeType = $ZodType> extends _$ZodTypeInternals {
|
|
1501
|
+
//$ZodTypeInternals<core.output<T>[], core.input<T>[]> {
|
|
1493
1502
|
def: $ZodArrayDef<T>;
|
|
1494
1503
|
isst: errors.$ZodIssueInvalidType;
|
|
1504
|
+
output: core.output<T>[];
|
|
1505
|
+
input: core.input<T>[];
|
|
1495
1506
|
}
|
|
1496
1507
|
|
|
1497
|
-
export interface $ZodArray<T extends SomeType = $ZodType>
|
|
1498
|
-
extends $ZodType<core.output<T>[], core.input<T>[], $ZodArrayInternals<T>> {}
|
|
1508
|
+
export interface $ZodArray<T extends SomeType = $ZodType> extends $ZodType<any, any, $ZodArrayInternals<T>> {}
|
|
1499
1509
|
|
|
1500
1510
|
function handleArrayResult(result: ParsePayload<any>, final: ParsePayload<any[]>, index: number) {
|
|
1501
1511
|
if (result.issues.length) {
|
|
@@ -1559,7 +1569,9 @@ type OptionalOutSchema = { _zod: { optout: "optional" } };
|
|
|
1559
1569
|
type OptionalInSchema = { _zod: { optin: "optional" } };
|
|
1560
1570
|
|
|
1561
1571
|
export type $InferObjectOutput<T extends $ZodLooseShape, Extra extends Record<string, unknown>> = string extends keyof T
|
|
1562
|
-
?
|
|
1572
|
+
? util.IsAny<T[keyof T]> extends true
|
|
1573
|
+
? Record<string, unknown>
|
|
1574
|
+
: Record<string, core.output<T[keyof T]>>
|
|
1563
1575
|
: keyof (T & Extra) extends never
|
|
1564
1576
|
? Record<string, never>
|
|
1565
1577
|
: util.Prettify<
|
|
@@ -1571,7 +1583,9 @@ export type $InferObjectOutput<T extends $ZodLooseShape, Extra extends Record<st
|
|
|
1571
1583
|
>;
|
|
1572
1584
|
|
|
1573
1585
|
export type $InferObjectInput<T extends $ZodLooseShape, Extra extends Record<string, unknown>> = string extends keyof T
|
|
1574
|
-
?
|
|
1586
|
+
? util.IsAny<T[keyof T]> extends true
|
|
1587
|
+
? Record<string, unknown>
|
|
1588
|
+
: Record<string, core.input<T[keyof T]>>
|
|
1575
1589
|
: keyof (T & Extra) extends never
|
|
1576
1590
|
? Record<string, never>
|
|
1577
1591
|
: util.Prettify<
|
|
@@ -1710,8 +1724,9 @@ export const $ZodObject: core.$constructor<$ZodObject> = /*@__PURE__*/ core.$con
|
|
|
1710
1724
|
doc.write(`const input = payload.value;`);
|
|
1711
1725
|
|
|
1712
1726
|
const ids: any = Object.create(null);
|
|
1727
|
+
let counter = 0;
|
|
1713
1728
|
for (const key of normalized.keys) {
|
|
1714
|
-
ids[key] =
|
|
1729
|
+
ids[key] = `key_${counter++}`;
|
|
1715
1730
|
}
|
|
1716
1731
|
|
|
1717
1732
|
// A: preserve key order {
|
|
@@ -1882,14 +1897,23 @@ export interface $ZodUnionDef<Options extends readonly SomeType[] = readonly $Zo
|
|
|
1882
1897
|
options: Options;
|
|
1883
1898
|
}
|
|
1884
1899
|
|
|
1885
|
-
|
|
1886
|
-
|
|
1900
|
+
type IsOptionalIn<T extends SomeType> = T extends OptionalInSchema ? true : false;
|
|
1901
|
+
type IsOptionalOut<T extends SomeType> = T extends OptionalOutSchema ? true : false;
|
|
1902
|
+
|
|
1903
|
+
export interface $ZodUnionInternals<T extends readonly SomeType[] = readonly $ZodType[]> extends _$ZodTypeInternals {
|
|
1887
1904
|
def: $ZodUnionDef<T>;
|
|
1888
1905
|
isst: errors.$ZodIssueInvalidUnion;
|
|
1889
1906
|
pattern: T[number]["_zod"]["pattern"];
|
|
1907
|
+
values: T[number]["_zod"]["values"]; //GetValues<T[number]>;
|
|
1908
|
+
output: $InferUnionOutput<T[number]>;
|
|
1909
|
+
input: $InferUnionInput<T[number]>;
|
|
1910
|
+
// if any element in the union is optional, then the union is optional
|
|
1911
|
+
optin: IsOptionalIn<T[number]> extends false ? "optional" | undefined : "optional";
|
|
1912
|
+
optout: IsOptionalOut<T[number]> extends false ? "optional" | undefined : "optional";
|
|
1890
1913
|
}
|
|
1891
1914
|
|
|
1892
|
-
export interface $ZodUnion<T extends readonly SomeType[] = readonly $ZodType[]>
|
|
1915
|
+
export interface $ZodUnion<T extends readonly SomeType[] = readonly $ZodType[]>
|
|
1916
|
+
extends $ZodType<any, any, $ZodUnionInternals<T>> {
|
|
1893
1917
|
_zod: $ZodUnionInternals<T>;
|
|
1894
1918
|
}
|
|
1895
1919
|
|
|
@@ -1914,6 +1938,14 @@ function handleUnionResults(results: ParsePayload[], final: ParsePayload, inst:
|
|
|
1914
1938
|
export const $ZodUnion: core.$constructor<$ZodUnion> = /*@__PURE__*/ core.$constructor("$ZodUnion", (inst, def) => {
|
|
1915
1939
|
$ZodType.init(inst, def);
|
|
1916
1940
|
|
|
1941
|
+
util.defineLazy(inst._zod, "optin", () =>
|
|
1942
|
+
def.options.some((o) => o._zod.optin === "optional") ? "optional" : undefined
|
|
1943
|
+
);
|
|
1944
|
+
|
|
1945
|
+
util.defineLazy(inst._zod, "optout", () =>
|
|
1946
|
+
def.options.some((o) => o._zod.optout === "optional") ? "optional" : undefined
|
|
1947
|
+
);
|
|
1948
|
+
|
|
1917
1949
|
util.defineLazy(inst._zod, "values", () => {
|
|
1918
1950
|
if (def.options.every((o) => o._zod.values)) {
|
|
1919
1951
|
return new Set<util.Primitive>(def.options.flatMap((option) => Array.from(option._zod.values!)));
|
|
@@ -2074,6 +2106,8 @@ export interface $ZodIntersectionInternals<A extends SomeType = $ZodType, B exte
|
|
|
2074
2106
|
extends $ZodTypeInternals<core.output<A> & core.output<B>, core.input<A> & core.input<B>> {
|
|
2075
2107
|
def: $ZodIntersectionDef<A, B>;
|
|
2076
2108
|
isst: never;
|
|
2109
|
+
optin: A["_zod"]["optin"] | B["_zod"]["optin"];
|
|
2110
|
+
optout: A["_zod"]["optout"] | B["_zod"]["optout"];
|
|
2077
2111
|
}
|
|
2078
2112
|
|
|
2079
2113
|
export interface $ZodIntersection<A extends SomeType = $ZodType, B extends SomeType = $ZodType> extends $ZodType {
|
|
@@ -2344,38 +2378,53 @@ export interface $ZodRecordDef<Key extends $ZodRecordKey = $ZodRecordKey, Value
|
|
|
2344
2378
|
valueType: Value;
|
|
2345
2379
|
}
|
|
2346
2380
|
|
|
2381
|
+
// export type $InferZodRecordOutput<
|
|
2382
|
+
// Key extends $ZodRecordKey = $ZodRecordKey,
|
|
2383
|
+
// Value extends SomeType = $ZodType,
|
|
2384
|
+
// > = undefined extends Key["_zod"]["values"]
|
|
2385
|
+
// ? string extends core.output<Key>
|
|
2386
|
+
// ? Record<core.output<Key>, core.output<Value>>
|
|
2387
|
+
// : number extends core.output<Key>
|
|
2388
|
+
// ? Record<core.output<Key>, core.output<Value>>
|
|
2389
|
+
// : symbol extends core.output<Key>
|
|
2390
|
+
// ? Record<core.output<Key>, core.output<Value>>
|
|
2391
|
+
// : Record<core.output<Key>, core.output<Value>>
|
|
2392
|
+
// : Record<core.output<Key>, core.output<Value>>;
|
|
2347
2393
|
export type $InferZodRecordOutput<
|
|
2348
2394
|
Key extends $ZodRecordKey = $ZodRecordKey,
|
|
2349
2395
|
Value extends SomeType = $ZodType,
|
|
2350
|
-
> =
|
|
2351
|
-
?
|
|
2352
|
-
? Record<core.output<Key>, core.output<Value>>
|
|
2353
|
-
: number extends core.output<Key>
|
|
2354
|
-
? Record<core.output<Key>, core.output<Value>>
|
|
2355
|
-
: symbol extends core.output<Key>
|
|
2356
|
-
? Record<core.output<Key>, core.output<Value>>
|
|
2357
|
-
: Partial<Record<core.output<Key>, core.output<Value>>>
|
|
2396
|
+
> = Key extends $partial
|
|
2397
|
+
? Partial<Record<core.output<Key>, core.output<Value>>>
|
|
2358
2398
|
: Record<core.output<Key>, core.output<Value>>;
|
|
2359
2399
|
|
|
2400
|
+
// export type $InferZodRecordInput<
|
|
2401
|
+
// Key extends $ZodRecordKey = $ZodRecordKey,
|
|
2402
|
+
// Value extends SomeType = $ZodType,
|
|
2403
|
+
// > = undefined extends Key["_zod"]["values"]
|
|
2404
|
+
// ? string extends core.input<Key>
|
|
2405
|
+
// ? Record<core.input<Key>, core.input<Value>>
|
|
2406
|
+
// : number extends core.input<Key>
|
|
2407
|
+
// ? Record<core.input<Key>, core.input<Value>>
|
|
2408
|
+
// : symbol extends core.input<Key>
|
|
2409
|
+
// ? Record<core.input<Key>, core.input<Value>>
|
|
2410
|
+
// : Record<core.input<Key>, core.input<Value>>
|
|
2411
|
+
// : Record<core.input<Key>, core.input<Value>>;
|
|
2360
2412
|
export type $InferZodRecordInput<
|
|
2361
2413
|
Key extends $ZodRecordKey = $ZodRecordKey,
|
|
2362
2414
|
Value extends SomeType = $ZodType,
|
|
2363
|
-
> =
|
|
2364
|
-
?
|
|
2365
|
-
? Record<core.input<Key>, core.input<Value>>
|
|
2366
|
-
: number extends core.input<Key>
|
|
2367
|
-
? Record<core.input<Key>, core.input<Value>>
|
|
2368
|
-
: symbol extends core.input<Key>
|
|
2369
|
-
? Record<core.input<Key>, core.input<Value>>
|
|
2370
|
-
: Partial<Record<core.input<Key>, core.input<Value>>>
|
|
2415
|
+
> = Key extends $partial
|
|
2416
|
+
? Partial<Record<core.input<Key>, core.input<Value>>>
|
|
2371
2417
|
: Record<core.input<Key>, core.input<Value>>;
|
|
2372
2418
|
|
|
2373
2419
|
export interface $ZodRecordInternals<Key extends $ZodRecordKey = $ZodRecordKey, Value extends SomeType = $ZodType>
|
|
2374
2420
|
extends $ZodTypeInternals<$InferZodRecordOutput<Key, Value>, $InferZodRecordInput<Key, Value>> {
|
|
2375
2421
|
def: $ZodRecordDef<Key, Value>;
|
|
2376
2422
|
isst: errors.$ZodIssueInvalidType | errors.$ZodIssueInvalidKey<Record<PropertyKey, unknown>>;
|
|
2423
|
+
optin?: "optional" | undefined;
|
|
2424
|
+
optout?: "optional" | undefined;
|
|
2377
2425
|
}
|
|
2378
2426
|
|
|
2427
|
+
export type $partial = { "~~partial": true };
|
|
2379
2428
|
export interface $ZodRecord<Key extends $ZodRecordKey = $ZodRecordKey, Value extends SomeType = $ZodType>
|
|
2380
2429
|
extends $ZodType {
|
|
2381
2430
|
_zod: $ZodRecordInternals<Key, Value>;
|
|
@@ -2506,6 +2555,8 @@ export interface $ZodMapInternals<Key extends SomeType = $ZodType, Value extends
|
|
|
2506
2555
|
extends $ZodTypeInternals<Map<core.output<Key>, core.output<Value>>, Map<core.input<Key>, core.input<Value>>> {
|
|
2507
2556
|
def: $ZodMapDef<Key, Value>;
|
|
2508
2557
|
isst: errors.$ZodIssueInvalidType | errors.$ZodIssueInvalidKey | errors.$ZodIssueInvalidElement<unknown>;
|
|
2558
|
+
optin?: "optional" | undefined;
|
|
2559
|
+
optout?: "optional" | undefined;
|
|
2509
2560
|
}
|
|
2510
2561
|
|
|
2511
2562
|
export interface $ZodMap<Key extends SomeType = $ZodType, Value extends SomeType = $ZodType> extends $ZodType {
|
|
@@ -2605,6 +2656,8 @@ export interface $ZodSetInternals<T extends SomeType = $ZodType>
|
|
|
2605
2656
|
extends $ZodTypeInternals<Set<core.output<T>>, Set<core.input<T>>> {
|
|
2606
2657
|
def: $ZodSetDef<T>;
|
|
2607
2658
|
isst: errors.$ZodIssueInvalidType;
|
|
2659
|
+
optin?: "optional" | undefined;
|
|
2660
|
+
optout?: "optional" | undefined;
|
|
2608
2661
|
}
|
|
2609
2662
|
|
|
2610
2663
|
export interface $ZodSet<T extends SomeType = $ZodType> extends $ZodType {
|
|
@@ -3015,6 +3068,7 @@ export interface $ZodDefaultInternals<T extends SomeType = $ZodType>
|
|
|
3015
3068
|
extends $ZodTypeInternals<util.NoUndefined<core.output<T>>, core.input<T> | undefined> {
|
|
3016
3069
|
def: $ZodDefaultDef<T>;
|
|
3017
3070
|
optin: "optional";
|
|
3071
|
+
optout?: "optional" | undefined; // required
|
|
3018
3072
|
isst: never;
|
|
3019
3073
|
values: T["_zod"]["values"];
|
|
3020
3074
|
}
|
|
@@ -3075,6 +3129,7 @@ export interface $ZodPrefaultInternals<T extends SomeType = $ZodType>
|
|
|
3075
3129
|
extends $ZodTypeInternals<util.NoUndefined<core.output<T>>, core.input<T> | undefined> {
|
|
3076
3130
|
def: $ZodPrefaultDef<T>;
|
|
3077
3131
|
optin: "optional";
|
|
3132
|
+
optout?: "optional" | undefined;
|
|
3078
3133
|
isst: never;
|
|
3079
3134
|
values: T["_zod"]["values"];
|
|
3080
3135
|
}
|
|
@@ -3117,6 +3172,8 @@ export interface $ZodNonOptionalInternals<T extends SomeType = $ZodType>
|
|
|
3117
3172
|
def: $ZodNonOptionalDef<T>;
|
|
3118
3173
|
isst: errors.$ZodIssueInvalidType;
|
|
3119
3174
|
values: T["_zod"]["values"];
|
|
3175
|
+
optin: "optional" | undefined;
|
|
3176
|
+
optout: "optional" | undefined;
|
|
3120
3177
|
}
|
|
3121
3178
|
|
|
3122
3179
|
export interface $ZodNonOptional<T extends SomeType = $ZodType> extends $ZodType {
|
|
@@ -3210,6 +3267,8 @@ export interface $ZodSuccessDef<T extends SomeType = $ZodType> extends $ZodTypeD
|
|
|
3210
3267
|
export interface $ZodSuccessInternals<T extends SomeType = $ZodType> extends $ZodTypeInternals<boolean, core.input<T>> {
|
|
3211
3268
|
def: $ZodSuccessDef<T>;
|
|
3212
3269
|
isst: never;
|
|
3270
|
+
optin: T["_zod"]["optin"];
|
|
3271
|
+
optout: "optional" | undefined;
|
|
3213
3272
|
}
|
|
3214
3273
|
|
|
3215
3274
|
export interface $ZodSuccess<T extends SomeType = $ZodType> extends $ZodType {
|
|
@@ -3272,7 +3331,7 @@ export interface $ZodCatch<T extends SomeType = $ZodType> extends $ZodType {
|
|
|
3272
3331
|
|
|
3273
3332
|
export const $ZodCatch: core.$constructor<$ZodCatch> = /*@__PURE__*/ core.$constructor("$ZodCatch", (inst, def) => {
|
|
3274
3333
|
$ZodType.init(inst, def);
|
|
3275
|
-
|
|
3334
|
+
inst._zod.optin = "optional";
|
|
3276
3335
|
util.defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
|
|
3277
3336
|
util.defineLazy(inst._zod, "values", () => def.innerType._zod.values);
|
|
3278
3337
|
|
|
@@ -3417,6 +3476,7 @@ export interface $ZodReadonlyInternals<T extends SomeType = $ZodType>
|
|
|
3417
3476
|
optout: T["_zod"]["optout"];
|
|
3418
3477
|
isst: never;
|
|
3419
3478
|
propValues: T["_zod"]["propValues"];
|
|
3479
|
+
values: T["_zod"]["values"];
|
|
3420
3480
|
}
|
|
3421
3481
|
|
|
3422
3482
|
export interface $ZodReadonly<T extends SomeType = $ZodType> extends $ZodType {
|
|
@@ -3428,6 +3488,7 @@ export const $ZodReadonly: core.$constructor<$ZodReadonly> = /*@__PURE__*/ core.
|
|
|
3428
3488
|
(inst, def) => {
|
|
3429
3489
|
$ZodType.init(inst, def);
|
|
3430
3490
|
util.defineLazy(inst._zod, "propValues", () => def.innerType._zod.propValues);
|
|
3491
|
+
util.defineLazy(inst._zod, "values", () => def.innerType._zod.values);
|
|
3431
3492
|
util.defineLazy(inst._zod, "optin", () => def.innerType._zod.optin);
|
|
3432
3493
|
util.defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
|
|
3433
3494
|
|
|
@@ -17,9 +17,13 @@ interface JSONSchemaGeneratorParams {
|
|
|
17
17
|
* - `"any"` — Unrepresentable types become `{}` */
|
|
18
18
|
unrepresentable?: "throw" | "any";
|
|
19
19
|
/** Arbitrary custom logic that can be used to modify the generated JSON Schema. */
|
|
20
|
-
override?: (ctx: {
|
|
20
|
+
override?: (ctx: {
|
|
21
|
+
zodSchema: schemas.$ZodTypes;
|
|
22
|
+
jsonSchema: JSONSchema.BaseSchema;
|
|
23
|
+
path: (string | number)[];
|
|
24
|
+
}) => void;
|
|
21
25
|
/** Whether to extract the `"input"` or `"output"` type. Relevant to transforms, Error converting schema to JSONz, defaults, coerced primitives, etc.
|
|
22
|
-
* - `"output" —
|
|
26
|
+
* - `"output"` — Default. Convert the output schema.
|
|
23
27
|
* - `"input"` — Convert the input schema. */
|
|
24
28
|
io?: "input" | "output";
|
|
25
29
|
}
|
|
@@ -61,13 +65,19 @@ interface Seen {
|
|
|
61
65
|
cycle?: (string | number)[] | undefined;
|
|
62
66
|
isParent?: boolean | undefined;
|
|
63
67
|
ref?: schemas.$ZodType | undefined | null;
|
|
68
|
+
/** JSON Schema property path for this schema */
|
|
69
|
+
path?: (string | number)[] | undefined;
|
|
64
70
|
}
|
|
65
71
|
|
|
66
72
|
export class JSONSchemaGenerator {
|
|
67
73
|
metadataRegistry: $ZodRegistry<Record<string, any>>;
|
|
68
74
|
target: "draft-7" | "draft-2020-12";
|
|
69
75
|
unrepresentable: "throw" | "any";
|
|
70
|
-
override: (ctx: {
|
|
76
|
+
override: (ctx: {
|
|
77
|
+
zodSchema: schemas.$ZodTypes;
|
|
78
|
+
jsonSchema: JSONSchema.BaseSchema;
|
|
79
|
+
path: (string | number)[];
|
|
80
|
+
}) => void;
|
|
71
81
|
io: "input" | "output";
|
|
72
82
|
|
|
73
83
|
counter = 0;
|
|
@@ -110,7 +120,7 @@ export class JSONSchemaGenerator {
|
|
|
110
120
|
}
|
|
111
121
|
|
|
112
122
|
// initialize
|
|
113
|
-
const result: Seen = { schema: {}, count: 1, cycle: undefined };
|
|
123
|
+
const result: Seen = { schema: {}, count: 1, cycle: undefined, path: _params.path };
|
|
114
124
|
this.seen.set(schema, result);
|
|
115
125
|
|
|
116
126
|
// custom method overrides default behavior
|
|
@@ -207,11 +217,6 @@ export class JSONSchemaGenerator {
|
|
|
207
217
|
}
|
|
208
218
|
break;
|
|
209
219
|
}
|
|
210
|
-
case "undefined": {
|
|
211
|
-
const json = _json as JSONSchema.NullSchema;
|
|
212
|
-
json.type = "null";
|
|
213
|
-
break;
|
|
214
|
-
}
|
|
215
220
|
case "null": {
|
|
216
221
|
_json.type = "null";
|
|
217
222
|
break;
|
|
@@ -222,8 +227,10 @@ export class JSONSchemaGenerator {
|
|
|
222
227
|
case "unknown": {
|
|
223
228
|
break;
|
|
224
229
|
}
|
|
225
|
-
case "
|
|
226
|
-
|
|
230
|
+
case "undefined": {
|
|
231
|
+
if (this.unrepresentable === "throw") {
|
|
232
|
+
throw new Error("Undefined cannot be represented in JSON Schema");
|
|
233
|
+
}
|
|
227
234
|
break;
|
|
228
235
|
}
|
|
229
236
|
case "void": {
|
|
@@ -232,6 +239,10 @@ export class JSONSchemaGenerator {
|
|
|
232
239
|
}
|
|
233
240
|
break;
|
|
234
241
|
}
|
|
242
|
+
case "never": {
|
|
243
|
+
_json.not = {};
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
235
246
|
case "date": {
|
|
236
247
|
if (this.unrepresentable === "throw") {
|
|
237
248
|
throw new Error("Date cannot be represented in JSON Schema");
|
|
@@ -740,6 +751,7 @@ export class JSONSchemaGenerator {
|
|
|
740
751
|
this.override({
|
|
741
752
|
zodSchema: zodSchema as schemas.$ZodTypes,
|
|
742
753
|
jsonSchema: schema,
|
|
754
|
+
path: seen.path ?? [],
|
|
743
755
|
});
|
|
744
756
|
};
|
|
745
757
|
|
package/src/v4/mini/schemas.ts
CHANGED
|
@@ -1023,11 +1023,11 @@ export function partialRecord<Key extends core.$ZodRecordKey, Value extends Some
|
|
|
1023
1023
|
keyType: Key,
|
|
1024
1024
|
valueType: Value,
|
|
1025
1025
|
params?: string | core.$ZodRecordParams
|
|
1026
|
-
): ZodMiniRecord<
|
|
1026
|
+
): ZodMiniRecord<Key & core.$partial, Value> {
|
|
1027
1027
|
return new ZodMiniRecord({
|
|
1028
1028
|
type: "record",
|
|
1029
1029
|
keyType: union([keyType, never()]),
|
|
1030
|
-
valueType: valueType as any
|
|
1030
|
+
valueType: valueType as any,
|
|
1031
1031
|
...util.normalizeParams(params),
|
|
1032
1032
|
}) as any;
|
|
1033
1033
|
}
|
|
@@ -99,6 +99,12 @@ test("z.url", () => {
|
|
|
99
99
|
expect(a.parse("http://localhost:3000")).toEqual("http://localhost:3000");
|
|
100
100
|
expect(a.parse("https://localhost:3000")).toEqual("https://localhost:3000");
|
|
101
101
|
|
|
102
|
+
// test trimming
|
|
103
|
+
expect(a.parse(" http://example.com ")).toEqual("http://example.com");
|
|
104
|
+
expect(a.parse(" http://example.com/")).toEqual("http://example.com/");
|
|
105
|
+
expect(a.parse(" http://example.com")).toEqual("http://example.com");
|
|
106
|
+
expect(a.parse(" http://example.com//")).toEqual("http://example.com//");
|
|
107
|
+
|
|
102
108
|
// invalid URLs
|
|
103
109
|
expect(() => a.parse("not-a-url")).toThrow();
|
|
104
110
|
// expect(() => a.parse("http:/example.com")).toThrow();
|
package/v4/classic/schemas.d.cts
CHANGED
|
@@ -151,6 +151,7 @@ export interface ZodString extends _ZodString<core.$ZodStringInternals<string>>
|
|
|
151
151
|
}
|
|
152
152
|
export declare const ZodString: core.$constructor<ZodString>;
|
|
153
153
|
export declare function string(params?: string | core.$ZodStringParams): ZodString;
|
|
154
|
+
export declare function string<T extends string>(params?: string | core.$ZodStringParams): core.$ZodType<T, T>;
|
|
154
155
|
export interface ZodStringFormat<Format extends string = string> extends _ZodString<core.$ZodStringFormatInternals<Format>> {
|
|
155
156
|
}
|
|
156
157
|
export declare const ZodStringFormat: core.$constructor<ZodStringFormat>;
|
|
@@ -474,7 +475,7 @@ export interface ZodRecord<Key extends core.$ZodRecordKey = core.$ZodRecordKey,
|
|
|
474
475
|
}
|
|
475
476
|
export declare const ZodRecord: core.$constructor<ZodRecord>;
|
|
476
477
|
export declare function record<Key extends core.$ZodRecordKey, Value extends core.SomeType>(keyType: Key, valueType: Value, params?: string | core.$ZodRecordParams): ZodRecord<Key, Value>;
|
|
477
|
-
export declare function partialRecord<Key extends core.$ZodRecordKey, Value extends core.SomeType>(keyType: Key, valueType: Value, params?: string | core.$ZodRecordParams): ZodRecord<
|
|
478
|
+
export declare function partialRecord<Key extends core.$ZodRecordKey, Value extends core.SomeType>(keyType: Key, valueType: Value, params?: string | core.$ZodRecordParams): ZodRecord<Key & core.$partial, Value>;
|
|
478
479
|
export interface ZodMap<Key extends core.SomeType = core.$ZodType, Value extends core.SomeType = core.$ZodType> extends _ZodType<core.$ZodMapInternals<Key, Value>>, core.$ZodMap<Key, Value> {
|
|
479
480
|
keyType: Key;
|
|
480
481
|
valueType: Value;
|
package/v4/classic/schemas.d.ts
CHANGED
|
@@ -151,6 +151,7 @@ export interface ZodString extends _ZodString<core.$ZodStringInternals<string>>
|
|
|
151
151
|
}
|
|
152
152
|
export declare const ZodString: core.$constructor<ZodString>;
|
|
153
153
|
export declare function string(params?: string | core.$ZodStringParams): ZodString;
|
|
154
|
+
export declare function string<T extends string>(params?: string | core.$ZodStringParams): core.$ZodType<T, T>;
|
|
154
155
|
export interface ZodStringFormat<Format extends string = string> extends _ZodString<core.$ZodStringFormatInternals<Format>> {
|
|
155
156
|
}
|
|
156
157
|
export declare const ZodStringFormat: core.$constructor<ZodStringFormat>;
|
|
@@ -474,7 +475,7 @@ export interface ZodRecord<Key extends core.$ZodRecordKey = core.$ZodRecordKey,
|
|
|
474
475
|
}
|
|
475
476
|
export declare const ZodRecord: core.$constructor<ZodRecord>;
|
|
476
477
|
export declare function record<Key extends core.$ZodRecordKey, Value extends core.SomeType>(keyType: Key, valueType: Value, params?: string | core.$ZodRecordParams): ZodRecord<Key, Value>;
|
|
477
|
-
export declare function partialRecord<Key extends core.$ZodRecordKey, Value extends core.SomeType>(keyType: Key, valueType: Value, params?: string | core.$ZodRecordParams): ZodRecord<
|
|
478
|
+
export declare function partialRecord<Key extends core.$ZodRecordKey, Value extends core.SomeType>(keyType: Key, valueType: Value, params?: string | core.$ZodRecordParams): ZodRecord<Key & core.$partial, Value>;
|
|
478
479
|
export interface ZodMap<Key extends core.SomeType = core.$ZodType, Value extends core.SomeType = core.$ZodType> extends _ZodType<core.$ZodMapInternals<Key, Value>>, core.$ZodMap<Key, Value> {
|
|
479
480
|
keyType: Key;
|
|
480
481
|
valueType: Value;
|
package/v4/core/registries.cjs
CHANGED
package/v4/core/registries.js
CHANGED
package/v4/core/schemas.cjs
CHANGED
|
@@ -38,8 +38,6 @@ const versions_js_1 = require("./versions.cjs");
|
|
|
38
38
|
exports.$ZodType = core.$constructor("$ZodType", (inst, def) => {
|
|
39
39
|
var _a;
|
|
40
40
|
inst ?? (inst = {});
|
|
41
|
-
// avoids issues with using Math.random() in Next.js caching
|
|
42
|
-
util.defineLazy(inst._zod, "id", () => def.type + "_" + util.randomString(10));
|
|
43
41
|
inst._zod.def = def; // set _def property
|
|
44
42
|
inst._zod.bag = inst._zod.bag || {}; // initialize _bag object
|
|
45
43
|
inst._zod.version = versions_js_1.version;
|
|
@@ -189,7 +187,9 @@ exports.$ZodURL = core.$constructor("$ZodURL", (inst, def) => {
|
|
|
189
187
|
exports.$ZodStringFormat.init(inst, def);
|
|
190
188
|
inst._zod.check = (payload) => {
|
|
191
189
|
try {
|
|
192
|
-
const
|
|
190
|
+
const orig = payload.value;
|
|
191
|
+
const url = new URL(orig);
|
|
192
|
+
const href = url.href;
|
|
193
193
|
if (def.hostname) {
|
|
194
194
|
def.hostname.lastIndex = 0;
|
|
195
195
|
if (!def.hostname.test(url.hostname)) {
|
|
@@ -218,6 +218,13 @@ exports.$ZodURL = core.$constructor("$ZodURL", (inst, def) => {
|
|
|
218
218
|
});
|
|
219
219
|
}
|
|
220
220
|
}
|
|
221
|
+
// payload.value = url.href;
|
|
222
|
+
if (!orig.endsWith("/") && href.endsWith("/")) {
|
|
223
|
+
payload.value = href.slice(0, -1);
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
payload.value = href;
|
|
227
|
+
}
|
|
221
228
|
return;
|
|
222
229
|
}
|
|
223
230
|
catch (_) {
|
|
@@ -546,6 +553,8 @@ exports.$ZodUndefined = core.$constructor("$ZodUndefined", (inst, def) => {
|
|
|
546
553
|
exports.$ZodType.init(inst, def);
|
|
547
554
|
inst._zod.pattern = regexes.undefined;
|
|
548
555
|
inst._zod.values = new Set([undefined]);
|
|
556
|
+
inst._zod.optin = "optional";
|
|
557
|
+
inst._zod.optout = "optional";
|
|
549
558
|
inst._zod.parse = (payload, _ctx) => {
|
|
550
559
|
const input = payload.value;
|
|
551
560
|
if (typeof input === "undefined")
|
|
@@ -749,8 +758,9 @@ exports.$ZodObject = core.$constructor("$ZodObject", (inst, def) => {
|
|
|
749
758
|
};
|
|
750
759
|
doc.write(`const input = payload.value;`);
|
|
751
760
|
const ids = Object.create(null);
|
|
761
|
+
let counter = 0;
|
|
752
762
|
for (const key of normalized.keys) {
|
|
753
|
-
ids[key] =
|
|
763
|
+
ids[key] = `key_${counter++}`;
|
|
754
764
|
}
|
|
755
765
|
// A: preserve key order {
|
|
756
766
|
doc.write(`const newResult = {}`);
|
|
@@ -909,6 +919,8 @@ function handleUnionResults(results, final, inst, ctx) {
|
|
|
909
919
|
}
|
|
910
920
|
exports.$ZodUnion = core.$constructor("$ZodUnion", (inst, def) => {
|
|
911
921
|
exports.$ZodType.init(inst, def);
|
|
922
|
+
util.defineLazy(inst._zod, "optin", () => def.options.some((o) => o._zod.optin === "optional") ? "optional" : undefined);
|
|
923
|
+
util.defineLazy(inst._zod, "optout", () => def.options.some((o) => o._zod.optout === "optional") ? "optional" : undefined);
|
|
912
924
|
util.defineLazy(inst._zod, "values", () => {
|
|
913
925
|
if (def.options.every((o) => o._zod.values)) {
|
|
914
926
|
return new Set(def.options.flatMap((option) => Array.from(option._zod.values)));
|
|
@@ -1550,7 +1562,7 @@ exports.$ZodSuccess = core.$constructor("$ZodSuccess", (inst, def) => {
|
|
|
1550
1562
|
});
|
|
1551
1563
|
exports.$ZodCatch = core.$constructor("$ZodCatch", (inst, def) => {
|
|
1552
1564
|
exports.$ZodType.init(inst, def);
|
|
1553
|
-
|
|
1565
|
+
inst._zod.optin = "optional";
|
|
1554
1566
|
util.defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
|
|
1555
1567
|
util.defineLazy(inst._zod, "values", () => def.innerType._zod.values);
|
|
1556
1568
|
inst._zod.parse = (payload, ctx) => {
|
|
@@ -1622,6 +1634,7 @@ function handlePipeResult(left, def, ctx) {
|
|
|
1622
1634
|
exports.$ZodReadonly = core.$constructor("$ZodReadonly", (inst, def) => {
|
|
1623
1635
|
exports.$ZodType.init(inst, def);
|
|
1624
1636
|
util.defineLazy(inst._zod, "propValues", () => def.innerType._zod.propValues);
|
|
1637
|
+
util.defineLazy(inst._zod, "values", () => def.innerType._zod.values);
|
|
1625
1638
|
util.defineLazy(inst._zod, "optin", () => def.innerType._zod.optin);
|
|
1626
1639
|
util.defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
|
|
1627
1640
|
inst._zod.parse = (payload, ctx) => {
|