diff --git a/spec/modifiers/deepPartial.spec.ts b/spec/modifiers/deepPartial.spec.ts new file mode 100644 index 0000000..9871c78 --- /dev/null +++ b/spec/modifiers/deepPartial.spec.ts @@ -0,0 +1,109 @@ +import { z } from 'zod'; +import { expectSchema, generateDataForRoute } from '../lib/helpers'; + +describe('describe', () => { + it('generates a deepPartial object', () => { + const schema = z.object({ + a: z.string(), + b: z.number(), + }); + + const { requestBody } = generateDataForRoute({ + request: { + body: { + description: 'Test description', + required: true, + content: { + 'application/json': { + schema: schema.deepPartial(), + }, + }, + }, + }, + }); + + expect(requestBody).toEqual({ + description: 'Test description', + required: true, + content: { + 'application/json': { + schema: { + type: 'object', + properties: { + a: { type: 'string' }, + b: { type: 'number' }, + }, + }, + }, + }, + }); + }); + + it('generates a deepPartial object from a registered schema', () => { + const schema = z + .object({ + a: z.string(), + b: z.number(), + }) + .openapi('TestSchema'); + + const { documentSchemas, requestBody, responses } = generateDataForRoute({ + request: { + body: { + description: 'Test description', + required: true, + content: { + 'application/json': { + schema: schema.deepPartial(), + }, + }, + }, + }, + responses: { + 200: { + description: 'Response description', + content: { + 'application/json': { schema }, + }, + }, + }, + }); + + // The schema is registered + expect(documentSchemas).toEqual({ + TestSchema: { + type: 'object', + properties: { + a: { type: 'string' }, + b: { type: 'number' }, + }, + required: ['a', 'b'], + }, + }); + + expect(responses[200]).toEqual({ + description: 'Response description', + content: { + 'application/json': { + schema: { $ref: '#/components/schemas/TestSchema' }, + }, + }, + }); + + expect(requestBody).toEqual({ + description: 'Test description', + required: true, + content: { + 'application/json': { + schema: { + type: 'object', + properties: { + a: { type: 'string' }, + b: { type: 'number' }, + }, + }, + }, + }, + }); + }); +}); diff --git a/src/zod-extensions.ts b/src/zod-extensions.ts index 210728c..65aac7a 100644 --- a/src/zod-extensions.ts +++ b/src/zod-extensions.ts @@ -160,6 +160,8 @@ export function extendZodWithOpenApi(zod: typeof z) { value._def.openapi = initialShape[key]?._def?.openapi; }); + result._def.openapi = undefined; + return result; };