Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 33 lines 926 Bytes
Raw
1 import { parseSchema } from './parse-schema';
2 import type { ParseContext } from '../context';
3 import type { JsonSchemaObject, JsonSchema } from '../types';
4
5 export const parseIfThenElse = <S>(
6 jsonSchema: JsonSchemaObject & {
7 if: JsonSchema;
8 then: JsonSchema;
9 else: JsonSchema;
10 },
11 ctx: ParseContext<S>
12 ): S => {
13 const { backend } = ctx;
14 const $if = parseSchema(jsonSchema.if, { ...ctx, path: [...ctx.path, 'if'] });
15 const $then = parseSchema(jsonSchema.then, {
16 ...ctx,
17 path: [...ctx.path, 'then'],
18 });
19 const $else = parseSchema(jsonSchema.else, {
20 ...ctx,
21 path: [...ctx.path, 'else'],
22 });
23
24 return backend.superRefine<unknown>(backend.union([$then, $else]), (value, issues) => {
25 const result = backend.run($if, value).success
26 ? backend.run($then, value)
27 : backend.run($else, value);
28
29 if (!result.success) {
30 issues.forward(result.failure);
31 }
32 });
33 };