-
Notifications
You must be signed in to change notification settings - Fork 20
fix: sanitize ~standard property from Zod v4 JSON schemas #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+223
−3
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cb0ea8e
fix: sanitize ~standard property from Zod v4 JSON schemas
mattapperson dcf37b1
refactor: extract checkNoTildeKeys to shared test utility
mattapperson b42e596
test: increase timeout for generator tool preliminary results test
mattapperson 338e69a
test: use claude-haiku-4.5 for generator tool preliminary results test
mattapperson 5eea4eb
fix: replace type assertion with typeguard for sanitizeJsonSchema
mattapperson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { sanitizeJsonSchema, convertZodToJsonSchema } from '../../src/lib/tool-executor.js'; | ||
| import { z } from 'zod/v4'; | ||
| import { assertNoTildeKeys } from '../utils/schema-test-helpers.js'; | ||
|
|
||
| describe('sanitizeJsonSchema', () => { | ||
| it('should remove ~standard property from root level', () => { | ||
| const input = { | ||
| type: 'object', | ||
| properties: { name: { type: 'string' } }, | ||
| '~standard': { version: 1, vendor: 'zod' }, | ||
| }; | ||
|
|
||
| const result = sanitizeJsonSchema(input); | ||
|
|
||
| expect(result).not.toHaveProperty('~standard'); | ||
| expect(result).toHaveProperty('type', 'object'); | ||
| expect(result).toHaveProperty('properties'); | ||
| }); | ||
|
|
||
| it('should remove all ~ prefixed properties from nested objects', () => { | ||
| const input = { | ||
| type: 'object', | ||
| '~metadata': { foo: 'bar' }, | ||
| properties: { | ||
| user: { | ||
| type: 'object', | ||
| '~internal': true, | ||
| properties: { | ||
| name: { type: 'string', '~validator': 'custom' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const result = sanitizeJsonSchema(input); | ||
| const typedResult = result as Record<string, unknown>; | ||
| const properties = typedResult['properties'] as Record<string, unknown>; | ||
| const user = properties['user'] as Record<string, unknown>; | ||
| const userProperties = user['properties'] as Record<string, unknown>; | ||
| const name = userProperties['name'] as Record<string, unknown>; | ||
|
|
||
| expect(typedResult).not.toHaveProperty('~metadata'); | ||
| expect(user).not.toHaveProperty('~internal'); | ||
| expect(name).not.toHaveProperty('~validator'); | ||
| expect(name).toHaveProperty('type', 'string'); | ||
| }); | ||
|
|
||
| it('should handle arrays correctly', () => { | ||
| const input = { | ||
| type: 'array', | ||
| '~arrayMeta': 'remove-me', | ||
| items: [ | ||
| { type: 'string', '~itemMeta': 'also-remove' }, | ||
| { type: 'number' }, | ||
| ], | ||
| }; | ||
|
|
||
| const result = sanitizeJsonSchema(input); | ||
| const typedResult = result as Record<string, unknown>; | ||
| const items = typedResult['items'] as Array<Record<string, unknown>>; | ||
|
|
||
| expect(typedResult).not.toHaveProperty('~arrayMeta'); | ||
| expect(items[0]).not.toHaveProperty('~itemMeta'); | ||
| expect(items[0]).toHaveProperty('type', 'string'); | ||
| expect(items[1]).toHaveProperty('type', 'number'); | ||
| }); | ||
|
|
||
| it('should pass through primitive values unchanged', () => { | ||
| expect(sanitizeJsonSchema(null)).toBe(null); | ||
| expect(sanitizeJsonSchema(undefined)).toBe(undefined); | ||
| expect(sanitizeJsonSchema(42)).toBe(42); | ||
| expect(sanitizeJsonSchema('string')).toBe('string'); | ||
| expect(sanitizeJsonSchema(true)).toBe(true); | ||
| }); | ||
|
|
||
| it('should handle empty objects', () => { | ||
| expect(sanitizeJsonSchema({})).toEqual({}); | ||
| }); | ||
|
|
||
| it('should handle empty arrays', () => { | ||
| expect(sanitizeJsonSchema([])).toEqual([]); | ||
| }); | ||
|
|
||
| it('should preserve non-~ prefixed properties', () => { | ||
| const input = { | ||
| type: 'object', | ||
| description: 'A user object', | ||
| required: ['name'], | ||
| properties: { | ||
| name: { type: 'string' }, | ||
| }, | ||
| }; | ||
|
|
||
| const result = sanitizeJsonSchema(input); | ||
|
|
||
| expect(result).toEqual(input); | ||
| }); | ||
| }); | ||
|
|
||
| describe('convertZodToJsonSchema', () => { | ||
| it('should return sanitized JSON schema without ~ prefixed properties', () => { | ||
| const schema = z.object({ | ||
| name: z.string().describe("The user's name"), | ||
| age: z.number().min(0).describe("The user's age"), | ||
| }); | ||
|
|
||
| const jsonSchema = convertZodToJsonSchema(schema); | ||
|
|
||
| // Check that standard schema properties are present | ||
| expect(jsonSchema).toHaveProperty('type', 'object'); | ||
| expect(jsonSchema).toHaveProperty('properties'); | ||
|
|
||
| // Check that no ~ prefixed keys exist anywhere in the schema | ||
| assertNoTildeKeys(jsonSchema); | ||
| }); | ||
|
|
||
| it('should preserve all valid JSON Schema properties', () => { | ||
| const schema = z.object({ | ||
| location: z.string().describe('City and country'), | ||
| }); | ||
|
|
||
| const jsonSchema = convertZodToJsonSchema(schema); | ||
| const properties = jsonSchema['properties'] as Record<string, unknown>; | ||
| const location = properties['location'] as Record<string, unknown>; | ||
|
|
||
| expect(location).toHaveProperty('description', 'City and country'); | ||
| expect(location).toHaveProperty('type', 'string'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { expect } from 'vitest'; | ||
|
|
||
| /** | ||
| * Recursively checks that no keys prefixed with ~ exist in the given object. | ||
| * Used to verify that JSON schemas have been properly sanitized. | ||
| * @throws AssertionError if a ~ prefixed key is found | ||
| */ | ||
| export function assertNoTildeKeys(obj: unknown): void { | ||
| if (obj === null || typeof obj !== 'object') { | ||
| return; | ||
| } | ||
|
|
||
| if (Array.isArray(obj)) { | ||
| for (const item of obj) { | ||
| assertNoTildeKeys(item); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| for (const key of Object.keys(obj)) { | ||
| expect(key.startsWith('~')).toBe(false); | ||
| assertNoTildeKeys((obj as Record<string, unknown>)[key]); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.