( schema: OpenAPIV3_1.SchemaObject, parentRequired: boolean = true, )
| 1 | import { OpenAPIV3_1 } from "openapi-types"; |
| 2 | |
| 3 | export function isChoiceRequired( |
| 4 | schema: OpenAPIV3_1.SchemaObject, |
| 5 | parentRequired: boolean = true, |
| 6 | ): boolean { |
| 7 | /** Determines whether there's a choice to be made by the AI for |
| 8 | * a parameter. There's no choice if it is required and has 1 enum |
| 9 | * value (or all nested children are like this) **/ |
| 10 | if (!parentRequired) return true; |
| 11 | if (schema.type !== "object") { |
| 12 | return !(schema.enum && schema.enum.length === 1); |
| 13 | } |
| 14 | |
| 15 | const requiredKeys = schema.required || []; |
| 16 | |
| 17 | for (let key in schema.properties) { |
| 18 | const isKeyRequired = requiredKeys.includes(key); |
| 19 | const childSchema = schema.properties[key] as OpenAPIV3_1.SchemaObject; |
| 20 | |
| 21 | // If any nested object requires choice, then choice is required. |
| 22 | if (isChoiceRequired(childSchema, isKeyRequired)) { |
| 23 | return true; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | // If no choice required in any properties, then no choice required. |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | export function getFilledNoChoiceRequiredFields( |
| 32 | schema: OpenAPIV3_1.SchemaObject | undefined, |
no outgoing calls
no test coverage detected