diff --git a/spec/modifiers/custom.spec.ts b/spec/modifiers/custom.spec.ts new file mode 100644 index 0000000..59547ec --- /dev/null +++ b/spec/modifiers/custom.spec.ts @@ -0,0 +1,48 @@ +import { z } from 'zod'; +import { expectSchema } from '../lib/helpers'; + +// File as a class is not available on older node versions +// so I am defining this just for testing purposes +class File {} + +describe('custom', () => { + it('generates OpenAPI schema for custom type', () => { + const FileSchema = z + .custom(target => target instanceof File) + .openapi({ + type: 'string', + format: 'binary', + }) + .openapi('File'); + + expectSchema([FileSchema], { + File: { + type: 'string', + format: 'binary', + }, + }); + }); + + it('generates OpenAPI schema for custom type in object', () => { + const FileUploadSchema = z + .object({ + file: z + .custom(target => target instanceof File) + .openapi({ + type: 'string', + format: 'binary', + }), + }) + .openapi('FileUpload'); + + expectSchema([FileUploadSchema], { + FileUpload: { + type: 'object', + properties: { + file: { type: 'string', format: 'binary' }, + }, + required: ['file'], + }, + }); + }); +}); diff --git a/spec/modifiers/instanceof.spec.ts b/spec/modifiers/instanceof.spec.ts new file mode 100644 index 0000000..7fb9763 --- /dev/null +++ b/spec/modifiers/instanceof.spec.ts @@ -0,0 +1,46 @@ +import { z } from 'zod'; +import { expectSchema } from '../lib/helpers'; + +// File as a class is not available on older node versions +// so I am defining this just for testing purposes +class File {} + +describe('instanceof', () => { + it('generates OpenAPI schema for instanceof type', () => { + const FileSchema = z + .instanceof(File) + .openapi({ + type: 'string', + format: 'binary', + }) + .openapi('File'); + + expectSchema([FileSchema], { + File: { + type: 'string', + format: 'binary', + }, + }); + }); + + it('generates OpenAPI schema for instanceof type in object', () => { + const FileUploadSchema = z + .object({ + file: z.instanceof(File).openapi({ + type: 'string', + format: 'binary', + }), + }) + .openapi('FileUpload'); + + expectSchema([FileUploadSchema], { + FileUpload: { + type: 'object', + properties: { + file: { type: 'string', format: 'binary' }, + }, + required: ['file'], + }, + }); + }); +}); diff --git a/spec/modifiers/refine.spec.ts b/spec/modifiers/refine.spec.ts index 739455c..b451200 100644 --- a/spec/modifiers/refine.spec.ts +++ b/spec/modifiers/refine.spec.ts @@ -64,7 +64,7 @@ describe('refine', () => { [ z .object({ - test: z.onumber().refine(num => num && num % 2 === 0), + test: z.onumber().refine(num => !num || num % 2 === 0), }) .openapi('ObjectWithRefinedString'), ], diff --git a/src/metadata.ts b/src/metadata.ts index 56f264c..05f1158 100644 --- a/src/metadata.ts +++ b/src/metadata.ts @@ -151,10 +151,6 @@ export class Metadata { } static isOptionalSchema(zodSchema: ZodTypeAny): boolean { - if (isZodType(zodSchema, 'ZodEffects')) { - return this.isOptionalSchema(zodSchema._def.schema); - } - return zodSchema.isOptional(); } }