Skip to content

Commit 714698d

Browse files
Copilotmrlubos
andcommitted
refactor: initialize Zod node with $.object(), call .pretty() in boolean branch only
Co-authored-by: mrlubos <12529395+mrlubos@users.noreply.github.com>
1 parent 202cc05 commit 714698d

4 files changed

Lines changed: 50 additions & 194 deletions

File tree

packages/openapi-ts-tests/zod/v4/__snapshots__/3.1.x/mini/validators-metadata-fn/zod.gen.ts

Lines changed: 23 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -2,128 +2,56 @@
22

33
import * as z from 'zod/mini';
44

5-
export const zBaz = z._default(z.readonly(z.string().check(z.regex(/foo\nbar/)).register(z.globalRegistry, {
6-
custom: 'value',
7-
title: 'string'
8-
})), 'baz');
5+
export const zBaz = z._default(z.readonly(z.string().check(z.regex(/foo\nbar/)).register(z.globalRegistry, { custom: 'value', title: 'string' })), 'baz');
96

107
export const zQux = z.record(z.string(), z.object({
11-
qux: z.optional(z.string().register(z.globalRegistry, {
12-
custom: 'value',
13-
title: 'string'
14-
}))
15-
}).register(z.globalRegistry, {
16-
custom: 'value',
17-
title: 'object'
18-
})).register(z.globalRegistry, {
19-
custom: 'value',
20-
title: 'object'
21-
});
8+
qux: z.optional(z.string().register(z.globalRegistry, { custom: 'value', title: 'string' }))
9+
}).register(z.globalRegistry, { custom: 'value', title: 'object' })).register(z.globalRegistry, { custom: 'value', title: 'object' });
2210

2311
/**
2412
* This is Foo schema.
2513
*/
2614
export const zFoo = z._default(z.nullable(z.object({
27-
foo: z.optional(z.string().check(z.regex(/^\d{3}-\d{2}-\d{4}$/)).register(z.globalRegistry, {
28-
custom: 'value',
29-
title: 'This is foo property.'
30-
})),
15+
foo: z.optional(z.string().check(z.regex(/^\d{3}-\d{2}-\d{4}$/)).register(z.globalRegistry, { custom: 'value', title: 'This is foo property.' })),
3116
bar: z.optional(z.lazy((): any => zBar)),
32-
baz: z.optional(z.array(z.lazy((): any => zFoo)).register(z.globalRegistry, {
33-
custom: 'value',
34-
title: 'This is baz property.'
35-
})),
36-
qux: z._default(z.optional(z.int().check(z.gt(0)).register(z.globalRegistry, {
37-
custom: 'value',
38-
title: 'This is qux property.'
39-
})), 0)
40-
}).register(z.globalRegistry, {
41-
custom: 'value',
42-
title: 'object'
43-
})), null);
17+
baz: z.optional(z.array(z.lazy((): any => zFoo)).register(z.globalRegistry, { custom: 'value', title: 'This is baz property.' })),
18+
qux: z._default(z.optional(z.int().check(z.gt(0)).register(z.globalRegistry, { custom: 'value', title: 'This is qux property.' })), 0)
19+
}).register(z.globalRegistry, { custom: 'value', title: 'object' })), null);
4420

4521
/**
4622
* This is Bar schema.
4723
*/
4824
export const zBar = z.object({
4925
foo: z.optional(zFoo)
50-
}).register(z.globalRegistry, {
51-
custom: 'value',
52-
title: 'This is Bar schema.'
53-
});
26+
}).register(z.globalRegistry, { custom: 'value', title: 'This is Bar schema.' });
5427

5528
/**
5629
* This is Foo parameter.
5730
*/
58-
export const zFoo2 = z.string().register(z.globalRegistry, {
59-
custom: 'value',
60-
title: 'This is Foo parameter.'
61-
});
31+
export const zFoo2 = z.string().register(z.globalRegistry, { custom: 'value', title: 'This is Foo parameter.' });
6232

