Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { jsonSchemaToZod } from '../src/json-schema-to-zod'; |
| 2 | import type { JsonSchema } from '../src/types'; |
| 3 | |
| 4 | console.log('=== Testing Complex Schema Edge Cases ===\n'); |
| 5 | |
| 6 | // Test 1: Original anyOf schema (not from nullable conversion) |
| 7 | console.log('Test 1: Original anyOf with different types and defaults'); |
| 8 | const originalAnyOfSchema: JsonSchema = { |
| 9 | type: 'object', |
| 10 | properties: { |
| 11 | flexible_field: { |
| 12 | anyOf: [ |
| 13 | { |
| 14 | type: 'string', |
| 15 | minLength: 5, |
| 16 | default: 'hello', // This should be preserved |
| 17 | }, |
| 18 | { |
| 19 | type: 'number', |
| 20 | minimum: 0, |
| 21 | default: 42, // This should be preserved |
| 22 | }, |
| 23 | { |
| 24 | type: 'boolean', |
| 25 | default: true, // This should be preserved |
| 26 | }, |
| 27 | ], |
| 28 | description: 'A field that can be string, number, or boolean', |
| 29 | default: 'hello', |
| 30 | }, |
| 31 | status: { |
| 32 | anyOf: [ |
| 33 | { |
| 34 | type: 'string', |
| 35 | enum: ['active', 'inactive'], |
| 36 | default: 'active', |
| 37 | }, |
| 38 | { |
| 39 | type: 'number', |
| 40 | enum: [1, 0], |
| 41 | default: 1, |
| 42 | }, |
| 43 | ], |
| 44 | default: 'active', |
| 45 | }, |
| 46 | }, |
| 47 | required: [], |
| 48 | }; |
| 49 | |
| 50 | console.log('Original schema:'); |
| 51 | console.log(JSON.stringify(originalAnyOfSchema, null, 2)); |
| 52 | |
| 53 | const zodSchema1 = jsonSchemaToZod(originalAnyOfSchema); |
| 54 | const backToJson1 = {}; // zodToJsonSchema removed - no longer compatible with zod v4 |
| 55 | |
| 56 | console.log('\nConverted back to JSON:'); |
| 57 | console.log(JSON.stringify(backToJson1, null, 2)); |
| 58 | |
| 59 | console.log('\nTesting parsing:'); |
| 60 | try { |
| 61 | console.log('Empty object:', zodSchema1.parse({})); |
| 62 | console.log('With string flexible_field:', zodSchema1.parse({ flexible_field: 'world' })); |
| 63 | console.log('With number flexible_field:', zodSchema1.parse({ flexible_field: 123 })); |
| 64 | console.log('With boolean flexible_field:', zodSchema1.parse({ flexible_field: false })); |
| 65 | } catch (error) { |
| 66 | console.error('Parse error:', error); |
| 67 | } |
| 68 | |
| 69 | console.log('\n' + '='.repeat(80) + '\n'); |
| 70 | |
| 71 | // Test 2: Array with different types (union types) |
| 72 | console.log('Test 2: Array with union of different types'); |
| 73 | const arrayUnionSchema: JsonSchema = { |
| 74 | type: 'object', |
| 75 | properties: { |
| 76 | mixed_items: { |
| 77 | type: 'array', |
| 78 | items: { |
| 79 | anyOf: [ |
| 80 | { |
| 81 | type: 'string', |
| 82 | pattern: '^[a-zA-Z]+$', |
| 83 | default: 'text', |
| 84 | }, |
| 85 | { |
| 86 | type: 'number', |
| 87 | multipleOf: 2, |
| 88 | default: 10, |
| 89 | }, |
| 90 | { |
| 91 | type: 'object', |
| 92 | properties: { |
| 93 | id: { type: 'number' }, |
| 94 | name: { type: 'string' }, |
| 95 | }, |
| 96 | required: ['id'], |
| 97 | default: { id: 1, name: 'default' }, |
| 98 | }, |
| 99 | ], |
| 100 | }, |
| 101 | default: ['text', 10, { id: 1, name: 'default' }], |
| 102 | description: 'Array that can contain strings, numbers, or objects', |
| 103 | }, |
| 104 | tags: { |
| 105 | type: 'array', |
| 106 | items: { |
| 107 | oneOf: [ |
| 108 | { |
| 109 | type: 'string', |
| 110 | enum: ['important', 'urgent', 'normal'], |
| 111 | default: 'normal', |
| 112 | }, |
| 113 | { |
| 114 | type: 'number', |
| 115 | minimum: 1, |
| 116 | maximum: 5, |
| 117 | default: 3, |
| 118 | }, |
| 119 | ], |
| 120 | }, |
| 121 | default: ['normal', 3], |
| 122 | description: 'Tags can be priority strings or numeric ratings', |
| 123 | }, |
| 124 | }, |
| 125 | required: [], |
| 126 | }; |
| 127 | |
| 128 | console.log('Original schema:'); |
| 129 | console.log(JSON.stringify(arrayUnionSchema, null, 2)); |
| 130 | |
| 131 | const zodSchema2 = jsonSchemaToZod(arrayUnionSchema); |
| 132 | |
| 133 | console.log('\nTesting parsing:'); |
| 134 | try { |
| 135 | console.log('Empty object:', zodSchema2.parse({})); |
| 136 | console.log( |
| 137 | 'With mixed array:', |
| 138 | zodSchema2.parse({ |
| 139 | mixed_items: ['hello', 42, { id: 5, name: 'test' }], |
| 140 | tags: ['urgent', 4, 'normal'], |
| 141 | }) |
| 142 | ); |
| 143 | } catch (error) { |
| 144 | console.error('Parse error:', error); |
| 145 | } |
| 146 | |
| 147 | console.log('\n' + '='.repeat(80) + '\n'); |
| 148 | |
| 149 | // Test 3: Complex nested schema with multiple anyOf and nullable fields |
| 150 | console.log('Test 3: Complex nested schema with anyOf and nullable'); |
| 151 | const complexNestedSchema: JsonSchema = { |
| 152 | type: 'object', |
| 153 | properties: { |
| 154 | user: { |
| 155 | anyOf: [ |
| 156 | { |
| 157 | type: 'object', |
| 158 | properties: { |
| 159 | id: { type: 'string', default: 'user-123' }, |
| 160 | name: { type: 'string' }, |
| 161 | email: { |
| 162 | type: 'string', |
| 163 | format: 'email', |
| 164 | nullable: true, |
| 165 | default: null, |
| 166 | } as JsonSchema, |
| 167 | }, |
| 168 | required: ['name'], |
| 169 | default: { id: 'user-123', name: 'Anonymous', email: null }, |
| 170 | }, |
| 171 | { |
| 172 | type: 'string', |
| 173 | pattern: '^user-\\d+$', |
| 174 | default: 'user-000', |
| 175 | }, |
| 176 | ], |
| 177 | description: 'User can be an object or a user ID string', |
| 178 | default: { id: 'user-123', name: 'Anonymous', email: null }, |
| 179 | }, |
| 180 | metadata: { |
| 181 | type: 'object', |
| 182 | properties: { |
| 183 | settings: { |
| 184 | anyOf: [ |
| 185 | { |
| 186 | type: 'object', |
| 187 | properties: { |
| 188 | theme: { |
| 189 | type: 'string', |
| 190 | enum: ['light', 'dark'], |
| 191 | default: 'light', |
| 192 | }, |
| 193 | notifications: { |
| 194 | type: 'boolean', |
| 195 | default: true, |
| 196 | }, |
| 197 | }, |
| 198 | default: { theme: 'light', notifications: true }, |
| 199 | }, |
| 200 | { |
| 201 | type: 'string', |
| 202 | enum: ['minimal', 'full'], |
| 203 | default: 'full', |
| 204 | }, |
| 205 | ], |
| 206 | default: { theme: 'light', notifications: true }, |
| 207 | }, |
| 208 | version: { |
| 209 | type: 'string', |
| 210 | nullable: true, |
| 211 | default: null, |
| 212 | } as JsonSchema, |
| 213 | }, |
| 214 | default: { |
| 215 | settings: { theme: 'light', notifications: true }, |
| 216 | version: null, |
| 217 | }, |
| 218 | }, |
| 219 | }, |
| 220 | required: [], |
| 221 | }; |
| 222 | |
| 223 | console.log('Original schema:'); |
| 224 | console.log(JSON.stringify(complexNestedSchema, null, 2)); |
| 225 | |
| 226 | const zodSchema3 = jsonSchemaToZod(complexNestedSchema); |
| 227 | |
| 228 | console.log('\nTesting parsing:'); |
| 229 | try { |
| 230 | console.log('Empty object:', zodSchema3.parse({})); |
| 231 | console.log( |
| 232 | 'With user object:', |
| 233 | zodSchema3.parse({ |
| 234 | user: { name: 'John Doe', email: 'john@example.com' }, |
| 235 | metadata: { |
| 236 | settings: 'minimal', |
| 237 | version: '1.0.0', |
| 238 | }, |
| 239 | }) |
| 240 | ); |
| 241 | console.log( |
| 242 | 'With user string:', |
| 243 | zodSchema3.parse({ |
| 244 | user: 'user-456', |
| 245 | }) |
| 246 | ); |
| 247 | } catch (error) { |
| 248 | console.error('Parse error:', error); |
| 249 | } |