Skip to content

Parse frontmatter in Zod descriptions #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
91 changes: 83 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/zod-openapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
"swagger"
],
"dependencies": {
"dedent": "^1.5.1",
"front-matter": "^4.0.2",
"ts-deepmerge": "^6.0.3"
},
"peerDependencies": {
"zod": "^3.20.0",
"openapi3-ts": "^4.1.2"
"openapi3-ts": "^4.1.2",
"zod": "^3.20.0"
}
}
109 changes: 72 additions & 37 deletions packages/zod-openapi/src/lib/zod-openapi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,34 @@ describe('zodOpenapi', () => {
expect((apiSchema.properties?.aNever as SchemaObject).readOnly).toEqual(
true
);
})
});

it('should support branded types', () => {
const zodSchema = extendApi(
z.object({
aBrandedString: z.string().describe('A branded test string').brand('BrandedString').optional(),
aBrandedString: z
.string()
.describe('A branded test string')
.brand('BrandedString')
.optional(),
aBrandedNumber: z.number().brand('BrandedNumber').optional(),
aBrandedBigInt: z.bigint().brand('BrandedBigInt'),
aBrandedBoolean: z.boolean().brand('BrandedBoolean'),
aBrandedDate: z.date().brand('BrandedDate'),
}),
{
description: `Branded primitives`
description: `Branded primitives`,
}
);
const apiSchema = generateSchema(zodSchema);

expect(apiSchema).toEqual({
type: 'object',
properties: {
aBrandedString: { description: 'A branded test string', type: 'string' },
aBrandedString: {
description: 'A branded test string',
type: 'string',
},
aBrandedNumber: { type: 'number' },
aBrandedBigInt: { type: 'integer', format: 'int64' },
aBrandedBoolean: { type: 'boolean' },
Expand Down Expand Up @@ -284,7 +291,7 @@ describe('zodOpenapi', () => {
items: { type: 'number' },
},
},
"hideDefinitions": ["aArrayLength"],
hideDefinitions: ['aArrayLength'],
description: 'I need arrays',
});
});
Expand All @@ -293,12 +300,12 @@ describe('zodOpenapi', () => {
const zodNestedSchema = extendApi(
z.strictObject({
aString: z.string(),
aNumber: z.number().optional()
aNumber: z.number().optional(),
}),
{
hideDefinitions: ['aNumber']
hideDefinitions: ['aNumber'],
}
)
);
const zodSchema = extendApi(
z.object({ data: zodNestedSchema }).partial(),
{
Expand All @@ -307,25 +314,22 @@ describe('zodOpenapi', () => {
);
const apiSchema = generateSchema(zodSchema);
expect(apiSchema).toEqual({
"description": "I need arrays",
"properties": {
"data": {
"additionalProperties": false,
"properties": {
"aString": {
"type": "string",
description: 'I need arrays',
properties: {
data: {
additionalProperties: false,
properties: {
aString: {
type: 'string',
},
},
"hideDefinitions": ["aNumber"],
"required": [
"aString",
],
"type": "object",
hideDefinitions: ['aNumber'],
required: ['aString'],
type: 'object',
},
},
"type": "object",
}
);
type: 'object',
});
});
});

Expand Down Expand Up @@ -501,7 +505,7 @@ describe('zodOpenapi', () => {
})
.strict(),
{
description: "Super strict",
description: 'Super strict',
}
);
const apiSchema = generateSchema(zodSchema);
Expand All @@ -513,7 +517,7 @@ describe('zodOpenapi', () => {
aNumber: { type: 'number' },
},
additionalProperties: false,
description: "Super strict",
description: 'Super strict',
});
});

Expand Down Expand Up @@ -904,33 +908,35 @@ describe('zodOpenapi', () => {
it('can summarize unions of zod literals as an enum', () => {
expect(generateSchema(z.union([z.literal('h'), z.literal('i')]))).toEqual({
type: 'string',
enum: ['h', 'i']
enum: ['h', 'i'],
});

expect(generateSchema(z.union([z.literal(3), z.literal(4)]))).toEqual({
type: 'number',
enum: [3, 4]
enum: [3, 4],
});

// should this just remove the enum? true | false is exhaustive...
expect(generateSchema(z.union([z.literal(true), z.literal(false)]))).toEqual({
expect(
generateSchema(z.union([z.literal(true), z.literal(false)]))
).toEqual({
type: 'boolean',
enum: [true, false]
enum: [true, false],
});

expect(generateSchema(z.union([z.literal(5), z.literal('i')]))).toEqual({
oneOf: [
{
type: 'number',
enum: [5]
enum: [5],
},
{
type: 'string',
enum: ['i']
}
]
enum: ['i'],
},
],
});
})
});

it('should work with ZodPipeline', () => {
expect(
Expand Down Expand Up @@ -962,7 +968,6 @@ describe('zodOpenapi', () => {
} satisfies SchemaObject);
});


it('should work with ZodTransform and correctly set nullable and optional', () => {
type Type = string;
const schema = z.object({
Expand Down Expand Up @@ -994,9 +999,8 @@ describe('zodOpenapi', () => {
required: ['item'],
type: 'object',
});

});

test('should work with ZodReadonly', () => {
expect(generateSchema(z.object({ field: z.string() })))
.toMatchInlineSnapshot(`
Expand Down Expand Up @@ -1027,6 +1031,37 @@ describe('zodOpenapi', () => {
"type": "object",
}
`);
});

test('should parse front matter', () => {
expect(
generateSchema(
z.object({
field: z.string().describe(`
---
title: "Test"
deprecated: because reasons
---
This is a description
`),
})
)
).toMatchInlineSnapshot(`
Object {
"properties": Object {
"field": Object {
"deprecated": true,
"description": "This is a description",
"type": "string",
"x-deprecated": "because reasons",
"x-title": "Test",
},
},
"required": Array [
"field",
],
"type": "object",
}
`);
});
});
Loading