6333
export const zFoo3 = z.object({
6434
foo: z.optional(zBar)
65-
}).register(z.globalRegistry, {
66-
custom: 'value',
67-
title: 'object'
68-
});
35+
}).register(z.globalRegistry, { custom: 'value', title: 'object' });
6936

7037
export const zPatchFooData = z.object({
7138
body: z.object({
72-
foo: z.optional(z.string().register(z.globalRegistry, {
73-
custom: 'value',
74-
title: 'string'
75-
}))
76-
}).register(z.globalRegistry, {
77-
custom: 'value',
78-
title: 'object'
79-
}),
80-
path: z.optional(z.never().register(z.globalRegistry, {
81-
custom: 'value',
82-
title: 'never'
83-
})),
39+
foo: z.optional(z.string().register(z.globalRegistry, { custom: 'value', title: 'string' }))
40+
}).register(z.globalRegistry, { custom: 'value', title: 'object' }),
41+
path: z.optional(z.never().register(z.globalRegistry, { custom: 'value', title: 'never' })),
8442
query: z.optional(z.object({
85-
foo: z.optional(z.string().register(z.globalRegistry, {
86-
custom: 'value',
87-
title: 'This is Foo parameter.'
88-
})),
43+
foo: z.optional(z.string().register(z.globalRegistry, { custom: 'value', title: 'This is Foo parameter.' })),
8944
bar: z.optional(zBar),
9045
baz: z.optional(z.object({
91-
baz: z.optional(z.string().register(z.globalRegistry, {
92-
custom: 'value',
93-
title: 'string'
94-
}))
95-
}).register(z.globalRegistry, {
96-
custom: 'value',
97-
title: 'object'
98-
})),
99-
qux: z.optional(z.iso.date().register(z.globalRegistry, {
100-
custom: 'value',
101-
title: 'string'
102-
})),
103-
quux: z.optional(z.iso.datetime().register(z.globalRegistry, {
104-
custom: 'value',
105-
title: 'string'
106-
}))
107-
}).register(z.globalRegistry, {
108-
custom: 'value',
109-
title: 'object'
110-
}))
111-
}).register(z.globalRegistry, {
112-
custom: 'value',
113-
title: 'object'
114-
});
46+
baz: z.optional(z.string().register(z.globalRegistry, { custom: 'value', title: 'string' }))
47+
}).register(z.globalRegistry, { custom: 'value', title: 'object' })),
48+
qux: z.optional(z.iso.date().register(z.globalRegistry, { custom: 'value', title: 'string' })),
49+
quux: z.optional(z.iso.datetime().register(z.globalRegistry, { custom: 'value', title: 'string' }))
50+
}).register(z.globalRegistry, { custom: 'value', title: 'object' }))
51+
}).register(z.globalRegistry, { custom: 'value', title: 'object' });
11552

11653
export const zPostFooData = z.object({
11754
body: zFoo3,
118-
path: z.optional(z.never().register(z.globalRegistry, {
119-
custom: 'value',
120-
title: 'never'
121-
})),
122-
query: z.optional(z.never().register(z.globalRegistry, {
123-
custom: 'value',
124-
title: 'never'
125-
}))
126-
}).register(z.globalRegistry, {
127-
custom: 'value',
128-
title: 'object'
129-
});
55+
path: z.optional(z.never().register(z.globalRegistry, { custom: 'value', title: 'never' })),
56+
query: z.optional(z.never().register(z.globalRegistry, { custom: 'value', title: 'never' }))
57+
}).register(z.globalRegistry, { custom: 'value', title: 'object' });

packages/openapi-ts-tests/zod/v4/__snapshots__/3.1.x/v4/validators-metadata-fn/zod.gen.ts

Lines changed: 23 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -2,128 +2,56 @@
22

33
import * as z from 'zod';
44

