Skip to content

Commit

Permalink
Merge pull request #47 from asteasolutions/split-test-files
Browse files Browse the repository at this point in the history
Split huge spec file into separate ones for clarity
  • Loading branch information
georgyangelov authored Oct 12, 2022
2 parents 618123c + e6de37e commit 6ae1420
Show file tree
Hide file tree
Showing 20 changed files with 1,088 additions and 1,034 deletions.
70 changes: 70 additions & 0 deletions spec/medatada-overrides.spec.ts
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'],
},
});
});
});
111 changes: 111 additions & 0 deletions spec/modifiers/default.spec.ts
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'],
},
}
);
});
});
36 changes: 36 additions & 0 deletions spec/modifiers/nullable.spec.ts
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'],
},
});
});
});
16 changes: 16 additions & 0 deletions spec/modifiers/optional.spec.ts
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' },
});
});
});
55 changes: 55 additions & 0 deletions spec/modifiers/preprocess.spec.ts
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',
},
}
);
});
});
Loading

0 comments on commit 6ae1420

Please sign in to comment.