Skip to content

Commit 47005d7

Browse files
Upates
1 parent ca3529b commit 47005d7

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/codecTools/toJsonSchema.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,31 @@ export const toJsonSchema = (
6363
),
6464
};
6565
}
66-
if (type._tag === 'IntersectionType') {
66+
if (type._tag === 'IntersectionType' && !alwaysIncludeRequired) {
6767
return {
6868
allOf: type.types.map((t: any) =>
6969
toJsonSchema(t, strict, alwaysIncludeRequired),
7070
),
7171
};
7272
}
73+
if (type._tag === 'IntersectionType' && alwaysIncludeRequired) {
74+
const results = type.types.map((t: any) =>
75+
toJsonSchema(t, strict, alwaysIncludeRequired),
76+
);
77+
if (!results.every((r: any) => r.type === 'object')) {
78+
throw new Error('InterfaceType must have all children as type=object');
79+
}
80+
return {
81+
type: 'object',
82+
required: results.map((r: any) => r.required).flat(),
83+
properties: results.reduce(
84+
(acc: any, r: any) => ({ ...acc, ...r.properties }),
85+
{} as any,
86+
),
87+
...(strict ? { additionalProperties: false } : {}),
88+
};
89+
}
90+
7391
if (type._tag === 'InterfaceType') {
7492
return {
7593
type: 'object',
@@ -138,7 +156,7 @@ export const toJsonSchema = (
138156
};
139157
}
140158
// could add more here for DateFromISOString, etc. etc.
141-
return unhandledType(type);
159+
return unhandledType(type as never);
142160
};
143161

144162
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type, @typescript-eslint/no-unused-vars

0 commit comments

Comments
 (0)