5-
export const zBaz = z.string().regex(/foo\nbar/).register(z.globalRegistry, {
6-
custom: 'value',
7-
title: 'string'
8-
}).readonly().default('baz');
5+
export const zBaz = z.string().regex(/foo\nbar/).register(z.globalRegistry, { custom: 'value', title: 'string' }).readonly().default('baz');
96

107
export const zQux = z.record(z.string(), z.object({
11-
qux: z.string().register(z.globalRegistry, {
12-
custom: 'value',
13-
title: 'string'
14-
}).optional()
15-
}).register(z.globalRegistry, {
16-
custom: 'value',
17-
title: 'object'
18-
})).register(z.globalRegistry, {
19-
custom: 'value',
20-
title: 'object'
21-
});
8+
qux: z.string().register(z.globalRegistry, { custom: 'value', title: 'string' }).optional()
9+
}).register(z.globalRegistry, { custom: 'value', title: 'object' })).register(z.globalRegistry, { custom: 'value', title: 'object' });
2210

2311
/**
2412
* This is Foo schema.
2513
*/
2614
export const zFoo = z.object({
27-
foo: z.string().regex(/^\d{3}-\d{2}-\d{4}$/).register(z.globalRegistry, {
28-
custom: 'value',
29-
title: 'This is foo property.'
30-
}).optional(),
15+
foo: z.string().regex(/^\d{3}-\d{2}-\d{4}$/).register(z.globalRegistry, { custom: 'value', title: 'This is foo property.' }).optional(),
3116
bar: z.lazy((): any => zBar).optional(),
32-
baz: z.array(z.lazy((): any => zFoo)).register(z.globalRegistry, {
33-
custom: 'value',
34-
title: 'This is baz property.'
35-
}).optional(),
36-
qux: z.int().gt(0).register(z.globalRegistry, {
37-
custom: 'value',
38-
title: 'This is qux property.'
39-
}).optional().default(0)
40-
}).register(z.globalRegistry, {
41-
custom: 'value',
42-
title: 'object'
43-
}).nullable().default(null);
17+
baz: z.array(z.lazy((): any => zFoo)).register(z.globalRegistry, { custom: 'value', title: 'This is baz property.' }).optional(),
18+
qux: z.int().gt(0).register(z.globalRegistry, { custom: 'value', title: 'This is qux property.' }).optional().default(0)
19+
}).register(z.globalRegistry, { custom: 'value', title: 'object' }).nullable().default(null);
4420

4521
/**
4622
* This is Bar schema.
4723
*/
4824
export const zBar = z.object({
4925
foo: zFoo.optional()
50-
}).register(z.globalRegistry, {
51-
custom: 'value',
52-
title: 'This is Bar schema.'
53-
});
26+
}).register(z.globalRegistry, { custom: 'value', title: 'This is Bar schema.' });
5427

5528
/**
5629
* This is Foo parameter.
5730
*/
58-
export const zFoo2 = z.string().register(z.globalRegistry, {
59-
custom: 'value',
60-
title: 'This is Foo parameter.'
61-
});
31+
export const zFoo2 = z.string().register(z.globalRegistry, { custom: 'value', title: 'This is Foo parameter.' });
6232

6333
export const zFoo3 = z.object({
6434
foo: zBar.optional()
65-
}).register(z.globalRegistry, {
66-
custom: 'value',
67-
title: 'object'
68-
});
35+
}).register(z.globalRegistry, { custom: 'value', title: 'object' });
6936

