Skip to content

Commit cad3c6d

Browse files
committed
add more tests, adjust catch logic
1 parent 74bf7e1 commit cad3c6d

File tree

2 files changed

+154
-49
lines changed

2 files changed

+154
-49
lines changed

src/create/schema/parsers/catch.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ZodCatch, ZodTypeAny } from 'zod';
22

3+
import type { oas31 } from '../../../../dist';
34
import {
45
type Schema,
56
type SchemaState,
@@ -15,7 +16,16 @@ export const createCatchSchema = <T extends ZodTypeAny>(
1516
'default',
1617
]);
1718

19+
const catchResult = zodCatch.safeParse(undefined);
20+
21+
const maybeDefaultValue: Pick<oas31.SchemaObject, 'default'> | undefined =
22+
catchResult.success
23+
? {
24+
default: catchResult.data,
25+
}
26+
: undefined;
27+
1828
return enhanceWithMetadata(schemaObject, {
19-
default: zodCatch.parse(undefined),
29+
...maybeDefaultValue,
2030
});
2131
};

src/create/schema/parsers/object.test.ts

Lines changed: 143 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -189,65 +189,160 @@ describe('createObjectSchema', () => {
189189
});
190190

191191
describe('required', () => {
192-
it('creates a required array containing all properties', () => {
193-
const schema = z.object({
194-
a: z.string(),
195-
b: z.string().nullable(),
196-
c: z.number(),
197-
d: z.literal(null),
198-
e: z.union([z.string(), z.number()]),
192+
describe('output', () => {
193+
it('creates a required array containing all properties', () => {
194+
const schema = z.object({
195+
a: z.string(),
196+
b: z.string().nullable(),
197+
c: z.number(),
198+
d: z.literal(null),
199+
e: z.union([z.string(), z.number()]),
200+
f: z.custom((r) => r !== undefined),
201+
g: z.string().default('a'),
202+
h: z.string().catch('a'),
203+
});
204+
205+
const result = createObjectSchema(schema, createOutputState());
206+
207+
expect(result).toEqual<Schema>({
208+
effects: expect.any(Array),
209+
type: 'schema',
210+
schema: {
211+
type: 'object',
212+
properties: {
213+
a: { type: 'string' },
214+
b: { type: ['string', 'null'] },
215+
c: { type: 'number' },
216+
d: { type: 'null' },
217+
e: { anyOf: [{ type: 'string' }, { type: 'number' }] },
218+
f: {},
219+
g: { type: 'string', default: 'a' },
220+
h: { type: 'string', default: 'a' },
221+
},
222+
required: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
223+
},
224+
});
199225
});
200226

201-
const result = createObjectSchema(schema, createOutputState());
227+
it('does not create an required array', () => {
228+
const ref = z.string().openapi({ ref: 'ref' });
229+
const oref = z.string().optional().openapi({ ref: 'oref' });
230+
const schema = z.object({
231+
a: z.literal(undefined),
232+
b: z.never(),
233+
c: z.undefined(),
234+
d: z.string().optional(),
235+
e: z.string().nullish(),
236+
f: z.number().optional(),
237+
g: z.union([z.string(), z.undefined()]),
238+
h: z.union([z.string(), z.number().optional()]),
239+
i: ref.optional(),
240+
j: oref,
241+
k: z.custom<string | undefined>(),
242+
l: z
243+
.string()
244+
.optional()
245+
.transform((str) => str?.length)
246+
.pipe(z.number().optional()),
247+
});
202248

203-
expect(result).toEqual<Schema>({
204-
type: 'schema',
205-
schema: {
206-
type: 'object',
207-
properties: {
208-
a: { type: 'string' },
209-
b: { type: ['string', 'null'] },
210-
c: { type: 'number' },
211-
d: { type: 'null' },
212-
e: { anyOf: [{ type: 'string' }, { type: 'number' }] },
249+
const result = createObjectSchema(schema, createOutputState());
250+
251+
expect(result).toEqual<Schema>({
252+
effects: expect.any(Array),
253+
type: 'schema',
254+
schema: {
255+
type: 'object',
256+
properties: {
257+
d: { type: 'string' },
258+
e: { type: ['string', 'null'] },
259+
f: { type: 'number' },
260+
g: { anyOf: [{ type: 'string' }] },
261+
h: { anyOf: [{ type: 'string' }, { type: 'number' }] },
262+
i: { $ref: '#/components/schemas/ref' },
263+
j: { $ref: '#/components/schemas/oref' },
264+
k: {},
265+
l: { type: 'number' },
266+
},
213267
},
214-
required: ['a', 'b', 'c', 'd', 'e'],
215-
},
268+
});
216269
});
217270
});
218271

219-
it('does not create an required array', () => {
220-
const ref = z.string().openapi({ ref: 'ref' });
221-
const oref = z.string().optional().openapi({ ref: 'oref' });
222-
const schema = z.object({
223-
a: z.literal(undefined),
224-
b: z.never(),
225-
c: z.undefined(),
226-
d: z.string().optional(),
227-
e: z.string().nullish(),
228-
f: z.number().optional(),
229-
g: z.union([z.string(), z.undefined()]),
230-
h: z.union([z.string(), z.number().optional()]),
231-
i: ref.optional(),
232-
j: oref,
272+
describe('input', () => {
273+
it('creates a required array containing all properties', () => {
274+
const schema = z.object({
275+
a: z.string(),
276+
b: z.string().nullable(),
277+
c: z.number(),
278+
d: z.literal(null),
279+
e: z.union([z.string(), z.number()]),
280+
f: z.custom((r) => r !== undefined),
281+
});
282+
283+
const result = createObjectSchema(schema, createInputState());
284+
285+
expect(result).toEqual<Schema>({
286+
type: 'schema',
287+
schema: {
288+
type: 'object',
289+
properties: {
290+
a: { type: 'string' },
291+
b: { type: ['string', 'null'] },
292+
c: { type: 'number' },
293+
d: { type: 'null' },
294+
e: { anyOf: [{ type: 'string' }, { type: 'number' }] },
295+
f: {},
296+
},
297+
required: ['a', 'b', 'c', 'd', 'e', 'f'],
298+
},
299+
});
233300
});
234301

235-
const result = createObjectSchema(schema, createOutputState());
302+
it('does not create an required array', () => {
303+
const ref = z.string().openapi({ ref: 'ref' });
304+
const oref = z.string().optional().openapi({ ref: 'oref' });
305+
const schema = z.object({
306+
a: z.literal(undefined),
307+
b: z.never(),
308+
c: z.undefined(),
309+
d: z.string().optional(),
310+
e: z.string().nullish(),
311+
f: z.number().optional(),
312+
g: z.union([z.string(), z.undefined()]),
313+
h: z.union([z.string(), z.number().optional()]),
314+
i: ref.optional(),
315+
j: oref,
316+
k: z.custom<string | undefined>(),
317+
l: z
318+
.string()
319+
.optional()
320+
.transform((str) => str?.length)
321+
.pipe(z.number().optional()),
322+
m: z.string().default('a'),
323+
});
236324

237-
expect(result).toEqual<Schema>({
238-
type: 'schema',
239-
schema: {
240-
type: 'object',
241-
properties: {
242-
d: { type: 'string' },
243-
e: { type: ['string', 'null'] },
244-
f: { type: 'number' },
245-
g: { anyOf: [{ type: 'string' }] },
246-
h: { anyOf: [{ type: 'string' }, { type: 'number' }] },
247-
i: { $ref: '#/components/schemas/ref' },
248-
j: { $ref: '#/components/schemas/oref' },
325+
const result = createObjectSchema(schema, createInputState());
326+
327+
expect(result).toEqual<Schema>({
328+
effects: expect.any(Array),
329+
type: 'schema',
330+
schema: {
331+
type: 'object',
332+
properties: {
333+
d: { type: 'string' },
334+
e: { type: ['string', 'null'] },
335+
f: { type: 'number' },
336+
g: { anyOf: [{ type: 'string' }] },
337+
h: { anyOf: [{ type: 'string' }, { type: 'number' }] },
338+
i: { $ref: '#/components/schemas/ref' },
339+
j: { $ref: '#/components/schemas/oref' },
340+
k: {},
341+
l: { type: 'string' },
342+
m: { type: 'string', default: 'a' },
343+
},
249344
},
250-
},
345+
});
251346
});
252347
});
253348
});

0 commit comments

Comments
 (0)