Skip to content

Commit

Permalink
Upates
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfarrell76 committed Oct 6, 2024
1 parent ca3529b commit bdc8b84
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/codecTools/toJsonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type MappableType =

/**
* Convert an io-ts codec to a JSON Schema (v7)
*
* @param _type - an io-ts codec
* @param strict - whether to enable strict mode
* @param alwaysIncludeRequired - whether to always include required fields (OpenAI requires this)
Expand Down Expand Up @@ -63,13 +64,28 @@ export const toJsonSchema = (
),
};
}
if (type._tag === 'IntersectionType') {
if (type._tag === 'IntersectionType' && !alwaysIncludeRequired) {
return {
allOf: type.types.map((t: any) =>
toJsonSchema(t, strict, alwaysIncludeRequired),
),
};
}
if (type._tag === 'IntersectionType' && alwaysIncludeRequired) {
const results = type.types.map((t: any) =>
toJsonSchema(t, strict, alwaysIncludeRequired),
);
if (!results.every((r) => r.type === 'object')) {
throw new Error('InterfaceType must have all children as type=object');
}
return {
type: 'object',
required: results.map((r) => r.required).flat(),
properties: results.reduce((acc, r) => ({ ...acc, ...r.properties }), {}),
...(strict ? { additionalProperties: false } : {}),
};
}

if (type._tag === 'InterfaceType') {
return {
type: 'object',
Expand Down

0 comments on commit bdc8b84

Please sign in to comment.