fix(core): align JSON Schema required fields with Zod (#549)

This commit is contained in:
s4kura authored and GitHub committed 2026-08-24 15:35:53 +08:00
1 parent 16562ed894
commit 5735cd69e7
2 files changed
+21 -6

No files matched your search

+1 -6
View File
@@ -370,12 +370,7 @@ function convertZodType(schema: ZodSchema): Record<string, unknown> {
for (const [key, value] of Object.entries(objectDef.shape())) {
properties[key] = convertZodType(value as ZodSchema)
const innerDef = ((value as ZodSchema) as unknown as { _def: ZodTypeDef })._def
const isOptional =
innerDef.typeName === ZodTypeName.ZodOptional ||
innerDef.typeName === ZodTypeName.ZodDefault ||
innerDef.typeName === ZodTypeName.ZodNullable
if (!isOptional) {
if (!(value as ZodSchema).isOptional()) {
required.push(key)
}
}
+20
View File
@@ -317,6 +317,26 @@ describe('ToolRegistry', () => {
expect(defs[0].name).toBe('echo')
expect(defs[0].inputSchema).toHaveProperty('properties')
})
it('marks nullable inputs as required when Zod rejects omission', () => {
const inputSchema = z.object({
requiredNullable: z.string().nullable(),
optional: z.string().optional(),
})
const registry = new ToolRegistry()
registry.register(defineTool({
name: 'nullable_input',
description: 'Accepts a required nullable value.',
inputSchema,
execute: async () => ({ data: 'ok' }),
}))
const [definition] = registry.toToolDefs()
expect(inputSchema.safeParse({}).success).toBe(false)
expect(inputSchema.safeParse({ requiredNullable: null }).success).toBe(true)
expect(definition.inputSchema).toMatchObject({ required: ['requiredNullable'] })
})
})
// ---------------------------------------------------------------------------