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 1,604 lines 50.3 KB
Raw
1 import { describe, it, expect } from 'vitest';
2 import type { JsonSchema } from '../src/types';
3 import { acceptedFor, loadObjectCases } from './fixtures/corpus';
4 import { builders } from './support/builders';
5 import { testFor } from './support/divergences';
6
7 describe.each(builders)('jsonSchemaToZod ($name)', builder => {
8 describe('Basic Types', () => {
9 it('should convert string schema', () => {
10 const schema: JsonSchema = {
11 type: 'string',
12 };
13 const zodSchema = builder.convert(schema);
14 expect(builder.kind(zodSchema)).toBe('string');
15 expect(zodSchema.parse('test')).toBe('test');
16 expect(() => zodSchema.parse(123)).toThrow();
17 });
18
19 it('should convert number schema', () => {
20 const schema: JsonSchema = {
21 type: 'number',
22 };
23 const zodSchema = builder.convert(schema);
24 expect(builder.kind(zodSchema)).toBe('number');
25 expect(zodSchema.parse(123)).toBe(123);
26 expect(() => zodSchema.parse('123')).toThrow();
27 });
28
29 it('should convert integer schema', () => {
30 const schema: JsonSchema = {
31 type: 'integer',
32 };
33 const zodSchema = builder.convert(schema);
34 expect(zodSchema.parse(123)).toBe(123);
35 expect(() => zodSchema.parse(123.45)).toThrow();
36 });
37
38 it('should convert boolean schema', () => {
39 const schema: JsonSchema = {
40 type: 'boolean',
41 };
42 const zodSchema = builder.convert(schema);
43 expect(builder.kind(zodSchema)).toBe('boolean');
44 expect(zodSchema.parse(true)).toBe(true);
45 expect(() => zodSchema.parse('true')).toThrow();
46 });
47
48 it('should convert null schema', () => {
49 const schema: JsonSchema = {
50 type: 'null',
51 };
52 const zodSchema = builder.convert(schema);
53 expect(builder.kind(zodSchema)).toBe('null');
54 expect(zodSchema.parse(null)).toBe(null);
55 expect(() => zodSchema.parse(undefined)).toThrow();
56 });
57 });
58
59 describe('String Formats', () => {
60 it('should validate email format', () => {
61 const schema: JsonSchema = {
62 type: 'string',
63 format: 'email',
64 };
65 const zodSchema = builder.convert(schema);
66 expect(zodSchema.parse('test@example.com')).toBe('test@example.com');
67 expect(() => zodSchema.parse('invalid-email')).toThrow();
68 });
69
70 it('should validate date-time format', () => {
71 const schema: JsonSchema = {
72 type: 'string',
73 format: 'date-time',
74 };
75 const zodSchema = builder.convert(schema);
76 expect(zodSchema.parse('2024-03-20T12:00:00Z')).toBe('2024-03-20T12:00:00Z');
77 expect(() => zodSchema.parse('invalid-date')).toThrow();
78 });
79
80 it('should validate uuid format', () => {
81 const schema: JsonSchema = {
82 type: 'string',
83 format: 'uuid',
84 };
85 const zodSchema = builder.convert(schema);
86 expect(zodSchema.parse('123e4567-e89b-12d3-a456-426614174000')).toBe(
87 '123e4567-e89b-12d3-a456-426614174000'
88 );
89 expect(() => zodSchema.parse('invalid-uuid')).toThrow();
90 });
91 });
92
93 describe('Number Validations', () => {
94 it('should validate minimum and maximum', () => {
95 const schema: JsonSchema = {
96 type: 'number',
97 minimum: 0,
98 maximum: 100,
99 };
100 const zodSchema = builder.convert(schema);
101 expect(zodSchema.parse(50)).toBe(50);
102 expect(() => zodSchema.parse(-1)).toThrow();
103 expect(() => zodSchema.parse(101)).toThrow();
104 });
105
106 it('should validate exclusive minimum and maximum', () => {
107 const schema: JsonSchema = {
108 type: 'number',
109 exclusiveMinimum: 0,
110 exclusiveMaximum: 100,
111 };
112 const zodSchema = builder.convert(schema);
113 expect(zodSchema.parse(50)).toBe(50);
114 expect(() => zodSchema.parse(0)).toThrow();
115 expect(() => zodSchema.parse(100)).toThrow();
116 });
117
118 it('should validate multipleOf', () => {
119 const schema: JsonSchema = {
120 type: 'number',
121 multipleOf: 5,
122 };
123 const zodSchema = builder.convert(schema);
124 expect(zodSchema.parse(15)).toBe(15);
125 expect(() => zodSchema.parse(17)).toThrow();
126 });
127
128 it('should handle generic min and max properties for numbers', () => {
129 const schema = {
130 type: 'number',
131 min: 0,
132 max: 100,
133 } satisfies JsonSchema;
134 const zodSchema = builder.convert(schema);
135 expect(zodSchema.parse(50)).toBe(50);
136 expect(() => zodSchema.parse(-1)).toThrow();
137 expect(() => zodSchema.parse(101)).toThrow();
138 });
139
140 it('should prioritize minimum/maximum over min/max for numbers', () => {
141 const schema = {
142 type: 'number',
143 minimum: 10,
144 maximum: 90,
145 min: 0,
146 max: 100,
147 } satisfies JsonSchema;
148 const zodSchema = builder.convert(schema);
149 expect(zodSchema.parse(50)).toBe(50);
150 expect(zodSchema.parse(10)).toBe(10);
151 expect(zodSchema.parse(90)).toBe(90);
152 expect(() => zodSchema.parse(5)).toThrow(); // Uses minimum (10), not min (0)
153 expect(() => zodSchema.parse(95)).toThrow(); // Uses maximum (90), not max (100)
154 });
155 });
156
157 describe('String Validations', () => {
158 it('should handle generic min and max properties for strings', () => {
159 const schema = {
160 type: 'string',
161 min: 3,
162 max: 10,
163 } satisfies JsonSchema;
164 const zodSchema = builder.convert(schema);
165 expect(zodSchema.parse('hello')).toBe('hello');
166 expect(() => zodSchema.parse('hi')).toThrow();
167 expect(() => zodSchema.parse('this is too long')).toThrow();
168 });
169
170 it('should prioritize minLength/maxLength over min/max for strings', () => {
171 const schema = {
172 type: 'string',
173 minLength: 5,
174 maxLength: 8,
175 min: 3,
176 max: 10,
177 } satisfies JsonSchema;
178 const zodSchema = builder.convert(schema);
179 expect(zodSchema.parse('hello')).toBe('hello');
180 expect(zodSchema.parse('testing')).toBe('testing');
181 expect(() => zodSchema.parse('test')).toThrow(); // Uses minLength (5), not min (3)
182 expect(() => zodSchema.parse('toolongstring')).toThrow(); // Uses maxLength (8), not max (10)
183 });
184 });
185
186 describe('Object Schemas', () => {
187 it('should support extension keywords in nested schemas', () => {
188 const schema = {
189 type: 'object',
190 properties: {
191 username: {
192 type: 'string',
193 min: 3,
194 max: 10,
195 example: 'john_doe',
196 },
197 },
198 required: ['username'],
199 } satisfies JsonSchema;
200
201 const zodSchema = builder.convert(schema);
202
203 expect(zodSchema.parse({ username: 'john_doe' })).toEqual({ username: 'john_doe' });
204 expect(() => zodSchema.parse({ username: 'ab' })).toThrow();
205 expect(() => zodSchema.parse({ username: 'a very long username' })).toThrow();
206 expect(builder.description(builder.shape(zodSchema).username)).toBe('Example: "john_doe"');
207 });
208
209 it('should validate required properties', () => {
210 const schema: JsonSchema = {
211 type: 'object',
212 properties: {
213 name: { type: 'string' },
214 age: { type: 'number' },
215 },
216 required: ['name'],
217 };
218 const zodSchema = builder.convert(schema);
219 expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
220 expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
221 expect(() => zodSchema.parse({ age: 30 })).toThrow();
222 });
223
224 it('should validate additional properties when set to false', () => {
225 const schema: JsonSchema = {
226 type: 'object',
227 properties: {
228 name: { type: 'string' },
229 },
230 additionalProperties: false,
231 };
232 const zodSchema = builder.convert(schema);
233 expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
234 expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
235 });
236
237 it('should allow any additional properties when set to true', () => {
238 const schema: JsonSchema = {
239 type: 'object',
240 properties: {
241 name: { type: 'string' },
242 },
243 additionalProperties: true,
244 };
245 const zodSchema = builder.convert(schema);
246 expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
247 expect(zodSchema.parse({ name: 'John', extra: 'field' })).toEqual({
248 name: 'John',
249 extra: 'field',
250 });
251 expect(zodSchema.parse({ name: 'John', age: 30, city: 'NYC' })).toEqual({
252 name: 'John',
253 age: 30,
254 city: 'NYC',
255 });
256 });
257
258 it('should validate additional properties against a schema', () => {
259 const schema: JsonSchema = {
260 type: 'object',
261 properties: {
262 name: { type: 'string' },
263 },
264 additionalProperties: { type: 'number' },
265 };
266 const zodSchema = builder.convert(schema);
267 expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
268 expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({
269 name: 'John',
270 age: 30,
271 });
272 expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
273 });
274
275 it('should validate additional properties with complex schema', () => {
276 const schema: JsonSchema = {
277 type: 'object',
278 properties: {
279 name: { type: 'string' },
280 },
281 additionalProperties: {
282 type: 'object',
283 properties: {
284 value: { type: 'string' },
285 count: { type: 'number' },
286 },
287 required: ['value'],
288 },
289 };
290 const zodSchema = builder.convert(schema);
291 expect(
292 zodSchema.parse({
293 name: 'John',
294 metadata: { value: 'test', count: 5 },
295 })
296 ).toEqual({
297 name: 'John',
298 metadata: { value: 'test', count: 5 },
299 });
300 expect(
301 zodSchema.parse({
302 name: 'John',
303 metadata: { value: 'test' },
304 })
305 ).toEqual({
306 name: 'John',
307 metadata: { value: 'test' },
308 });
309 expect(() =>
310 zodSchema.parse({
311 name: 'John',
312 metadata: { count: 5 }, // missing required 'value'
313 })
314 ).toThrow();
315 });
316
317 it('should reject additional properties when additionalProperties is not specified', () => {
318 const schema: JsonSchema = {
319 type: 'object',
320 properties: {
321 name: { type: 'string' },
322 },
323 };
324 const zodSchema = builder.convert(schema);
325 expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
326 expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
327 });
328
329 it('should handle additionalProperties with patternProperties', () => {
330 const schema: JsonSchema = {
331 type: 'object',
332 properties: {
333 name: { type: 'string' },
334 },
335 patternProperties: {
336 '^prefix_': { type: 'string' },
337 },
338 additionalProperties: { type: 'number' },
339 };
340 const zodSchema = builder.convert(schema);
341
342 // Should allow defined properties
343 expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
344
345 // Should allow pattern properties
346 expect(
347 zodSchema.parse({
348 name: 'John',
349 prefix_test: 'value',
350 })
351 ).toEqual({
352 name: 'John',
353 prefix_test: 'value',
354 });
355
356 // Should allow additional properties matching the schema
357 expect(
358 zodSchema.parse({
359 name: 'John',
360 age: 30,
361 })
362 ).toEqual({
363 name: 'John',
364 age: 30,
365 });
366
367 // Should reject additional properties not matching the schema
368 expect(() =>
369 zodSchema.parse({
370 name: 'John',
371 extra: 'field', // string, but additionalProperties expects number
372 })
373 ).toThrow();
374 });
375
376 it('should handle object with no properties but with additionalProperties schema', () => {
377 const schema: JsonSchema = {
378 type: 'object',
379 additionalProperties: { type: 'string' },
380 };
381 const zodSchema = builder.convert(schema);
382 expect(zodSchema.parse({})).toEqual({});
383 expect(zodSchema.parse({ key1: 'value1', key2: 'value2' })).toEqual({
384 key1: 'value1',
385 key2: 'value2',
386 });
387 expect(() => zodSchema.parse({ key1: 123 })).toThrow();
388 });
389
390 it('should handle additionalProperties: false with patternProperties', () => {
391 const schema: JsonSchema = {
392 type: 'object',
393 properties: {
394 name: { type: 'string' },
395 },
396 patternProperties: {
397 '^prefix_': { type: 'string' },
398 },
399 additionalProperties: false,
400 };
401 const zodSchema = builder.convert(schema);
402
403 expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
404 expect(
405 zodSchema.parse({
406 name: 'John',
407 prefix_test: 'value',
408 })
409 ).toEqual({
410 name: 'John',
411 prefix_test: 'value',
412 });
413
414 // Should reject properties that don't match patterns or defined properties
415 expect(() =>
416 zodSchema.parse({
417 name: 'John',
418 extra: 'field',
419 })
420 ).toThrow();
421 });
422
423 it('should handle empty object with additionalProperties: true', () => {
424 const schema: JsonSchema = {
425 type: 'object',
426 additionalProperties: true,
427 };
428 const zodSchema = builder.convert(schema);
429 expect(zodSchema.parse({})).toEqual({});
430 expect(zodSchema.parse({ any: 'value', another: 123 })).toEqual({
431 any: 'value',
432 another: 123,
433 });
434 });
435
436 it('should handle empty object with additionalProperties: false', () => {
437 const schema: JsonSchema = {
438 type: 'object',
439 additionalProperties: false,
440 };
441 const zodSchema = builder.convert(schema);
442 expect(zodSchema.parse({})).toEqual({});
443 expect(() => zodSchema.parse({ any: 'value' })).toThrow();
444 });
445
446 it('should handle nested additionalProperties', () => {
447 const schema: JsonSchema = {
448 type: 'object',
449 properties: {
450 config: {
451 type: 'object',
452 properties: {
453 name: { type: 'string' },
454 },
455 additionalProperties: { type: 'number' },
456 },
457 },
458 additionalProperties: { type: 'string' },
459 };
460 const zodSchema = builder.convert(schema);
461
462 expect(
463 zodSchema.parse({
464 config: { name: 'test', timeout: 5000 },
465 env: 'production',
466 })
467 ).toEqual({
468 config: { name: 'test', timeout: 5000 },
469 env: 'production',
470 });
471
472 // Should reject nested additional properties that don't match schema
473 expect(() =>
474 zodSchema.parse({
475 config: { name: 'test', timeout: 'invalid' }, // should be number
476 })
477 ).toThrow();
478
479 // Should reject top-level additional properties that don't match schema
480 expect(() =>
481 zodSchema.parse({
482 config: { name: 'test' },
483 count: 123, // should be string
484 })
485 ).toThrow();
486 });
487
488 it('should validate pattern properties', () => {
489 const schema: JsonSchema = {
490 type: 'object',
491 patternProperties: {
492 '^prefix_': { type: 'string' },
493 },
494 };
495 const zodSchema = builder.convert(schema);
496 expect(zodSchema.parse({ prefix_one: 'value1', prefix_two: 'value2' })).toEqual({
497 prefix_one: 'value1',
498 prefix_two: 'value2',
499 });
500 expect(() => zodSchema.parse({ prefix_one: 123 })).toThrow();
501 });
502 });
503
504 describe('Array Schemas', () => {
505 it('should validate array items', () => {
506 const schema: JsonSchema = {
507 type: 'array',
508 items: { type: 'string' },
509 };
510 const zodSchema = builder.convert(schema);
511 expect(zodSchema.parse(['one', 'two'])).toEqual(['one', 'two']);
512 expect(() => zodSchema.parse(['one', 2])).toThrow();
513 });
514
515 it('should validate tuple items', () => {
516 const schema: JsonSchema = {
517 type: 'array',
518 items: [{ type: 'string' }, { type: 'number' }],
519 };
520 const zodSchema = builder.convert(schema);
521 expect(zodSchema.parse(['test', 123])).toEqual(['test', 123]);
522 expect(() => zodSchema.parse(['test', 'wrong'])).toThrow();
523 });
524
525 it('should validate array length', () => {
526 const schema: JsonSchema = {
527 type: 'array',
528 items: { type: 'string' },
529 minItems: 1,
530 maxItems: 3,
531 };
532 const zodSchema = builder.convert(schema);
533 expect(zodSchema.parse(['one'])).toEqual(['one']);
534 expect(zodSchema.parse(['one', 'two', 'three'])).toEqual(['one', 'two', 'three']);
535 expect(() => zodSchema.parse([])).toThrow();
536 expect(() => zodSchema.parse(['one', 'two', 'three', 'four'])).toThrow();
537 });
538
539 it('should handle generic min and max properties for arrays', () => {
540 const schema = {
541 type: 'array',
542 items: { type: 'string' },
543 min: 1,
544 max: 3,
545 } satisfies JsonSchema;
546 const zodSchema = builder.convert(schema);
547 expect(zodSchema.parse(['one'])).toEqual(['one']);
548 expect(zodSchema.parse(['one', 'two', 'three'])).toEqual(['one', 'two', 'three']);
549 expect(() => zodSchema.parse([])).toThrow();
550 expect(() => zodSchema.parse(['one', 'two', 'three', 'four'])).toThrow();
551 });
552
553 it('should prioritize minItems/maxItems over min/max for arrays', () => {
554 const schema = {
555 type: 'array',
556 items: { type: 'string' },
557 minItems: 2,
558 maxItems: 4,
559 min: 1,
560 max: 3,
561 } satisfies JsonSchema;
562 const zodSchema = builder.convert(schema);
563 expect(zodSchema.parse(['one', 'two'])).toEqual(['one', 'two']);
564 expect(zodSchema.parse(['one', 'two', 'three', 'four'])).toEqual([
565 'one',
566 'two',
567 'three',
568 'four',
569 ]);
570 expect(() => zodSchema.parse(['one'])).toThrow(); // Uses minItems (2), not min (1)
571 expect(() => zodSchema.parse(['one', 'two', 'three', 'four', 'five'])).toThrow(); // Uses maxItems (4), not max (3)
572 });
573
574 it('should handle arrays with anyOf patterns', () => {
575 const schema: JsonSchema = {
576 type: 'array',
577 anyOf: [
578 {
579 type: 'array',
580 items: { type: 'string' },
581 },
582 {
583 type: 'array',
584 items: { type: 'number' },
585 },
586 ],
587 };
588 const zodSchema = builder.convert(schema);
589
590 // Should accept string arrays
591 expect(zodSchema.parse(['hello', 'world'])).toEqual(['hello', 'world']);
592
593 // Should accept number arrays
594 expect(zodSchema.parse([1, 2, 3])).toEqual([1, 2, 3]);
595
596 // Each anyOf branch constrains the entire array, so a mixed array
597 // satisfies neither the all-string nor the all-number branch.
598 expect(() => zodSchema.parse(['hello', 123])).toThrow();
599 });
600
601 it('should handle arrays with anyOf at top level without explicit types', () => {
602 const schema: JsonSchema = {
603 type: 'array',
604 anyOf: [
605 {
606 items: { type: 'string' },
607 },
608 {
609 items: { type: 'number' },
610 },
611 ],
612 };
613 const zodSchema = builder.convert(schema);
614
615 // Should accept string arrays
616 expect(zodSchema.parse(['hello', 'world'])).toEqual(['hello', 'world']);
617
618 // Should accept number arrays
619 expect(zodSchema.parse([1, 2, 3])).toEqual([1, 2, 3]);
620 });
621
622 it('should handle arrays with object items having default null', () => {
623 const schema: JsonSchema = {
624 type: 'array',
625 items: {
626 type: 'object',
627 properties: {
628 name: { type: 'string' },
629 status: { type: 'string', default: null },
630 },
631 required: ['name'],
632 },
633 };
634 const zodSchema = builder.convert(schema);
635
636 // Should apply default null for missing status
637 expect(zodSchema.parse([{ name: 'test1' }, { name: 'test2', status: 'active' }])).toEqual([
638 { name: 'test1', status: null },
639 { name: 'test2', status: 'active' },
640 ]);
641
642 // Should fail for missing required field
643 expect(() => zodSchema.parse([{ status: 'active' }])).toThrow();
644 });
645
646 it('should handle nested arrays', () => {
647 const schema: JsonSchema = {
648 type: 'array',
649 items: {
650 type: 'array',
651 items: { type: 'number' },
652 },
653 };
654 const zodSchema = builder.convert(schema);
655
656 expect(
657 zodSchema.parse([
658 [1, 2],
659 [3, 4, 5],
660 ])
661 ).toEqual([
662 [1, 2],
663 [3, 4, 5],
664 ]);
665 expect(() => zodSchema.parse([['string', 2]])).toThrow();
666 });
667 });
668
669 describe('Enum Schemas', () => {
670 it('should validate string enums', () => {
671 const schema: JsonSchema = {
672 type: 'string',
673 enum: ['one', 'two', 'three'],
674 };
675 const zodSchema = builder.convert(schema);
676 expect(zodSchema.parse('one')).toBe('one');
677 expect(() => zodSchema.parse('four')).toThrow();
678 });
679
680 it('should validate mixed type enums', () => {
681 const schema: JsonSchema = {
682 enum: ['string', 123, true],
683 };
684 const zodSchema = builder.convert(schema);
685 expect(zodSchema.parse('string')).toBe('string');
686 expect(zodSchema.parse(123)).toBe(123);
687 expect(zodSchema.parse(true)).toBe(true);
688 expect(() => zodSchema.parse('invalid')).toThrow();
689 });
690 });
691
692 describe('Combining Schemas', () => {
693 it('should validate anyOf', () => {
694 const schema: JsonSchema = {
695 anyOf: [{ type: 'string' }, { type: 'number' }],
696 };
697 const zodSchema = builder.convert(schema);
698 expect(zodSchema.parse('test')).toBe('test');
699 expect(zodSchema.parse(123)).toBe(123);
700 expect(() => zodSchema.parse(true)).toThrow();
701 });
702
703 it('should validate oneOf', () => {
704 const schema: JsonSchema = {
705 oneOf: [
706 { type: 'number', minimum: 0 },
707 { type: 'number', maximum: 0 },
708 ],
709 };
710 const zodSchema = builder.convert(schema);
711 expect(zodSchema.parse(1)).toBe(1);
712 expect(zodSchema.parse(-1)).toBe(-1);
713 expect(() => zodSchema.parse(0)).toThrow(); // Should fail as 0 matches both schemas
714 });
715
716 it('should validate allOf', () => {
717 const schema: JsonSchema = {
718 allOf: [
719 { type: 'number', minimum: 0 },
720 { type: 'number', maximum: 100 },
721 ],
722 };
723 const zodSchema = builder.convert(schema);
724 expect(zodSchema.parse(50)).toBe(50);
725 expect(() => zodSchema.parse(-1)).toThrow();
726 expect(() => zodSchema.parse(101)).toThrow();
727 });
728 });
729
730 describe('Complex Nested Schemas', () => {
731 it('should validate deeply nested objects', () => {
732 const schema: JsonSchema = {
733 type: 'object',
734 properties: {
735 user: {
736 type: 'object',
737 properties: {
738 name: { type: 'string' },
739 contacts: {
740 type: 'array',
741 items: {
742 type: 'object',
743 properties: {
744 type: { type: 'string', enum: ['email', 'phone'] },
745 value: { type: 'string' },
746 },
747 required: ['type', 'value'],
748 },
749 },
750 },
751 required: ['name'],
752 },
753 },
754 required: ['user'],
755 };
756 const zodSchema = builder.convert(schema);
757
758 const validData = {
759 user: {
760 name: 'John Doe',
761 contacts: [
762 { type: 'email', value: 'john@example.com' },
763 { type: 'phone', value: '1234567890' },
764 ],
765 },
766 };
767
768 expect(zodSchema.parse(validData)).toEqual(validData);
769 expect(() =>
770 zodSchema.parse({
771 user: {
772 name: 'John Doe',
773 contacts: [{ type: 'invalid', value: 'test' }],
774 },
775 })
776 ).toThrow();
777 });
778 });
779
780 describe('Schema Metadata and Constraints', () => {
781 it('should handle required fields and descriptions', () => {
782 const schema: JsonSchema = {
783 type: 'object',
784 description: 'A user profile object',
785 properties: {
786 username: {
787 type: 'string',
788 description: "The user's display name",
789 },
790 email: {
791 type: 'string',
792 description: "The user's email address",
793 format: 'email',
794 },
795 age: {
796 type: 'number',
797 description: "User's age in years",
798 },
799 },
800 required: ['username', 'email'],
801 };
802
803 const zodSchema = builder.convert(schema);
804
805 // Test description
806 expect(builder.description(zodSchema)).toBe('A user profile object');
807
808 // Test required fields
809 expect(() => zodSchema.parse({})).toThrow();
810 expect(() => zodSchema.parse({ username: 'test' })).toThrow();
811 expect(() => zodSchema.parse({ email: 'test@example.com' })).toThrow();
812
813 // Valid data should pass
814 expect(
815 zodSchema.parse({
816 username: 'testuser',
817 email: 'test@example.com',
818 })
819 ).toEqual({
820 username: 'testuser',
821 email: 'test@example.com',
822 });
823
824 // Optional field should be allowed to be missing
825 expect(
826 zodSchema.parse({
827 username: 'testuser',
828 email: 'test@example.com',
829 })
830 ).toEqual({
831 username: 'testuser',
832 email: 'test@example.com',
833 });
834 });
835
836 it('should handle properties without type field', () => {
837 const schema: JsonSchema = {
838 type: 'object',
839 properties: {
840 withType: {
841 type: 'string',
842 description: 'Property with type',
843 },
844 withoutType: {
845 description: 'Property without type',
846 default: 'default value',
847 },
848 withAnyOf: {
849 anyOf: [{ type: 'string' }, { type: 'number' }],
850 description: 'Property with anyOf but no direct type',
851 },
852 withEnum: {
853 enum: ['one', 'two', 'three'],
854 description: 'Property with enum but no type',
855 },
856 },
857 };
858
859 const zodSchema = builder.convert(schema);
860
861 // Test property with type
862 expect(zodSchema.parse({ withType: 'test' })).toEqual({
863 withType: 'test',
864 withoutType: 'default value',
865 });
866 expect(() => zodSchema.parse({ withType: 123 })).toThrow();
867
868 // Test property without type (should accept any value)
869 expect(zodSchema.parse({ withoutType: 'string value' })).toEqual({
870 withoutType: 'string value', // Override default
871 });
872 expect(zodSchema.parse({ withoutType: 123 })).toEqual({
873 withoutType: 123, // Override default
874 });
875 expect(zodSchema.parse({ withoutType: { nested: true } })).toEqual({
876 withoutType: { nested: true }, // Override default
877 });
878
879 // Test property with anyOf but no direct type
880 expect(zodSchema.parse({ withAnyOf: 'string value' })).toEqual({
881 withAnyOf: 'string value',
882 withoutType: 'default value', // Default value is included
883 });
884 expect(zodSchema.parse({ withAnyOf: 123 })).toEqual({
885 withAnyOf: 123,
886 withoutType: 'default value', // Default value is included
887 });
888 expect(() => zodSchema.parse({ withAnyOf: true })).toThrow();
889
890 // Test property with enum but no type
891 expect(zodSchema.parse({ withEnum: 'one' })).toEqual({
892 withEnum: 'one',
893 withoutType: 'default value', // Default value is included
894 });
895 expect(() => zodSchema.parse({ withEnum: 'invalid' })).toThrow();
896
897 // Test that default value is included when property is missing
898 expect(zodSchema.parse({})).toEqual({
899 withoutType: 'default value',
900 });
901 });
902
903 it('should handle default values', () => {
904 const schema: JsonSchema = {
905 type: 'object',
906 properties: {
907 name: {
908 type: 'string',
909 default: 'Anonymous',
910 },
911 settings: {
912 type: 'object',
913 default: { theme: 'light' },
914 properties: {
915 theme: {
916 type: 'string',
917 },
918 },
919 },
920 },
921 };
922
923 const zodSchema = builder.convert(schema);
924
925 // Test default values for top-level property
926 expect(zodSchema.parse({})).toEqual({
927 name: 'Anonymous',
928 settings: { theme: 'light' },
929 });
930
931 // Test that defaults don't override provided values
932 expect(
933 zodSchema.parse({
934 name: 'John',
935 settings: {
936 theme: 'dark',
937 },
938 })
939 ).toEqual({
940 name: 'John',
941 settings: {
942 theme: 'dark',
943 },
944 });
945 });
946
947 it('should handle default null values', () => {
948 const schema: JsonSchema = {
949 type: 'object',
950 properties: {
951 name: {
952 type: 'string',
953 description: 'The name field',
954 },
955 status: {
956 type: 'string',
957 default: null,
958 description: 'A field with default null',
959 },
960 age: {
961 type: 'number',
962 description: 'Age field without default',
963 },
964 },
965 required: ['name'],
966 };
967
968 const zodSchema = builder.convert(schema);
969
970 // Test with required field only - should include null default
971 expect(zodSchema.parse({ name: 'John' })).toEqual({
972 name: 'John',
973 status: null,
974 });
975
976 // Test with status explicitly set to null
977 expect(zodSchema.parse({ name: 'John', status: null })).toEqual({
978 name: 'John',
979 status: null,
980 });
981
982 // Test with status set to a string value
983 expect(zodSchema.parse({ name: 'John', status: 'active' })).toEqual({
984 name: 'John',
985 status: 'active',
986 });
987
988 // Test that required field validation still works
989 expect(() => zodSchema.parse({})).toThrow();
990 });
991
992 it('should transform GetUserRequest schema correctly', () => {
993 const schema: JsonSchema = {
994 type: 'object',
995 properties: {
996 username: {
997 type: 'string',
998 description: 'The username of the Hacker News user to retrieve.',
999 title: 'Username',
1000 },
1001 },
1002 required: ['username'],
1003 title: 'GetUserRequest',
1004 };
1005
1006 const zodSchema = builder.convert(schema);
1007
1008 // Test schema description/title
1009 expect(builder.description(zodSchema)).toBe('GetUserRequest');
1010
1011 // Test required field validation
1012 expect(() => zodSchema.parse({})).toThrow();
1013 expect(() => zodSchema.parse({ username: 123 })).toThrow();
1014
1015 // Test valid data
1016 expect(zodSchema.parse({ username: 'testuser' })).toEqual({
1017 username: 'testuser',
1018 });
1019
1020 // Test property description
1021 const shape = builder.shape(zodSchema);
1022 expect(builder.description(shape.username)).toBe(
1023 'The username of the Hacker News user to retrieve.'
1024 );
1025 });
1026
1027 it('should append example to description', () => {
1028 const schema = {
1029 type: 'string',
1030 description: 'User name',
1031 example: 'john_doe',
1032 } satisfies JsonSchema;
1033 const zodSchema = builder.convert(schema);
1034 expect(builder.description(zodSchema)).toBe('User name\nExample: "john_doe"');
1035 });
1036
1037 it('should handle example without description', () => {
1038 const schema = {
1039 type: 'number',
1040 example: 42,
1041 } satisfies JsonSchema;
1042 const zodSchema = builder.convert(schema);
1043 expect(builder.description(zodSchema)).toBe('Example: 42');
1044 });
1045
1046 it('should handle example with title but no description', () => {
1047 const schema = {
1048 type: 'object',
1049 title: 'User Object',
1050 example: { name: 'John', age: 30 },
1051 } satisfies JsonSchema;
1052 const zodSchema = builder.convert(schema);
1053 expect(builder.description(zodSchema)).toBe('User Object\nExample: {"name":"John","age":30}');
1054 });
1055
1056 it('should handle single example in examples array', () => {
1057 const schema = {
1058 type: 'string',
1059 description: 'Property name',
1060 examples: ['lifecyclestage'],
1061 } satisfies JsonSchema;
1062 const zodSchema = builder.convert(schema);
1063 expect(builder.description(zodSchema)).toBe('Property name\nExample: "lifecyclestage"');
1064 });
1065
1066 it('should handle multiple examples in examples array', () => {
1067 const schema = {
1068 type: 'array',
1069 description: 'List of property names',
1070 examples: ["['lifecyclestage', 'hs_lead_status']", "['hubspot_owner_id']"],
1071 } satisfies JsonSchema;
1072 const zodSchema = builder.convert(schema);
1073 expect(builder.description(zodSchema)).toBe(
1074 "List of property names\nExamples:\n \"['lifecyclestage', 'hs_lead_status']\"\n \"['hubspot_owner_id']\""
1075 );
1076 });
1077
1078 it('should prioritize example over examples', () => {
1079 const schema = {
1080 type: 'string',
1081 description: 'Property name',
1082 example: 'single_example',
1083 examples: ['example1', 'example2'],
1084 } satisfies JsonSchema;
1085 const zodSchema = builder.convert(schema);
1086 expect(builder.description(zodSchema)).toBe('Property name\nExample: "single_example"');
1087 });
1088
1089 it('should handle empty examples array', () => {
1090 const schema = {
1091 type: 'string',
1092 description: 'Property name',
1093 examples: [],
1094 } satisfies JsonSchema;
1095 const zodSchema = builder.convert(schema);
1096 expect(builder.description(zodSchema)).toBe('Property name');
1097 });
1098
1099 it('should preserve array structure in examples', () => {
1100 const schema = {
1101 type: 'array',
1102 description: 'Property combinations',
1103 examples: [
1104 ['lifecyclestage', 'hs_lead_status'],
1105 ['hubspot_owner_id'],
1106 ['created_date', 'modified_date', 'status'],
1107 ],
1108 } satisfies JsonSchema;
1109 const zodSchema = builder.convert(schema);
1110 expect(builder.description(zodSchema)).toBe(
1111 'Property combinations\nExamples:\n ["lifecyclestage","hs_lead_status"]\n ["hubspot_owner_id"]\n ["created_date","modified_date","status"]'
1112 );
1113 });
1114
1115 it('should handle primitive types in examples array', () => {
1116 const schema = {
1117 type: 'string',
1118 description: 'Status values',
1119 examples: ['active', 'inactive', 'pending'],
1120 } satisfies JsonSchema;
1121 const zodSchema = builder.convert(schema);
1122 expect(builder.description(zodSchema)).toBe(
1123 'Status values\nExamples:\n "active"\n "inactive"\n "pending"'
1124 );
1125 });
1126
1127 it('should handle mixed primitive types in examples array', () => {
1128 const schema = {
1129 type: 'number',
1130 description: 'Various numbers',
1131 examples: [42, 3.14, 0, -5],
1132 } satisfies JsonSchema;
1133 const zodSchema = builder.convert(schema);
1134 expect(builder.description(zodSchema)).toBe(
1135 'Various numbers\nExamples:\n 42\n 3.14\n 0\n -5'
1136 );
1137 });
1138 });
1139
1140 describe('Object Assertions Without Explicit Type', () => {
1141 it('should apply properties only to object instances', () => {
1142 const schema: JsonSchema = {
1143 properties: {
1144 name: { type: 'string' },
1145 age: { type: 'number' },
1146 },
1147 required: ['name'],
1148 };
1149 const zodSchema = builder.convert(schema);
1150
1151 expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
1152 expect(zodSchema.parse({ name: 'Jane' })).toEqual({ name: 'Jane' });
1153 expect(() => zodSchema.parse({ age: 30 })).toThrow(); // Missing required field
1154 expect(zodSchema.parse('not an object')).toBe('not an object');
1155 });
1156
1157 it('should detect object schema with only additionalProperties defined', () => {
1158 const schema: JsonSchema = {
1159 additionalProperties: { type: 'string' },
1160 };
1161 const zodSchema = builder.convert(schema);
1162
1163 expect(zodSchema.parse({})).toEqual({});
1164 expect(zodSchema.parse({ key1: 'value1', key2: 'value2' })).toEqual({
1165 key1: 'value1',
1166 key2: 'value2',
1167 });
1168 expect(() => zodSchema.parse({ key1: 123 })).toThrow(); // Wrong type
1169 expect(zodSchema.parse('not an object')).toBe('not an object');
1170 });
1171
1172 it('should detect object schema with only patternProperties defined', () => {
1173 const schema: JsonSchema = {
1174 patternProperties: {
1175 '^prefix_': { type: 'string' },
1176 '^num_': { type: 'number' },
1177 },
1178 };
1179 const zodSchema = builder.convert(schema);
1180
1181 expect(zodSchema.parse({})).toEqual({});
1182 expect(zodSchema.parse({ prefix_one: 'value', num_one: 123 })).toEqual({
1183 prefix_one: 'value',
1184 num_one: 123,
1185 });
1186 expect(() => zodSchema.parse({ prefix_one: 123 })).toThrow(); // Wrong type for pattern
1187 expect(zodSchema.parse('not an object')).toBe('not an object');
1188 });
1189
1190 it('should detect object schema with properties and additionalProperties but no type', () => {
1191 const schema: JsonSchema = {
1192 properties: {
1193 name: { type: 'string' },
1194 },
1195 additionalProperties: { type: 'number' },
1196 };
1197 const zodSchema = builder.convert(schema);
1198
1199 expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
1200 expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
1201 expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
1202 });
1203
1204 it('should detect object schema with properties and patternProperties but no type', () => {
1205 const schema: JsonSchema = {
1206 properties: {
1207 name: { type: 'string' },
1208 },
1209 patternProperties: {
1210 '^data_': { type: 'number' },
1211 },
1212 additionalProperties: false,
1213 };
1214 const zodSchema = builder.convert(schema);
1215
1216 expect(zodSchema.parse({ name: 'John', data_age: 30 })).toEqual({
1217 name: 'John',
1218 data_age: 30,
1219 });
1220 expect(() => zodSchema.parse({ name: 'John', data_age: 'not a number' })).toThrow();
1221 expect(() => zodSchema.parse({ name: 'John', extra: 'field' })).toThrow();
1222 });
1223
1224 it('should detect nested objects without explicit type', () => {
1225 const schema: JsonSchema = {
1226 type: 'object',
1227 properties: {
1228 user: {
1229 // No type specified, but has properties
1230 properties: {
1231 name: { type: 'string' },
1232 email: { type: 'string', format: 'email' },
1233 },
1234 required: ['name'],
1235 },
1236 metadata: {
1237 // No type specified, but has additionalProperties
1238 additionalProperties: { type: 'string' },
1239 },
1240 },
1241 };
1242 const zodSchema = builder.convert(schema);
1243
1244 const validData = {
1245 user: { name: 'John', email: 'john@example.com' },
1246 metadata: { key1: 'value1', key2: 'value2' },
1247 };
1248 expect(zodSchema.parse(validData)).toEqual(validData);
1249
1250 // Test nested validation
1251 expect(() =>
1252 zodSchema.parse({
1253 user: { email: 'john@example.com' }, // Missing required name
1254 })
1255 ).toThrow();
1256
1257 expect(() =>
1258 zodSchema.parse({
1259 metadata: { key1: 123 }, // Wrong type
1260 })
1261 ).toThrow();
1262 });
1263
1264 it('should handle arrays containing objects without explicit type', () => {
1265 const schema: JsonSchema = {
1266 type: 'array',
1267 items: {
1268 // No type specified, but has properties
1269 properties: {
1270 id: { type: 'number' },
1271 name: { type: 'string' },
1272 },
1273 required: ['id'],
1274 },
1275 };
1276 const zodSchema = builder.convert(schema);
1277
1278 expect(zodSchema.parse([{ id: 1, name: 'Item 1' }, { id: 2 }])).toEqual([
1279 { id: 1, name: 'Item 1' },
1280 { id: 2 },
1281 ]);
1282
1283 expect(() => zodSchema.parse([{ name: 'Item without ID' }])).toThrow();
1284 });
1285
1286 it('should handle complex nested structure without explicit types', () => {
1287 const schema: JsonSchema = {
1288 type: 'object',
1289 properties: {
1290 config: {
1291 // No type, but has properties
1292 properties: {
1293 settings: {
1294 // No type, but has additionalProperties
1295 additionalProperties: {
1296 // No type, but has properties
1297 properties: {
1298 enabled: { type: 'boolean' },
1299 value: { type: 'string' },
1300 },
1301 },
1302 },
1303 tags: {
1304 type: 'array',
1305 items: {
1306 // No type, but has properties
1307 properties: {
1308 key: { type: 'string' },
1309 value: { type: 'string' },
1310 },
1311 required: ['key'],
1312 },
1313 },
1314 },
1315 },
1316 },
1317 };
1318 const zodSchema = builder.convert(schema);
1319
1320 const validData = {
1321 config: {
1322 settings: {
1323 feature1: { enabled: true, value: 'on' },
1324 feature2: { enabled: false, value: 'off' },
1325 },
1326 tags: [{ key: 'env', value: 'production' }, { key: 'region' }],
1327 },
1328 };
1329 expect(zodSchema.parse(validData)).toEqual(validData);
1330 });
1331
1332 it('should handle anyOf with objects without explicit type', () => {
1333 const schema: JsonSchema = {
1334 anyOf: [
1335 {
1336 // No type, but has properties
1337 properties: {
1338 name: { type: 'string' },
1339 },
1340 },
1341 {
1342 type: 'string',
1343 },
1344 ],
1345 };
1346 const zodSchema = builder.convert(schema);
1347
1348 expect(zodSchema.parse({ name: 'John' })).toEqual({ name: 'John' });
1349 expect(zodSchema.parse('just a string')).toBe('just a string');
1350 });
1351
1352 it('should handle oneOf with objects without explicit type', () => {
1353 const schema: JsonSchema = {
1354 oneOf: [
1355 {
1356 // No type, but has properties
1357 properties: {
1358 type: { type: 'string', enum: ['user'] },
1359 name: { type: 'string' },
1360 },
1361 required: ['type'],
1362 },
1363 {
1364 // No type, but has properties
1365 properties: {
1366 type: { type: 'string', enum: ['admin'] },
1367 role: { type: 'string' },
1368 },
1369 required: ['type'],
1370 },
1371 ],
1372 };
1373 const zodSchema = builder.convert(schema);
1374
1375 expect(zodSchema.parse({ type: 'user', name: 'John' })).toEqual({
1376 type: 'user',
1377 name: 'John',
1378 });
1379 expect(zodSchema.parse({ type: 'admin', role: 'superadmin' })).toEqual({
1380 type: 'admin',
1381 role: 'superadmin',
1382 });
1383 });
1384
1385 it('should handle allOf with explicit object types (requires additionalProperties for merging)', () => {
1386 // Note: allOf with strict objects (without additionalProperties) creates strict intersections
1387 // Each object must allow additional properties for the intersection to accept merged fields
1388 const schema: JsonSchema = {
1389 allOf: [
1390 {
1391 type: 'object',
1392 properties: {
1393 name: { type: 'string' },
1394 },
1395 required: ['name'],
1396 additionalProperties: true, // Required for allOf to work with merged fields
1397 },
1398 {
1399 type: 'object',
1400 properties: {
1401 age: { type: 'number' },
1402 },
1403 additionalProperties: true, // Required for allOf to work with merged fields
1404 },
1405 ],
1406 };
1407 const zodSchema = builder.convert(schema);
1408
1409 expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
1410 expect(zodSchema.parse({ name: 'Jane' })).toEqual({ name: 'Jane' });
1411 expect(() => zodSchema.parse({ age: 30 })).toThrow(); // Missing required name
1412 });
1413
1414 it('should handle allOf with implicit object detection (note: creates strict intersections)', () => {
1415 // When objects in allOf don't have explicit types, they are detected as objects
1416 // but the intersection behavior is strict
1417 const schema: JsonSchema = {
1418 allOf: [
1419 {
1420 // No type, but has properties - will be detected as object
1421 properties: {
1422 name: { type: 'string' },
1423 },
1424 required: ['name'],
1425 additionalProperties: true, // Need to allow additional properties for intersection to work
1426 },
1427 {
1428 // No type, but has properties - will be detected as object
1429 properties: {
1430 age: { type: 'number' },
1431 },
1432 additionalProperties: true, // Need to allow additional properties for intersection to work
1433 },
1434 ],
1435 };
1436 const zodSchema = builder.convert(schema);
1437
1438 // With additionalProperties: true, the intersection allows all properties
1439 expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
1440 expect(zodSchema.parse({ name: 'Jane' })).toEqual({ name: 'Jane' });
1441 expect(() => zodSchema.parse({ age: 30 })).toThrow(); // Missing required name
1442 });
1443
1444 it('should correctly identify objects in its.an.object utility', () => {
1445 const schema: JsonSchema = {
1446 type: 'object',
1447 properties: {
1448 explicit: {
1449 type: 'object',
1450 properties: { a: { type: 'string' } },
1451 },
1452 implicitFromProperties: {
1453 properties: { b: { type: 'number' } },
1454 },
1455 implicitFromAdditionalProperties: {
1456 additionalProperties: { type: 'boolean' },
1457 },
1458 implicitFromPatternProperties: {
1459 patternProperties: {
1460 '^[a-z]+$': { type: 'string' },
1461 },
1462 },
1463 notAnObject: {
1464 type: 'string',
1465 },
1466 },
1467 };
1468 const zodSchema = builder.convert(schema);
1469
1470 const validData = {
1471 explicit: { a: 'test' },
1472 implicitFromProperties: { b: 123 },
1473 implicitFromAdditionalProperties: { any: true, keys: false },
1474 implicitFromPatternProperties: { abc: 'value' },
1475 notAnObject: 'just a string',
1476 };
1477 expect(zodSchema.parse(validData)).toEqual(validData);
1478
1479 // Test each property type validation
1480 expect(() => zodSchema.parse({ ...validData, explicit: 'not an object' })).toThrow();
1481 expect(() =>
1482 zodSchema.parse({ ...validData, implicitFromProperties: { b: 'wrong' } })
1483 ).toThrow();
1484 expect(() =>
1485 zodSchema.parse({ ...validData, implicitFromAdditionalProperties: { key: 'wrong' } })
1486 ).toThrow();
1487 expect(() =>
1488 zodSchema.parse({ ...validData, implicitFromPatternProperties: { abc: 123 } })
1489 ).toThrow();
1490 });
1491
1492 it('should handle empty object schemas without explicit type', () => {
1493 const schema: JsonSchema = {
1494 properties: {},
1495 };
1496 const zodSchema = builder.convert(schema);
1497
1498 expect(zodSchema.parse({})).toEqual({});
1499 expect(zodSchema.parse('not an object')).toBe('not an object');
1500 });
1501
1502 it('should handle objects with only required field but no type', () => {
1503 const schema: JsonSchema = {
1504 properties: {
1505 name: { type: 'string' },
1506 age: { type: 'number' },
1507 },
1508 required: ['name', 'age'],
1509 };
1510 const zodSchema = builder.convert(schema);
1511
1512 expect(zodSchema.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
1513 expect(() => zodSchema.parse({ name: 'John' })).toThrow();
1514 expect(() => zodSchema.parse({ age: 30 })).toThrow();
1515 });
1516 });
1517 });
1518
1519 describe.each(builders)('issue paths ($name)', builder => {
1520 const issuePaths = (schema: JsonSchema, value: unknown) => {
1521 const result = builder.convert(schema).safeParse(value);
1522 expect(result.success).toBe(false);
1523 return result.success ? [] : result.error.issues.map(issue => [issue.code, issue.path]);
1524 };
1525
1526 it('reports a nested patternProperties violation under its property once', () => {
1527 const schema: JsonSchema = {
1528 type: 'object',
1529 properties: {
1530 x: { type: 'object', patternProperties: { '^p_': { type: 'number' } } },
1531 },
1532 };
1533 expect(issuePaths(schema, { x: { p_a: 'no' } })).toEqual([['custom', ['x', 'p_a']]]);
1534 });
1535
1536 it('reports a nested additionalProperties violation under its property once', () => {
1537 const schema: JsonSchema = {
1538 type: 'object',
1539 properties: {
1540 x: {
1541 type: 'object',
1542 patternProperties: { '^p_': { type: 'number' } },
1543 additionalProperties: { type: 'string' },
1544 },
1545 },
1546 };
1547 expect(issuePaths(schema, { x: { other: 1 } })).toEqual([['custom', ['x', 'other']]]);
1548 });
1549
1550 it('reports nested unrecognized keys at the property that rejects them', () => {
1551 const schema: JsonSchema = {
1552 type: 'object',
1553 properties: {
1554 x: {
1555 type: 'object',
1556 properties: { a: { type: 'string' } },
1557 patternProperties: { '^p_': { type: 'number' } },
1558 additionalProperties: false,
1559 },
1560 },
1561 };
1562 expect(issuePaths(schema, { x: { zzz: 1 } })).toEqual([['unrecognized_keys', ['x']]]);
1563 });
1564
1565 it('reports a nested oneOf failure at the property once', () => {
1566 // `contentEncoding` is an annotation to the Draft 7 guard but a check to
1567 // the native string parser, so the guard accepts exactly one branch while
1568 // the parser rejects both and the oneOf refinement itself reports.
1569 const schema: JsonSchema = {
1570 type: 'object',
1571 properties: {
1572 x: {
1573 oneOf: [
1574 { type: 'string', contentEncoding: 'base64' },
1575 { type: 'string', maxLength: 1 },
1576 ],
1577 },
1578 },
1579 };
1580 expect(issuePaths(schema, { x: '!!!' })).toEqual([['invalid_union', ['x']]]);
1581 });
1582 });
1583
1584 describe.each(builders)('shared cross-SDK object corpus ($name)', builder => {
1585 for (const testCase of loadObjectCases()) {
1586 describe(testCase.id, () => {
1587 for (const [index, instance] of testCase.instances.entries()) {
1588 const expected = acceptedFor(instance, 'zod');
1589
1590 const name = `instance ${index} is ${expected ? 'accepted' : 'rejected'}`;
1591
1592 testFor(builder, `${testCase.id} > ${name}`)(name, () => {
1593 const zodSchema = builder.convert(testCase.schema as JsonSchema);
1594 const result = zodSchema.safeParse(instance.input);
1595
1596 expect(result.success).toBe(expected);
1597 if (result.success && instance.zod && 'output' in instance.zod) {
1598 expect(result.data).toEqual(instance.zod.output);
1599 }
1600 });
1601 }
1602 });
1603 }
1604 });