Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions packages/zod-openapi/src/lib/zod-openapi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,23 @@ describe('zodOpenapi', () => {
description: `Empty types in a schema`,
}
);
const apiSchema = generateSchema(zodSchema);
let apiSchema = generateSchema(zodSchema);
expect(apiSchema).toEqual({
type: ['object'],
properties: {
aUndefined: {},
aNull: { type: ['string', 'null'], enum: ['null'] },
aNull: { type: 'null' },
aVoid: {},
},
required: ['aNull'],
description: 'Empty types in a schema',
});
apiSchema = generateSchema(zodSchema, false, '3.0');
expect(apiSchema).toEqual({
type: 'object',
properties: {
aUndefined: {},
aNull: { type: 'string', enum: [null] },
aVoid: {},
},
required: ['aNull'],
Expand Down Expand Up @@ -262,7 +273,7 @@ describe('zodOpenapi', () => {
description: 'I need arrays',
}
);
const apiSchema = generateSchema(zodSchema);
let apiSchema = generateSchema(zodSchema);
expect(apiSchema).toEqual({
type: ['object'],
properties: {
Expand All @@ -277,7 +288,7 @@ describe('zodOpenapi', () => {
aArrayNonempty: {
type: ['array'],
minItems: 1,
items: { type: ['string', 'null'], enum: ['null'] },
items: { type: 'null' },
},
aArrayMinAndMax: {
type: ['array'],
Expand All @@ -288,6 +299,32 @@ describe('zodOpenapi', () => {
},
description: 'I need arrays',
});
apiSchema = generateSchema(zodSchema, false, '3.0');
expect(apiSchema).toEqual({
type: 'object',
properties: {
aArrayMin: { type: 'array', minItems: 3, items: { type: 'string' } },
aArrayMax: { type: 'array', maxItems: 8, items: { type: 'number' } },
aArrayLength: {
type: 'array',
minItems: 10,
maxItems: 10,
items: { type: 'boolean' },
},
aArrayNonempty: {
type: 'array',
minItems: 1,
items: { type: 'string', enum: [null] },
},
aArrayMinAndMax: {
type: 'array',
minItems: 3,
maxItems: 8,
items: { type: 'number' },
},
},
description: 'I need arrays',
});
});

describe('Regarding omitted schema definitions', () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/zod-openapi/src/lib/zod-openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,10 @@ function parseDate({ zodRef, schemas, openApiVersion }: ParsingArgs<z.ZodDate>):

function parseNull({ zodRef, schemas, openApiVersion }: ParsingArgs<z.ZodNull>): SchemaObject {
return merge(
openApiVersion === '3.0' ? { type: 'null' as SchemaObjectType } : {
type: ['string', 'null'] as SchemaObjectType[],
enum: ['null'],
},
openApiVersion === '3.0' ? {
type: 'string' as SchemaObjectType,
enum: [null],
} : { type: 'null' as SchemaObjectType },
zodRef.description ? { description: zodRef.description } : {},
...schemas
);
Expand Down
Loading