Skip to content

Commit cb024ff

Browse files
committed
fix: handle bigint and date schemas in JSON Schema conversion
1 parent ca9b7b2 commit cb024ff

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"incur": patch
3+
---
4+
5+
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.

src/Schema.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,35 @@ describe('toJsonSchema', () => {
9797
})
9898
})
9999

100+
test('converts z.bigint() as string', () => {
101+
expect(Schema.toJsonSchema(z.bigint())).toEqual({ type: 'string' })
102+
})
103+
104+
test('converts z.coerce.bigint() as string', () => {
105+
expect(Schema.toJsonSchema(z.coerce.bigint())).toEqual({ type: 'string' })
106+
})
107+
108+
test('converts z.object() with bigint field', () => {
109+
expect(
110+
Schema.toJsonSchema(z.object({ amount: z.coerce.bigint().describe('Token amount') })),
111+
).toEqual({
112+
type: 'object',
113+
properties: {
114+
amount: { type: 'string', description: 'Token amount' },
115+
},
116+
required: ['amount'],
117+
additionalProperties: false,
118+
})
119+
})
120+
121+
test('converts z.date() as string', () => {
122+
expect(Schema.toJsonSchema(z.date())).toEqual({ type: 'string' })
123+
})
124+
125+
test('converts z.coerce.date() as string', () => {
126+
expect(Schema.toJsonSchema(z.coerce.date())).toEqual({ type: 'string' })
127+
})
128+
100129
test('full object with optional, default, and describe', () => {
101130
const result = Schema.toJsonSchema(
102131
z.object({

src/Schema.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
import { z } from 'zod'
22

3-
/** Converts a Zod schema to a JSON Schema object. Strips the `$schema` meta-property. */
3+
/**
4+
* Converts a Zod schema to a JSON Schema object. Strips the `$schema`
5+
* meta-property. Represents bigints and dates as `{ type: "string" }`
6+
* since JSON lacks native types for them.
7+
*/
48
export function toJsonSchema(schema: z.ZodType): Record<string, unknown> {
5-
const result = z.toJSONSchema(schema) as Record<string, unknown>
9+
const result = z.toJSONSchema(schema, {
10+
unrepresentable: 'any',
11+
override: (ctx) => {
12+
const type = ctx.zodSchema._zod?.def?.type
13+
if (type === 'bigint' || type === 'date') ctx.jsonSchema.type = 'string'
14+
},
15+
}) as Record<string, unknown>
616
delete result.$schema
717
return result
818
}

0 commit comments

Comments
 (0)