Skip to content

Commit ad89892

Browse files
RobertCraigiestainless-app[bot]
authored andcommitted
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
1 parent d8a80a9 commit ad89892

File tree

4 files changed

+14
-21
lines changed

4 files changed

+14
-21
lines changed

src/_vendor/zod-to-json-schema/Options.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export type Options<Target extends Targets = 'jsonSchema7'> = {
1717
dateStrategy: DateStrategy | DateStrategy[];
1818
mapStrategy: 'entries' | 'record';
1919
removeAdditionalStrategy: 'passthrough' | 'strict';
20+
nullableStrategy: 'from-target' | 'property';
2021
target: Target;
2122
strictUnions: boolean;
2223
definitionPath: string;
@@ -45,6 +46,7 @@ export const defaultOptions: Options = {
4546
pipeStrategy: 'all',
4647
dateStrategy: 'format:date-time',
4748
mapStrategy: 'entries',
49+
nullableStrategy: 'from-target',
4850
removeAdditionalStrategy: 'passthrough',
4951
definitionPath: 'definitions',
5052
target: 'jsonSchema7',

src/_vendor/zod-to-json-schema/parsers/nullable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function parseNullableDef(def: ZodNullableDef, refs: Refs): JsonSchema7Nu
1717
['ZodString', 'ZodNumber', 'ZodBigInt', 'ZodBoolean', 'ZodNull'].includes(def.innerType._def.typeName) &&
1818
(!def.innerType._def.checks || !def.innerType._def.checks.length)
1919
) {
20-
if (refs.target === 'openApi3') {
20+
if (refs.target === 'openApi3' || refs.nullableStrategy === 'property') {
2121
return {
2222
type: primitiveMappings[def.innerType._def.typeName as keyof typeof primitiveMappings],
2323
nullable: true,

src/helpers/zod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function zodToJsonSchema(schema: z.ZodType, options: { name: string }): Record<s
1414
name: options.name,
1515
nameStrategy: 'duplicate-ref',
1616
$refStrategy: 'extract-to-root',
17+
nullableStrategy: 'property',
1718
});
1819
}
1920

tests/lib/parser.test.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -307,19 +307,15 @@ describe('.parse()', () => {
307307
"properties": {
308308
"description": {
309309
"description": "Open text for any other relevant information about what the contact does.",
310-
"type": [
311-
"string",
312-
"null",
313-
],
310+
"nullable": true,
311+
"type": "string",
314312
},
315313
"name": {
316314
"type": "string",
317315
},
318316
"phone_number": {
319-
"type": [
320-
"string",
321-
"null",
322-
],
317+
"nullable": true,
318+
"type": "string",
323319
},
324320
"roles": {
325321
"description": "Any roles for which the contact is important, use other for custom roles",
@@ -376,10 +372,8 @@ describe('.parse()', () => {
376372
"type": "string",
377373
},
378374
"contactPerson_properties_person1_properties_phone_number": {
379-
"type": [
380-
"string",
381-
"null",
382-
],
375+
"nullable": true,
376+
"type": "string",
383377
},
384378
},
385379
"properties": {
@@ -388,19 +382,15 @@ describe('.parse()', () => {
388382
"properties": {
389383
"description": {
390384
"description": "Open text for any other relevant information about what the contact does.",
391-
"type": [
392-
"string",
393-
"null",
394-
],
385+
"nullable": true,
386+
"type": "string",
395387
},
396388
"name": {
397389
"type": "string",
398390
},
399391
"phone_number": {
400-
"type": [
401-
"string",
402-
"null",
403-
],
392+
"nullable": true,
393+
"type": "string",
404394
},
405395
"roles": {
406396
"description": "Any roles for which the contact is important, use other for custom roles",

0 commit comments

Comments
 (0)