-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from asteasolutions/split-test-files
Split huge spec file into separate ones for clarity
- Loading branch information
Showing
20 changed files
with
1,088 additions
and
1,034 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,70 @@ | ||
import { z } from 'zod'; | ||
import { expectSchema } from './lib/helpers'; | ||
|
||
describe('metadata overrides', () => { | ||
it.todo( | ||
'throws error for openapi data to be provided for unrecognized literal types' | ||
); | ||
|
||
it.todo( | ||
'throws error for openapi data to be provided for unrecognized enum types' | ||
); | ||
|
||
it('does not infer the type if one is provided using .openapi', () => { | ||
expectSchema( | ||
[z.string().openapi({ type: 'number', refId: 'StringAsNumber' })], | ||
{ | ||
StringAsNumber: { type: 'number' }, | ||
} | ||
); | ||
}); | ||
|
||
it('can remove .openapi properties', () => { | ||
expectSchema( | ||
[ | ||
z | ||
.string() | ||
.openapi({ refId: 'Test', description: 'test', deprecated: true }) | ||
.openapi({ description: undefined, deprecated: undefined }), | ||
], | ||
{ | ||
Test: { type: 'string' }, | ||
} | ||
); | ||
}); | ||
|
||
it('generates schemas with metadata', () => { | ||
expectSchema( | ||
[z.string().openapi({ refId: 'SimpleString', description: 'test' })], | ||
{ SimpleString: { type: 'string', description: 'test' } } | ||
); | ||
}); | ||
|
||
it('supports .openapi for registered schemas', () => { | ||
const StringSchema = z.string().openapi({ refId: 'String' }); | ||
|
||
const TestSchema = z | ||
.object({ | ||
key: StringSchema.openapi({ example: 'test', deprecated: true }), | ||
}) | ||
.openapi({ refId: 'Test' }); | ||
|
||
expectSchema([StringSchema, TestSchema], { | ||
String: { | ||
type: 'string', | ||
}, | ||
Test: { | ||
type: 'object', | ||
properties: { | ||
key: { | ||
allOf: [ | ||
{ $ref: '#/components/schemas/String' }, | ||
{ example: 'test', deprecated: true }, | ||
], | ||
}, | ||
}, | ||
required: ['key'], | ||
}, | ||
}); | ||
}); | ||
}); |
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,111 @@ | ||
import { z } from 'zod'; | ||
import { expectSchema } from '../lib/helpers'; | ||
|
||
describe('default', () => { | ||
it('supports defaults', () => { | ||
expectSchema( | ||
[z.string().default('test').openapi({ refId: 'StringWithDefault' })], | ||
{ | ||
StringWithDefault: { | ||
type: 'string', | ||
}, | ||
} | ||
); | ||
}); | ||
|
||
it('supports optional defaults', () => { | ||
expectSchema( | ||
[ | ||
z | ||
.object({ | ||
test: z.ostring().default('test'), | ||
}) | ||
.openapi({ refId: 'ObjectWithDefault' }), | ||
], | ||
{ | ||
ObjectWithDefault: { | ||
type: 'object', | ||
properties: { | ||
test: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
} | ||
); | ||
}); | ||
|
||
it('supports required defaults', () => { | ||
expectSchema( | ||
[ | ||
z | ||
.object({ | ||
test: z.string().default('test'), | ||
}) | ||
.openapi({ refId: 'ObjectWithDefault' }), | ||
], | ||
{ | ||
ObjectWithDefault: { | ||
type: 'object', | ||
properties: { | ||
test: { | ||
type: 'string', | ||
}, | ||
}, | ||
required: ['test'], | ||
}, | ||
} | ||
); | ||
}); | ||
|
||
it('supports optional default schemas with refine', () => { | ||
expectSchema( | ||
[ | ||
z | ||
.object({ | ||
test: z | ||
.onumber() | ||
.default(42) | ||
.refine(num => num && num % 2 === 0), | ||
}) | ||
.openapi({ refId: 'Object' }), | ||
], | ||
{ | ||
Object: { | ||
type: 'object', | ||
properties: { | ||
test: { | ||
type: 'number', | ||
}, | ||
}, | ||
}, | ||
} | ||
); | ||
}); | ||
|
||
it('supports required default schemas with refine', () => { | ||
expectSchema( | ||
[ | ||
z | ||
.object({ | ||
test: z | ||
.number() | ||
.default(42) | ||
.refine(num => num && num % 2 === 0), | ||
}) | ||
.openapi({ refId: 'Object' }), | ||
], | ||
{ | ||
Object: { | ||
type: 'object', | ||
properties: { | ||
test: { | ||
type: 'number', | ||
}, | ||
}, | ||
required: ['test'], | ||
}, | ||
} | ||
); | ||
}); | ||
}); |
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,36 @@ | ||
import { z } from 'zod'; | ||
import { expectSchema } from '../lib/helpers'; | ||
|
||
describe('nullable', () => { | ||
it('supports nullable', () => { | ||
expectSchema([z.string().nullable().openapi({ refId: 'NullableString' })], { | ||
NullableString: { type: 'string', nullable: true }, | ||
}); | ||
}); | ||
|
||
it('supports nullable for registered schemas', () => { | ||
const StringSchema = z.string().openapi({ refId: 'String' }); | ||
|
||
const TestSchema = z | ||
.object({ key: StringSchema.nullable() }) | ||
.openapi({ refId: 'Test' }); | ||
|
||
expectSchema([StringSchema, TestSchema], { | ||
String: { | ||
type: 'string', | ||
}, | ||
Test: { | ||
type: 'object', | ||
properties: { | ||
key: { | ||
allOf: [ | ||
{ $ref: '#/components/schemas/String' }, | ||
{ nullable: true }, | ||
], | ||
}, | ||
}, | ||
required: ['key'], | ||
}, | ||
}); | ||
}); | ||
}); |
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,16 @@ | ||
import { z } from 'zod'; | ||
import { expectSchema } from '../lib/helpers'; | ||
|
||
describe('optional', () => { | ||
it('generates OpenAPI schema for optional after the metadata', () => { | ||
expectSchema([z.string().openapi({ refId: 'SimpleString' }).optional()], { | ||
SimpleString: { type: 'string' }, | ||
}); | ||
}); | ||
|
||
it('generates OpenAPI schema for optional before the metadata', () => { | ||
expectSchema([z.string().optional().openapi({ refId: 'SimpleString' })], { | ||
SimpleString: { type: 'string' }, | ||
}); | ||
}); | ||
}); |
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,55 @@ | ||
import { z } from 'zod'; | ||
import { expectSchema } from '../lib/helpers'; | ||
|
||
describe('preprocess', () => { | ||
it('supports preprocessed string -> boolean schema', () => { | ||
expectSchema( | ||
[ | ||
z | ||
.preprocess(arg => { | ||
if (typeof arg === 'boolean') { | ||
return arg; | ||
} | ||
|
||
if (typeof arg === 'string') { | ||
if (arg === 'true') return true; | ||
if (arg === 'false') return false; | ||
} | ||
|
||
return undefined; | ||
}, z.boolean()) | ||
.openapi({ refId: 'PreprocessedBoolean' }), | ||
], | ||
{ | ||
PreprocessedBoolean: { | ||
type: 'boolean', | ||
}, | ||
} | ||
); | ||
}); | ||
|
||
it('supports preprocessed string -> number schema', () => { | ||
expectSchema( | ||
[ | ||
z | ||
.preprocess(arg => { | ||
if (typeof arg === 'number') { | ||
return arg; | ||
} | ||
|
||
if (typeof arg === 'string') { | ||
return parseInt(arg, 10); | ||
} | ||
|
||
return undefined; | ||
}, z.number()) | ||
.openapi({ refId: 'PreprocessedNumber' }), | ||
], | ||
{ | ||
PreprocessedNumber: { | ||
type: 'number', | ||
}, | ||
} | ||
); | ||
}); | ||
}); |
Oops, something went wrong.