7037
export const zPatchFooData = z.object({
7138
body: z.object({
72-
foo: z.string().register(z.globalRegistry, {
73-
custom: 'value',
74-
title: 'string'
75-
}).optional()
76-
}).register(z.globalRegistry, {
77-
custom: 'value',
78-
title: 'object'
79-
}),
80-
path: z.never().register(z.globalRegistry, {
81-
custom: 'value',
82-
title: 'never'
83-
}).optional(),
39+
foo: z.string().register(z.globalRegistry, { custom: 'value', title: 'string' }).optional()
40+
}).register(z.globalRegistry, { custom: 'value', title: 'object' }),
41+
path: z.never().register(z.globalRegistry, { custom: 'value', title: 'never' }).optional(),
8442
query: z.object({
85-
foo: z.string().register(z.globalRegistry, {
86-
custom: 'value',
87-
title: 'This is Foo parameter.'
88-
}).optional(),
43+
foo: z.string().register(z.globalRegistry, { custom: 'value', title: 'This is Foo parameter.' }).optional(),
8944
bar: zBar.optional(),
9045
baz: z.object({
91-
baz: z.string().register(z.globalRegistry, {
92-
custom: 'value',
93-
title: 'string'
94-
}).optional()
95-
}).register(z.globalRegistry, {
96-
custom: 'value',
97-
title: 'object'
98-
}).optional(),
99-
qux: z.iso.date().register(z.globalRegistry, {
100-
custom: 'value',
101-
title: 'string'
102-
}).optional(),
103-
quux: z.iso.datetime().register(z.globalRegistry, {
104-
custom: 'value',
105-
title: 'string'
106-
}).optional()
107-
}).register(z.globalRegistry, {
108-
custom: 'value',
109-
title: 'object'
110-
}).optional()
111-
}).register(z.globalRegistry, {
112-
custom: 'value',
113-
title: 'object'
114-
});
46+
baz: z.string().register(z.globalRegistry, { custom: 'value', title: 'string' }).optional()
47+
}).register(z.globalRegistry, { custom: 'value', title: 'object' }).optional(),
48+
qux: z.iso.date().register(z.globalRegistry, { custom: 'value', title: 'string' }).optional(),
49+
quux: z.iso.datetime().register(z.globalRegistry, { custom: 'value', title: 'string' }).optional()
50+
}).register(z.globalRegistry, { custom: 'value', title: 'object' }).optional()
51+
}).register(z.globalRegistry, { custom: 'value', title: 'object' });
11552

11653
export const zPostFooData = z.object({
11754
body: zFoo3,
118-
path: z.never().register(z.globalRegistry, {
119-
custom: 'value',
120-
title: 'never'
121-
}).optional(),
122-
query: z.never().register(z.globalRegistry, {
123-
custom: 'value',
124-
title: 'never'
125-
}).optional()
126-
}).register(z.globalRegistry, {
127-
custom: 'value',
128-
title: 'object'
129-
});
55+
path: z.never().register(z.globalRegistry, { custom: 'value', title: 'never' }).optional(),
56+
query: z.never().register(z.globalRegistry, { custom: 'value', title: 'never' }).optional()
57+
}).register(z.globalRegistry, { custom: 'value', title: 'object' });

packages/openapi-ts/src/plugins/zod/mini/walker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,11 @@ export function createVisitor(
208208
if (!metadata) {
209209
return result;
210210
}
211-
const node = $.object().pretty();
211+
const node = $.object();
212212
if (typeof metadata === 'function') {
213213
metadata({ $, node, schema });
214214
} else if (schema.description) {
215-
node.prop('description', $.literal(schema.description));
215+
node.pretty().prop('description', $.literal(schema.description));
216216
}
217217
if (node.isEmpty) {
218218
return result;

packages/openapi-ts/src/plugins/zod/v4/walker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,11 @@ export function createVisitor(
225225
if (!metadata) {
226226
return result;
227227
}
228-
const node = $.object().pretty();
228+
const node = $.object();
229229
if (typeof metadata === 'function') {
230230
metadata({ $, node, schema });
231231
} else if (schema.description) {
232-
node.prop('description', $.literal(schema.description));
232+
node.pretty().prop('description', $.literal(schema.description));
233233
}
234234
if (node.isEmpty) {
235235
return result;

0 commit comments

Comments
 (0)