Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/zod-nestjs/src/lib/create-zod-dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SchemaObject } from 'openapi3-ts/oas31';
import type { SchemaObject } from 'openapi3-ts/oas30';
import { generateSchema, OpenApiZodAny } from '@anatine/zod-openapi';
import * as z from 'zod';

Expand Down
2 changes: 1 addition & 1 deletion packages/zod-nestjs/src/lib/patch-nest-swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* https://github.com/kbkk/abitia/blob/master/packages/zod-dto/src/OpenApi/patchNestjsSwagger.ts
*/
import {generateSchema} from '@anatine/zod-openapi';
import type {SchemaObject} from 'openapi3-ts/oas31';
import type {SchemaObject} from 'openapi3-ts/oas30';

interface Type<T = any> extends Function {
new (...args: any[]): T;
Expand Down
1 change: 0 additions & 1 deletion packages/zod-openapi/src/lib/zod-extensions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ describe('Zod Extensions', () => {
const schema = z.object({
one: z.string().openapi({example: 'oneOne'}),
two: z.number(),

}).openapi({example: {one: 'oneOne', two: 42}})

const apiSchema = generateSchema(schema);
Expand Down
2 changes: 1 addition & 1 deletion packages/zod-openapi/src/lib/zod-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This code is heavily inspired by https://github.com/asteasolutions/zod-to-openap

import { extendApi } from './zod-openapi';
import {z} from "zod";
import { SchemaObject } from "openapi3-ts/oas31";
import { SchemaObject } from "openapi3-ts/oas30";
import {ZodTypeDef} from "zod/lib/types";


Expand Down
62 changes: 53 additions & 9 deletions packages/zod-openapi/src/lib/zod-openapi.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SchemaObject } from 'openapi3-ts/oas31';
import { SchemaObject } from 'openapi3-ts/oas30';
import validator from 'validator';
import { z } from 'zod';
import { generateSchema, extendApi } from './zod-openapi';
import { generateSchema, extendApi, fragmentName, generateVocabulary } from './zod-openapi';

describe('zodOpenapi', () => {
/**
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('zodOpenapi', () => {
);
const schemaIn = generateSchema(zodTransform);
expect(schemaIn.type).toEqual('string');
const schemaOut = generateSchema(zodTransform, true);
const schemaOut = generateSchema(zodTransform, {useOutput: true}) as SchemaObject;
expect(schemaOut.type).toEqual('number');
});

Expand Down Expand Up @@ -197,12 +197,12 @@ describe('zodOpenapi', () => {
aNumberMin: { type: 'number', minimum: 3 },
aNumberMax: { type: 'number', maximum: 8 },
aNumberInt: { type: 'integer' },
aNumberPositive: { type: 'number', minimum: 0, exclusiveMinimum: 0 },
aNumberPositive: { type: 'number', minimum: 0, exclusiveMinimum: true },
aNumberNonnegative: { type: 'number', minimum: 0 },
aNumberNegative: { type: 'number', maximum: 0, exclusiveMaximum: 0 },
aNumberNegative: { type: 'number', maximum: 0, exclusiveMaximum: true },
aNumberNonpositive: { type: 'number', maximum: 0 },
aNumberGt: { type: 'number', minimum: 5, exclusiveMinimum: 5 },
aNumberLt: { type: 'number', maximum: 5, exclusiveMaximum: 5 },
aNumberGt: { type: 'number', minimum: 5, exclusiveMinimum: true },
aNumberLt: { type: 'number', maximum: 5, exclusiveMaximum: true },
aNumberMultipleOf: { type: 'number', multipleOf: 2 },
},
description: 'Look mah, the horse can count higher than me!',
Expand Down Expand Up @@ -521,7 +521,7 @@ describe('zodOpenapi', () => {
.passthrough(),
});

const schemaTest = generateSchema(zodSchema, true);
const schemaTest = generateSchema(zodSchema, {useOutput: true});

expect(schemaTest).toEqual({
type: 'object',
Expand Down Expand Up @@ -853,5 +853,49 @@ describe('zodOpenapi', () => {
}
]
});
})
});

it('Ignores components out of its vocabulary', () => {
const schema = extendApi(z.string(), {
[fragmentName]: "coolstring"
});

expect(generateSchema(schema))
.toEqual({"type": "string"});
expect(generateSchema(z.object({string: schema})))
.toEqual({
"properties": {
"string": {
"type": "string"
}
},
"required": ["string"],
"type": "object"
});
});

it('Elides components in its vocabulary', () => {
const schema = extendApi(z.string(), {
[fragmentName]: "coolstring"
});

const [schemas, vocabulary] = generateVocabulary([schema]);

expect(schemas)
.toEqual({"coolstring": {"type": "string"}});

expect(generateSchema(schema, { vocabulary }))
.toEqual({"$ref": "#/components/schemas/coolstring"});

expect(generateSchema(z.object({string: schema}), { vocabulary }))
.toEqual({
"properties": {
"string": {
"$ref": "#/components/schemas/coolstring"
}
},
"required": ["string"],
"type": "object"
});
});
});
Loading