Skip to content

Commit 2954f40

Browse files
authored
Fix json (#4630)
* Fix json * Tweaks * Clean up * Clean up
1 parent c58bd9b commit 2954f40

File tree

4 files changed

+49
-99
lines changed

4 files changed

+49
-99
lines changed

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ export function date(params?: string | core.$ZodDateParams): ZodDate {
987987

988988
// ZodArray
989989
export interface ZodArray<T extends core.SomeType = core.$ZodType>
990-
extends ZodType<any, any, core.$ZodArrayInternals<T>>,
990+
extends _ZodType<core.$ZodArrayInternals<T>>,
991991
core.$ZodArray<T> {
992992
element: T;
993993
min(minLength: number, params?: string | core.$ZodCheckMinLengthParams): this;
@@ -1370,7 +1370,7 @@ export function map<Key extends core.SomeType, Value extends core.SomeType>(
13701370

13711371
// ZodSet
13721372
export interface ZodSet<T extends core.SomeType = core.$ZodType>
1373-
extends ZodType<any, any, core.$ZodSetInternals<T>>,
1373+
extends _ZodType<core.$ZodSetInternals<T>>,
13741374
core.$ZodSet<T> {
13751375
min(minSize: number, params?: string | core.$ZodCheckMinSizeParams): this;
13761376
/** */
@@ -1996,14 +1996,18 @@ export const stringbool: (_params?: string | core.$ZodStringBoolParams) => ZodPi
19961996
) as any;
19971997

19981998
// json
1999-
export type ZodJSONSchema = ZodUnion<
1999+
type _ZodJSONSchema = ZodUnion<
20002000
[ZodString, ZodNumber, ZodBoolean, ZodNull, ZodArray<ZodJSONSchema>, ZodRecord<ZodString, ZodJSONSchema>]
2001-
> & {
2002-
_zod: {
2003-
input: util.JSONType;
2004-
output: util.JSONType;
2005-
};
2006-
};
2001+
>;
2002+
type _ZodJSONSchemaInternals = _ZodJSONSchema["_zod"];
2003+
2004+
export interface ZodJSONSchemaInternals extends _ZodJSONSchemaInternals {
2005+
output: util.JSONType;
2006+
input: util.JSONType;
2007+
}
2008+
export interface ZodJSONSchema extends _ZodJSONSchema {
2009+
_zod: ZodJSONSchemaInternals;
2010+
}
20072011

20082012
export function json(params?: string | core.$ZodCustomParams): ZodJSONSchema {
20092013
const jsonSchema: any = lazy(() => {
@@ -2016,9 +2020,9 @@ export function json(params?: string | core.$ZodCustomParams): ZodJSONSchema {
20162020
// preprocess
20172021

20182022
// /** @deprecated Use `z.pipe()` and `z.transform()` instead. */
2019-
export function preprocess<A, U extends core.SomeType>(
2020-
fn: (arg: unknown, ctx: RefinementCtx) => A,
2023+
export function preprocess<A, U extends core.SomeType, B = unknown>(
2024+
fn: (arg: B, ctx: RefinementCtx) => A,
20212025
schema: U
2022-
): ZodPipe<ZodTransform<A, unknown>, U> {
2023-
return pipe(transform(fn as any), schema as any);
2026+
): ZodPipe<ZodTransform<A, B>, U> {
2027+
return pipe(transform(fn as any), schema as any) as any;
20242028
}

packages/zod/src/v4/classic/tests/recursive-types.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ test("mutual recursion with meta", () => {
211211
b: _B;
212212
}>
213213
| undefined;
214+
// | undefined;
214215
type _B = Readonly<{
215216
name: string;
216217
a?: _A;

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

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,7 @@ export interface $ZodTypeInternals<out O = unknown, out I = unknown> extends _$Z
154154

155155
export type $ZodStandardSchema<T> = StandardSchemaV1.Props<core.input<T>, core.output<T>>;
156156

157-
// export interface _$ZodType<T extends _$ZodTypeInternals = _$ZodTypeInternals> {
158-
// _zod: T;
159-
// }
160157
export type SomeType = { _zod: _$ZodTypeInternals };
161-
// _$ZodType; //<any, any, $ZodTypeInternals<unknown, unknown>>;
162158

163159
export interface $ZodType<
164160
O = unknown,
@@ -168,6 +164,10 @@ export interface $ZodType<
168164
_zod: Internals;
169165
"~standard": $ZodStandardSchema<this>;
170166
}
167+
export interface _$ZodType<T extends $ZodTypeInternals = $ZodTypeInternals>
168+
extends $ZodType<T["output"], T["input"], T> {
169+
// _zod: T;
170+
}
171171

172172
export const $ZodType: core.$constructor<$ZodType> = /*@__PURE__*/ core.$constructor("$ZodType", (inst, def) => {
173173
inst ??= {} as any;
@@ -300,8 +300,8 @@ export interface $ZodStringInternals<Input> extends $ZodTypeInternals<string, In
300300
}>;
301301
}
302302

303-
export interface $ZodString<Input = unknown> extends $ZodType {
304-
_zod: $ZodStringInternals<Input>;
303+
export interface $ZodString<Input = unknown> extends _$ZodType<$ZodStringInternals<Input>> {
304+
// _zod: $ZodStringInternals<Input>;
305305
}
306306

307307
export const $ZodString: core.$constructor<$ZodString> = /*@__PURE__*/ core.$constructor("$ZodString", (inst, def) => {
@@ -1463,9 +1463,8 @@ export interface $ZodArrayInternals<T extends SomeType = $ZodType>
14631463
isst: errors.$ZodIssueInvalidType;
14641464
}
14651465

1466-
export interface $ZodArray<T extends SomeType = $ZodType> extends $ZodType {
1467-
_zod: $ZodArrayInternals<T>;
1468-
}
1466+
export interface $ZodArray<T extends SomeType = $ZodType>
1467+
extends $ZodType<core.output<T>[], core.input<T>[], $ZodArrayInternals<T>> {}
14691468

14701469
function handleArrayResult(result: ParsePayload<any>, final: ParsePayload<any[]>, index: number) {
14711470
if (result.issues.length) {
@@ -1524,30 +1523,6 @@ export const $ZodArray: core.$constructor<$ZodArray> = /*@__PURE__*/ core.$const
15241523
////////// //////////
15251524
//////////////////////////////////////////
15261525
//////////////////////////////////////////
1527-
export type $ZodShape = Readonly<{ [k: string]: $ZodType }>;
1528-
1529-
export interface $ZodObjectDef<Shape extends $ZodShape = $ZodShape> extends $ZodTypeDef {
1530-
type: "object";
1531-
shape: Shape;
1532-
catchall?: $ZodType | undefined;
1533-
}
1534-
1535-
export interface $ZodObjectInternals<
1536-
/** @ts-ignore Cast variance */
1537-
out Shape extends Readonly<$ZodShape> = Readonly<$ZodShape>,
1538-
out Config extends $ZodObjectConfig = $ZodObjectConfig,
1539-
> extends _$ZodTypeInternals {
1540-
def: $ZodObjectDef<Shape>;
1541-
config: Config;
1542-
isst: errors.$ZodIssueInvalidType | errors.$ZodIssueUnrecognizedKeys;
1543-
propValues: util.PropValues;
1544-
// special keys only used for objects
1545-
// not defined on $ZodTypeInternals (base interface) because it breaks cyclical inference
1546-
// the z.infer<> util checks for these first when extracting inferred type
1547-
output: $InferObjectOutput<Shape, Config["out"]>;
1548-
input: $InferObjectInput<Shape, Config["in"]>;
1549-
}
1550-
export type $ZodLooseShape = Record<string, any>;
15511526

15521527
type OptionalOutSchema = { _zod: { optout: "optional" } };
15531528
type OptionalInSchema = { _zod: { optin: "optional" } };
@@ -1656,12 +1631,33 @@ export type $catchall<T extends SomeType> = {
16561631
in: { [k: string]: core.input<T> };
16571632
};
16581633

1634+
export type $ZodShape = Readonly<{ [k: string]: $ZodType }>;
1635+
1636+
export interface $ZodObjectDef<Shape extends $ZodShape = $ZodShape> extends $ZodTypeDef {
1637+
type: "object";
1638+
shape: Shape;
1639+
catchall?: $ZodType | undefined;
1640+
}
1641+
1642+
export interface $ZodObjectInternals<
1643+
/** @ts-ignore Cast variance */
1644+
out Shape extends Readonly<$ZodShape> = Readonly<$ZodShape>,
1645+
out Config extends $ZodObjectConfig = $ZodObjectConfig,
1646+
> extends _$ZodTypeInternals {
1647+
def: $ZodObjectDef<Shape>;
1648+
config: Config;
1649+
isst: errors.$ZodIssueInvalidType | errors.$ZodIssueUnrecognizedKeys;
1650+
propValues: util.PropValues;
1651+
output: $InferObjectOutput<Shape, Config["out"]>;
1652+
input: $InferObjectInput<Shape, Config["in"]>;
1653+
}
1654+
export type $ZodLooseShape = Record<string, any>;
1655+
16591656
export interface $ZodObject<
16601657
/** @ts-ignore Cast variance */
16611658
out Shape extends Readonly<$ZodShape> = Readonly<$ZodShape>,
16621659
out Params extends $ZodObjectConfig = $ZodObjectConfig,
16631660
> extends $ZodType<any, any, $ZodObjectInternals<Shape, Params>> {
1664-
// _zod: $ZodObjectInternals<Shape, Params>;
16651661
"~standard": $ZodStandardSchema<this>;
16661662
}
16671663

play.ts

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,3 @@
11
import { z } from "zod/v4";
22

3-
z.ipv4()
4-
.transform((value) => {
5-
// Called when invalid value
6-
console.log("input:", value);
7-
8-
return value;
9-
})
10-
.safeParse("invalid_value");
11-
12-
z.email()
13-
.transform((value) => {
14-
// Called when invalid value
15-
console.log("input:", value);
16-
17-
return value;
18-
})
19-
.safeParse("invalid_value");
20-
21-
z.uuid()
22-
.transform((value) => {
23-
// Called when invalid value
24-
console.log("input:", value);
25-
26-
return value;
27-
})
28-
.safeParse("invalid_value");
29-
30-
z.url()
31-
.transform((value) => {
32-
// Not called when invalid value
33-
console.log("input:", value);
34-
return value;
35-
})
36-
.safeParse("invalid_value");
37-
38-
z.number()
39-
.transform((value) => {
40-
// Not called when invalid value
41-
console.log("input:", value);
42-
43-
return value;
44-
})
45-
.safeParse("invalid_value");
46-
47-
z.boolean()
48-
.transform((value) => {
49-
// Not called when invalid value
50-
console.log("input:", value);
51-
52-
return value;
53-
})
54-
.safeParse("invalid_value");
3+
z;

0 commit comments

Comments
 (0)