-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
95 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters