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 src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export const createJsonSchemaTransformObject =
for (const key in outputSchemas) {
if (inputSchemas[key]) {
throw new Error(
`Collision detected for schema "${key}". The is already an input schema with the same name.`,
`Collision detected for schema "${key}". There already exists an input schema with the same name.`,
)
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/zod-to-json.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { $ZodDate, JSONSchema } from 'zod/v4/core'
import type { $ZodDate, $ZodUndefined, JSONSchema } from 'zod/v4/core'
import { $ZodRegistry, $ZodType, toJSONSchema } from 'zod/v4/core'

const getSchemaId = (id: string, io: 'input' | 'output') => {
Expand All @@ -13,6 +13,10 @@ function isZodDate(entity: unknown): entity is $ZodDate {
return entity instanceof $ZodType && entity._zod.def.type === 'date'
}

function isZodUndefined(entity: unknown): entity is $ZodUndefined {
return entity instanceof $ZodType && entity._zod.def.type === 'undefined'
}

const getOverride = (
ctx: {
zodSchema: $ZodType
Expand All @@ -27,18 +31,19 @@ const getOverride = (
ctx.jsonSchema.format = 'date-time'
}

if (ctx.zodSchema._zod.def.type === 'undefined') {
// Allow undefined to be represented as null in output schemas
if (isZodUndefined(ctx.zodSchema)) {
ctx.jsonSchema.type = 'null'
}
}

// ToDo should be unnecessary after https://github.com/colinhacks/zod/pull/4811 is released
// TODO: should be unnecessary after https://github.com/colinhacks/zod/pull/4811 is released
// Remove propertyNames from record schemas
if (ctx.jsonSchema.propertyNames) {
delete ctx.jsonSchema.propertyNames
}

// ToDo should be unnecessary after https://github.com/colinhacks/zod/pull/4811 is released
// TODO: should be unnecessary after https://github.com/colinhacks/zod/pull/4811 is released
// Transform anyOf with type: null to nullable: true
if (ctx.jsonSchema.anyOf && ctx.jsonSchema.anyOf.some((s) => s.type === 'null')) {
ctx.jsonSchema.type = ctx.jsonSchema.anyOf.find((s) => s.type !== 'null')?.type
Expand All @@ -55,7 +60,7 @@ const deleteInvalidProperties: (
delete object.id
delete object.$schema

// ToDo added in newer zod
// TODO: added in newer zod
delete object.$id

return object
Expand Down
Loading