Skip to content
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

#94 Added support for zod's .describe method #95

Merged
merged 1 commit into from
Feb 28, 2023
Merged
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
43 changes: 43 additions & 0 deletions spec/modifiers/describe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { z } from 'zod';
import { expectSchema, registerSchema } from '../lib/helpers';

describe('describe', () => {
it('generates OpenAPI schema with description when the .describe method is used', () => {
const schema = registerSchema(
'SimpleString',
z.string().describe('This is a test string')
);

expectSchema([schema], {
SimpleString: { type: 'string', description: 'This is a test string' },
});
});

it('can overload .openapi description when the .describe method is used', () => {
const schema = registerSchema(
'SimpleString',
z
.string()
.openapi({ description: 'Alternative description' })
.describe('This is a test string')
);

expectSchema([schema], {
SimpleString: { type: 'string', description: 'This is a test string' },
});
});

it('can overload descriptions from .describe with .openapi', () => {
const schema = registerSchema(
'SimpleString',
z
.string()
.describe('This is a test string')
.openapi({ description: 'Alternative description' })
);

expectSchema([schema], {
SimpleString: { type: 'string', description: 'Alternative description' },
});
});
});
8 changes: 8 additions & 0 deletions src/zod-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ export function extendZodWithOpenApi(zod: typeof z) {
return result;
};

const zodDescribe = zod.ZodSchema.prototype.describe;
zod.ZodSchema.prototype.describe = function (this: any, ...args: [string]) {
const [description] = args;
const result = zodDescribe.apply(this, args).openapi({ description });

return result;
};

const zodPick = zod.ZodObject.prototype.pick as any;
zod.ZodObject.prototype.pick = function (this: any, ...args: any[]) {
const result = zodPick.apply(this, args);
Expand Down