Skip to content

Commit 9cd75be

Browse files
committed
Update custom field handling for Zod 4. Add unit tests.
1 parent f7e6a53 commit 9cd75be

4 files changed

Lines changed: 81 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
- [2025-12-19] [Update custom field handling for Zod 4. Add unit tests.](https://github.com/RubricLab/agents/commit/ab12bae95da47b65c2c8b14e9a3b0e307deab23f)
12
- [2025-12-13] [Add required package repo field for provenance](https://github.com/RubricLab/agents/commit/0ffd9cd04802e1f72b847317cc52046734aca156)
23
- [2025-12-11] [Use updated package release script](https://github.com/RubricLab/agents/commit/001058e9312fe9c903534207ab7148b7ff919605)
34
- [2025-12-11] [Publish with latest NPM](https://github.com/RubricLab/agents/commit/6b76baeba3147b60a6928a5d95008f27f405c6a2)

lib/responseFormat.test.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { describe, expect, it } from 'bun:test'
2+
import { z } from 'zod'
3+
import { createJSONSchema, createResponseFormat } from './responseFormat'
4+
5+
describe('createJSONSchema', () => {
6+
// it('should convert a basic object schema to JSON schema', () => {
7+
// const schema = z.object({
8+
// age: z.number(),
9+
// name: z.string()
10+
// })
11+
12+
// const jsonSchema = createJSONSchema({ schema })
13+
14+
// expect(jsonSchema.type).toBe('object')
15+
// expect(jsonSchema.properties).toEqual({
16+
// age: { type: 'number' },
17+
// name: { type: 'string' }
18+
// })
19+
// expect(jsonSchema.additionalProperties).toBe(false)
20+
// })
21+
22+
it('should handle custom schemas with params.type and params.token', () => {
23+
const customSchema = z.custom<string>(() => true, {
24+
params: { token: 'my_custom_token', type: 'custom' }
25+
})
26+
27+
const schema = z.object({
28+
customField: customSchema
29+
})
30+
31+
const jsonSchema = createJSONSchema({ schema })
32+
33+
expect(jsonSchema.type).toBe('object')
34+
expect((jsonSchema.properties as Record<string, unknown>).customField).toEqual({
35+
const: 'my_custom_token',
36+
type: 'string'
37+
})
38+
})
39+
})
40+
41+
describe('createResponseFormat', () => {
42+
it('should create a response format with correct structure', () => {
43+
const schema = z.object({
44+
age: z.number(),
45+
name: z.string()
46+
})
47+
48+
const responseFormat = createResponseFormat({
49+
name: 'test_response',
50+
schema
51+
})
52+
53+
expect(responseFormat.name).toBe('test_response')
54+
expect(responseFormat.type).toBe('json_schema')
55+
expect(responseFormat.strict).toBe(true)
56+
expect(responseFormat.$brand).toBe('auto-parseable-response-format')
57+
expect(responseFormat.schema.properties).toEqual({
58+
age: { type: 'number' },
59+
name: { type: 'string' }
60+
})
61+
})
62+
63+
it('should parse raw JSON correctly', () => {
64+
const schema = z.object({
65+
value: z.number()
66+
})
67+
68+
const responseFormat = createResponseFormat({
69+
name: 'number_response',
70+
schema
71+
})
72+
73+
const parsed = responseFormat.$parseRaw('{"value": 42}')
74+
75+
expect(parsed).toEqual({ value: 42 })
76+
})
77+
})

lib/responseFormat.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ export function createJSONSchema<T extends z.ZodType>({
1111
}) {
1212
return z.toJSONSchema(schema, {
1313
override: ({ jsonSchema, zodSchema }) => {
14-
if (zodSchema._zod.def.type === 'custom' && zodSchema._zod.def.extended.type === 'custom') {
14+
if (zodSchema._zod.def.type === 'custom' && zodSchema._zod.def.params?.type === 'custom') {
1515
jsonSchema.type = 'string'
16-
jsonSchema.const = zodSchema._zod.def.extended.token
16+
jsonSchema.const = zodSchema._zod.def.params?.token
1717
}
1818
if ('id' in jsonSchema) {
1919
delete jsonSchema.id

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424
"simple-git-hooks": {
2525
"post-commit": "bun x @rubriclab/package post-commit"
2626
},
27-
"version": "0.0.82"
27+
"version": "0.0.83"
2828
}

0 commit comments

Comments
 (0)