Skip to content

Commit

Permalink
Merge pull request #281 from asteasolutions/fix/#271-handle-required-…
Browse files Browse the repository at this point in the history
…for-custom-and-instnaceof

#271 handle instance of and custom required fields
  • Loading branch information
AGalabov authored Dec 6, 2024
2 parents 1160af8 + 3713bd7 commit 95680eb
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 5 deletions.
48 changes: 48 additions & 0 deletions spec/modifiers/custom.spec.ts
Original file line number Diff line number Diff line change
@@ -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'],
},
});
});
});
46 changes: 46 additions & 0 deletions spec/modifiers/instanceof.spec.ts
Original file line number Diff line number Diff line change
@@ -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'],
},
});
});
});
2 changes: 1 addition & 1 deletion spec/modifiers/refine.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
],
Expand Down
4 changes: 0 additions & 4 deletions src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 95680eb

Please sign in to comment.