Skip to content

Commit 8be955f

Browse files
authored
feat(zod): place null at the end when converting ZodNullable (#672)
Currently, the Zod converter transforms nullable types into schemas in the order of [null, json]. Because null appears as the first value in the array, UIs like Swagger or Scalar display null as the example value. ```json { "nullableProperty": null } ``` Although it is possible to customize the example using oz.openapi, I believe it is more reasonable to show a non-null default value as the example. ```json { "nullableProperty": "non-null example" } ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Refactor** - Updated the order of types in nullable schemas to prioritize the main type before null. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent e05ad8a commit 8be955f

2 files changed

Lines changed: 2 additions & 2 deletions

File tree

packages/zod/src/converter.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ const processedCases: SchemaTestCase[] = [
286286
},
287287
{
288288
schema: z.string().nullable(),
289-
input: [true, { anyOf: [{ type: 'null' }, { type: 'string' }] }],
289+
input: [true, { anyOf: [{ type: 'string' }, { type: 'null' }] }],
290290
ignoreZodToJsonSchema: true,
291291
},
292292
{

packages/zod/src/converter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ export class ZodToJsonSchemaConverter implements ConditionalSchemaConverter {
610610

611611
const [required, json] = this.convert(schema_._def.innerType, options, lazyDepth, false, false, structureDepth)
612612

613-
return [required, { anyOf: [{ type: 'null' }, json] }]
613+
return [required, { anyOf: [json, { type: 'null' }] }]
614614
}
615615
}
616616

0 commit comments

Comments
 (0)