Skip to content

Commit 9126782

Browse files
committed
Update input<> and output<> to prevent excessively deep in more cases
1 parent 011623a commit 9126782

File tree

8 files changed

+41
-28
lines changed

8 files changed

+41
-28
lines changed

packages/tsc/generate.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { writeFileSync } from "node:fs";
2+
import { mkdirSync } from "node:fs";
3+
import { dirname } from "node:path";
24

35
export const ZOD = {
46
imports: [`import * as z from "zod/v4";`],
@@ -175,6 +177,8 @@ export function generate(params: GenerateObjectsParams) {
175177

176178
file.push(custom);
177179

180+
// Create directory if it doesn't exist
181+
mkdirSync(dirname(path), { recursive: true });
178182
writeFileSync(path, file.join("\n"), { flag: "w" });
179183
}
180184

@@ -304,6 +308,8 @@ export function generateExtendChain(params: GenerateExtendChainParams) {
304308
}
305309
}
306310

311+
// Create directory if it doesn't exist
312+
mkdirSync(dirname(path), { recursive: true });
307313
writeFileSync(path, file.join("\n"), { flag: "w" });
308314
}
309315

packages/tsc/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"compilerOptions": {
55
"target": "es2022",
66
"module": "NodeNext",
7-
// "customConditions": ["@zod/source"],
7+
"customConditions": [],
88
"incremental": false,
99
"noEmit": true
1010
}

packages/zod/src/v4/classic/schemas.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ export const ZodObject: core.$constructor<ZodObject> = /*@__PURE__*/ core.$const
11301130
return Object.fromEntries(Object.entries(inst._zod.def.shape));
11311131
});
11321132
inst.keyof = () => _enum(Object.keys(inst._zod.def.shape)) as any;
1133-
inst.catchall = (catchall) => inst.clone({ ...inst._zod.def, catchall });
1133+
inst.catchall = (catchall) => inst.clone({ ...inst._zod.def, catchall }) as any;
11341134
inst.passthrough = () => inst.clone({ ...inst._zod.def, catchall: unknown() });
11351135
// inst.nonstrict = () => inst.clone({ ...inst._zod.def, catchall: api.unknown() });
11361136
inst.loose = () => inst.clone({ ...inst._zod.def, catchall: unknown() });

packages/zod/src/v4/classic/tests/registries.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,8 @@ test("inherit across clone", () => {
158158
expect(C.meta()).toEqual({ a: true, b: true, description: "hello" });
159159
});
160160

161-
test("register with replace", () => {
161+
test("loose examples", () => {
162162
z.string().register(z.globalRegistry, {
163163
examples: ["example"],
164164
});
165-
z.string().register(z.globalRegistry, {
166-
// @ts-expect-error
167-
examples: [1234],
168-
});
169165
});

packages/zod/src/v4/core/core.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,23 @@ export class $ZodAsyncError extends Error {
7575

7676
//////////////////////////// TYPE HELPERS ///////////////////////////////////
7777

78-
export type input<T extends schemas.$ZodType> = T["_zod"] extends { "~input": any }
78+
// export type input<T extends schemas.$ZodType> = T["_zod"] extends { "~input": any }
79+
// ? T["_zod"]["~input"]
80+
// : T["_zod"]["input"];
81+
// export type output<T extends schemas.$ZodType> = T["_zod"] extends { "~output": any }
82+
// ? T["_zod"]["~output"]
83+
// : T["_zod"]["output"];
84+
export type input<T> = T extends { _zod: { "~input": any } }
7985
? T["_zod"]["~input"]
80-
: T["_zod"]["input"];
81-
export type output<T extends schemas.$ZodType> = T["_zod"] extends { "~output": any }
86+
: T extends { _zod: { input: any } }
87+
? T["_zod"]["input"]
88+
: never;
89+
export type output<T> = T extends { _zod: { "~output": any } }
8290
? T["_zod"]["~output"]
83-
: T["_zod"]["output"];
91+
: T extends { _zod: { output: any } }
92+
? T["_zod"]["output"]
93+
: never;
94+
8495
export type { output as infer };
8596

8697
////////////////////////////// CONFIG ///////////////////////////////////////

packages/zod/src/v4/core/parse.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const _parse: (_Err: $ZodErrorClass) => $Parse = (_Err) => (schema, value
2424
Error.captureStackTrace(e, _params?.callee);
2525
throw e;
2626
}
27-
return result.value;
27+
return result.value as core.output<typeof schema>;
2828
};
2929

3030
export const parse: $Parse = /* @__PURE__*/ _parse(errors.$ZodRealError);
@@ -68,7 +68,7 @@ export const _safeParse: (_Err: $ZodErrorClass) => $SafeParse = (_Err) => (schem
6868
success: false,
6969
error: new (_Err ?? errors.$ZodError)(result.issues.map((iss) => util.finalizeIssue(iss, ctx, core.config()))),
7070
}
71-
: { success: true, data: result.value };
71+
: ({ success: true, data: result.value } as any);
7272
};
7373
export const safeParse: $SafeParse = /* @__PURE__*/ _safeParse(errors.$ZodRealError);
7474

@@ -88,7 +88,7 @@ export const _safeParseAsync: (_Err: $ZodErrorClass) => $SafeParseAsync = (_Err)
8888
success: false,
8989
error: new _Err(result.issues.map((iss) => util.finalizeIssue(iss, ctx, core.config()))),
9090
}
91-
: { success: true, data: result.value };
91+
: ({ success: true, data: result.value } as any);
9292
};
9393

