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 148 lines 4.71 KB
Raw
1 import type { ZodBackend } from '../backend/types';
2 import type { JsonSchemaObject } from '../types';
3 import { extendSchemaWithMessage } from '../utils/extend-schema';
4
5 type DecimalInteger = {
6 readonly integer: bigint;
7 readonly scale: number;
8 };
9
10 const toDecimalInteger = (value: number): DecimalInteger | undefined => {
11 if (!Number.isFinite(value)) return undefined;
12
13 const negative = value < 0;
14 const [coefficient, exponentText] = Math.abs(value).toString().toLowerCase().split('e');
15 const exponent = exponentText === undefined ? 0 : Number(exponentText);
16 const [whole, fraction = ''] = coefficient.split('.');
17 let digits = `${whole}${fraction}`.replace(/^0+(?=\d)/, '');
18 let scale = fraction.length - exponent;
19
20 if (scale < 0) {
21 digits += '0'.repeat(-scale);
22 scale = 0;
23 }
24
25 const integer = BigInt(`${negative ? '-' : ''}${digits}`);
26 return { integer, scale };
27 };
28
29 const isJsonMultipleOf = (value: number, multiple: number): boolean => {
30 const dividend = toDecimalInteger(value);
31 const divisor = toDecimalInteger(multiple);
32 if (!dividend || !divisor || divisor.integer === 0n) return false;
33
34 const scale = Math.max(dividend.scale, divisor.scale);
35 const scaledDividend = dividend.integer * 10n ** BigInt(scale - dividend.scale);
36 const scaledDivisor = divisor.integer * 10n ** BigInt(scale - divisor.scale);
37 return scaledDividend % scaledDivisor === 0n;
38 };
39
40 export const parseNumber = <S>(
41 jsonSchema: JsonSchemaObject & { type: 'number' | 'integer' },
42 backend: ZodBackend<S>
43 ): S => {
44 let zodSchema = backend.number();
45
46 let isInteger = false;
47 if (jsonSchema.type === 'integer') {
48 isInteger = true;
49 zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, 'type', (zs, _, errorMsg) =>
50 backend.integer(zs, errorMsg)
51 );
52 } else if (jsonSchema.format === 'int64') {
53 isInteger = true;
54 zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, 'format', (zs, _, errorMsg) =>
55 backend.integer(zs, errorMsg)
56 );
57 }
58
59 if (typeof jsonSchema.minimum === 'number') {
60 if (jsonSchema.exclusiveMinimum === true) {
61 zodSchema = extendSchemaWithMessage(
62 zodSchema,
63 jsonSchema,
64 'minimum',
65 (zs, minimum, errorMsg) => backend.bound(zs, 'gt', minimum, errorMsg)
66 );
67 } else {
68 zodSchema = extendSchemaWithMessage(
69 zodSchema,
70 jsonSchema,
71 'minimum',
72 (zs, minimum, errorMsg) => backend.bound(zs, 'gte', minimum, errorMsg)
73 );
74 }
75 }
76 if (typeof jsonSchema.exclusiveMinimum === 'number') {
77 zodSchema = extendSchemaWithMessage(
78 zodSchema,
79 jsonSchema,
80 'exclusiveMinimum',
81 (zs, exclusiveMinimum, errorMsg) =>
82 backend.bound(zs, 'gt', exclusiveMinimum as number, errorMsg)
83 );
84 }
85
86 if (typeof jsonSchema.maximum === 'number') {
87 if (jsonSchema.exclusiveMaximum === true) {
88 zodSchema = extendSchemaWithMessage(
89 zodSchema,
90 jsonSchema,
91 'maximum',
92 (zs, maximum, errorMsg) => backend.bound(zs, 'lt', maximum, errorMsg)
93 );
94 } else {
95 zodSchema = extendSchemaWithMessage(
96 zodSchema,
97 jsonSchema,
98 'maximum',
99 (zs, maximum, errorMsg) => backend.bound(zs, 'lte', maximum, errorMsg)
100 );
101 }
102 }
103 if (typeof jsonSchema.exclusiveMaximum === 'number') {
104 zodSchema = extendSchemaWithMessage(
105 zodSchema,
106 jsonSchema,
107 'exclusiveMaximum',
108 (zs, exclusiveMaximum, errorMsg) =>
109 backend.bound(zs, 'lt', exclusiveMaximum as number, errorMsg)
110 );
111 }
112
113 // Handle generic 'min' property as alias for 'minimum'
114 if (typeof jsonSchema.min === 'number' && typeof jsonSchema.minimum !== 'number') {
115 zodSchema = extendSchemaWithMessage(
116 zodSchema,
117 { ...jsonSchema, minimum: jsonSchema.min },
118 'minimum',
119 (zs, minimum, errorMsg) => backend.bound(zs, 'gte', minimum, errorMsg)
120 );
121 }
122
123 // Handle generic 'max' property as alias for 'maximum'
124 if (typeof jsonSchema.max === 'number' && typeof jsonSchema.maximum !== 'number') {
125 zodSchema = extendSchemaWithMessage(
126 zodSchema,
127 { ...jsonSchema, maximum: jsonSchema.max },
128 'maximum',
129 (zs, maximum, errorMsg) => backend.bound(zs, 'lte', maximum, errorMsg)
130 );
131 }
132
133 if (typeof jsonSchema.multipleOf === 'number') {
134 const errorMessage = (
135 jsonSchema as JsonSchemaObject & { errorMessage?: Record<string, string> }
136 ).errorMessage?.multipleOf;
137 if (jsonSchema.multipleOf === 1) {
138 return isInteger ? zodSchema : backend.integer(zodSchema, errorMessage);
139 }
140 return backend.refine<number>(
141 zodSchema,
142 value => isJsonMultipleOf(value, jsonSchema.multipleOf!),
143 errorMessage ?? `Number must be a multiple of ${jsonSchema.multipleOf}`
144 );
145 }
146
147 return zodSchema;
148 };