Skip to content

Commit

Permalink
Merge pull request #95 from asteasolutions/feature/#94-include-suppor…
Browse files Browse the repository at this point in the history
…t-for-zod-describe

#94 Added support for `zod`'s `.describe` method
  • Loading branch information
AGalabov authored Feb 28, 2023
2 parents 90e298d + b91cd8a commit 395872d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
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

0 comments on commit 395872d

Please sign in to comment.