9494
export const safeParseAsync: $SafeParseAsync = /* @__PURE__*/ _safeParseAsync(errors.$ZodRealError);

packages/zod/src/v4/mini/schemas.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,7 @@ export function partialRecord<Key extends core.$ZodRecordKey, Value extends Some
986986
keyType: union([keyType, never()]),
987987
valueType,
988988
...util.normalizeParams(params),
989-
}) as ZodMiniRecord<Key, Value>;
989+
}) as ZodMiniRecord<ZodMiniUnion<[Key, ZodMiniNever]>, Value>;
990990
}
991991

992992
// ZodMiniMap
@@ -1512,17 +1512,15 @@ export const stringbool: (
15121512
) as any;
15131513

15141514
// json
1515-
export type ZodMiniJSONSchema = ZodMiniLazy<
1516-
ZodMiniUnion<
1517-
[
1518-
ZodMiniString<string>,
1519-
ZodMiniNumber<number>,
1520-
ZodMiniBoolean<boolean>,
1521-
ZodMiniNull,
1522-
ZodMiniArray<ZodMiniJSONSchema>,
1523-
ZodMiniRecord<ZodMiniString<string>, ZodMiniJSONSchema>,
1524-
]
1525-
>
1515+
export type ZodMiniJSONSchema = ZodMiniUnion<
1516+
[
1517+
ZodMiniString,
1518+
ZodMiniNumber,
1519+
ZodMiniBoolean,
1520+
ZodMiniNull,
1521+
ZodMiniArray<ZodMiniJSONSchema>,
1522+
ZodMiniRecord<ZodMiniString<string>, ZodMiniJSONSchema>,
1523+
]
15261524
> & {
15271525
_zod: {
15281526
input: util.JSONType;
@@ -1531,8 +1529,8 @@ export type ZodMiniJSONSchema = ZodMiniLazy<
15311529
};
15321530

15331531
export function json(): ZodMiniJSONSchema {
1534-
const jsonSchema: ZodMiniJSONSchema = _lazy(() => {
1532+
const jsonSchema: any = _lazy(() => {
15351533
return union([string(), number(), boolean(), _null(), array(jsonSchema), record(string(), jsonSchema)]);
1536-
}) as ZodMiniJSONSchema;
1534+
});
15371535
return jsonSchema;
15381536
}

packages/zod/src/v4/mini/tests/index.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,8 @@ test("z.lazy", () => {
720720
test("z.json", () => {
721721
const a = z.json();
722722
type a = z.output<typeof a>;
723+
a._zod.output;
724+
723725
expectTypeOf<a>().toEqualTypeOf<util.JSONType>();
724726

725727
expect(z.parse(a, "hello")).toEqual("hello");

0 commit comments

Comments
 (0)