Skip to content

Commit 51534b0

Browse files
author
Kamiel Wanrooij
committed
fix: support JSON Schema extension keywords and properly infer required fields
1 parent 2c4f29d commit 51534b0

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

runtimes/server-interface/agent.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ interface BaseSchema {
66
$id?: string
77
$schema?: string
88
definitions?: Record<string, JSONSchema>
9+
[extensionProperties: string]: any
910
}
1011

1112
interface StringSchema extends BaseSchema {
@@ -74,9 +75,20 @@ type InferArray<T extends { type: 'array'; items: any }> = T['items'] extends {
7475
? InferSchema<T['items']>[]
7576
: never
7677

77-
type InferObject<T extends { type: 'object'; properties: Record<string, any> }> = {
78-
[K in keyof T['properties']]?: InferSchema<T['properties'][K]>
79-
} & (T extends { required: string[] } ? { [K in T['required'][number]]: InferSchema<T['properties'][K]> } : unknown)
78+
type InferObject<
79+
T extends {
80+
type: 'object'
81+
properties: Record<string, any>
82+
},
83+
> = T extends { required: readonly string[] }
84+
? {
85+
[K in keyof T['properties'] as K extends T['required'][number] ? K : never]: InferSchema<T['properties'][K]>
86+
} & {
87+
[K in keyof T['properties'] as K extends T['required'][number] ? never : K]?: InferSchema<T['properties'][K]>
88+
}
89+
: {
90+
[K in keyof T['properties']]?: InferSchema<T['properties'][K]>
91+
}
8092

8193
export type InferSchema<T> = T extends { type: 'array'; items: any }
8294
? InferArray<T>

0 commit comments

Comments
 (0)