From ad89892f4ac0daba161ce97267a165a12f67c341 Mon Sep 17 00:00:00 2001 From: Robert Craigie Date: Thu, 8 Aug 2024 15:25:59 +0100 Subject: [PATCH] fix(helpers/zod): add `nullableStrategy` option so we can generate `nullable: true` instead of `type: ["foo", "null"]` and avoid having to change the `target` json schema version as we're in a weird in the middle state --- src/_vendor/zod-to-json-schema/Options.ts | 2 ++ .../zod-to-json-schema/parsers/nullable.ts | 2 +- src/helpers/zod.ts | 1 + tests/lib/parser.test.ts | 30 +++++++------------ 4 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/_vendor/zod-to-json-schema/Options.ts b/src/_vendor/zod-to-json-schema/Options.ts index e765eef78..a83690e59 100644 --- a/src/_vendor/zod-to-json-schema/Options.ts +++ b/src/_vendor/zod-to-json-schema/Options.ts @@ -17,6 +17,7 @@ export type Options = { dateStrategy: DateStrategy | DateStrategy[]; mapStrategy: 'entries' | 'record'; removeAdditionalStrategy: 'passthrough' | 'strict'; + nullableStrategy: 'from-target' | 'property'; target: Target; strictUnions: boolean; definitionPath: string; @@ -45,6 +46,7 @@ export const defaultOptions: Options = { pipeStrategy: 'all', dateStrategy: 'format:date-time', mapStrategy: 'entries', + nullableStrategy: 'from-target', removeAdditionalStrategy: 'passthrough', definitionPath: 'definitions', target: 'jsonSchema7', diff --git a/src/_vendor/zod-to-json-schema/parsers/nullable.ts b/src/_vendor/zod-to-json-schema/parsers/nullable.ts index efb70076e..0d7063610 100644 --- a/src/_vendor/zod-to-json-schema/parsers/nullable.ts +++ b/src/_vendor/zod-to-json-schema/parsers/nullable.ts @@ -17,7 +17,7 @@ export function parseNullableDef(def: ZodNullableDef, refs: Refs): JsonSchema7Nu ['ZodString', 'ZodNumber', 'ZodBigInt', 'ZodBoolean', 'ZodNull'].includes(def.innerType._def.typeName) && (!def.innerType._def.checks || !def.innerType._def.checks.length) ) { - if (refs.target === 'openApi3') { + if (refs.target === 'openApi3' || refs.nullableStrategy === 'property') { return { type: primitiveMappings[def.innerType._def.typeName as keyof typeof primitiveMappings], nullable: true, diff --git a/src/helpers/zod.ts b/src/helpers/zod.ts index aa09ffaac..1946b2199 100644 --- a/src/helpers/zod.ts +++ b/src/helpers/zod.ts @@ -14,6 +14,7 @@ function zodToJsonSchema(schema: z.ZodType, options: { name: string }): Record { "properties": { "description": { "description": "Open text for any other relevant information about what the contact does.", - "type": [ - "string", - "null", - ], + "nullable": true, + "type": "string", }, "name": { "type": "string", }, "phone_number": { - "type": [ - "string", - "null", - ], + "nullable": true, + "type": "string", }, "roles": { "description": "Any roles for which the contact is important, use other for custom roles", @@ -376,10 +372,8 @@ describe('.parse()', () => { "type": "string", }, "contactPerson_properties_person1_properties_phone_number": { - "type": [ - "string", - "null", - ], + "nullable": true, + "type": "string", }, }, "properties": { @@ -388,19 +382,15 @@ describe('.parse()', () => { "properties": { "description": { "description": "Open text for any other relevant information about what the contact does.", - "type": [ - "string", - "null", - ], + "nullable": true, + "type": "string", }, "name": { "type": "string", }, "phone_number": { - "type": [ - "string", - "null", - ], + "nullable": true, + "type": "string", }, "roles": { "description": "Any roles for which the contact is important, use other for custom roles",