Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/fix-bigint-json-schema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"incur": patch
---

Fixed `z.bigint()`, `z.coerce.bigint()`, `z.date()`, and `z.coerce.date()` schemas failing during skill sync by representing them as `{ type: "string" }` in JSON Schema output.
29 changes: 29 additions & 0 deletions src/Schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,35 @@ describe('toJsonSchema', () => {
})
})

test('converts z.bigint() as string', () => {
expect(Schema.toJsonSchema(z.bigint())).toEqual({ type: 'string' })
})

test('converts z.coerce.bigint() as string', () => {
expect(Schema.toJsonSchema(z.coerce.bigint())).toEqual({ type: 'string' })
})

test('converts z.object() with bigint field', () => {
expect(
Schema.toJsonSchema(z.object({ amount: z.coerce.bigint().describe('Token amount') })),
).toEqual({
type: 'object',
properties: {
amount: { type: 'string', description: 'Token amount' },
},
required: ['amount'],
additionalProperties: false,
})
})

test('converts z.date() as string', () => {
expect(Schema.toJsonSchema(z.date())).toEqual({ type: 'string' })
})

test('converts z.coerce.date() as string', () => {
expect(Schema.toJsonSchema(z.coerce.date())).toEqual({ type: 'string' })
})

test('full object with optional, default, and describe', () => {
const result = Schema.toJsonSchema(
z.object({
Expand Down
14 changes: 12 additions & 2 deletions src/Schema.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { z } from 'zod'

/** Converts a Zod schema to a JSON Schema object. Strips the `$schema` meta-property. */
/**
* Converts a Zod schema to a JSON Schema object. Strips the `$schema`
* meta-property. Represents bigints and dates as `{ type: "string" }`
* since JSON lacks native types for them.
*/
export function toJsonSchema(schema: z.ZodType): Record<string, unknown> {
const result = z.toJSONSchema(schema) as Record<string, unknown>
const result = z.toJSONSchema(schema, {
unrepresentable: 'any',
override: (ctx) => {
const type = ctx.zodSchema._zod?.def?.type
if (type === 'bigint' || type === 'date') ctx.jsonSchema.type = 'string'
},
}) as Record<string, unknown>
delete result.$schema
return result
}
Loading