forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-schema.test.ts
More file actions
812 lines (707 loc) · 22.8 KB
/
Copy pathjson-schema.test.ts
File metadata and controls
812 lines (707 loc) · 22.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
import { describe, it, expect, vi } from "vitest";
import { z } from "zod";
import {
convertJsonSchemaToZodSchema,
actionParametersToJsonSchema,
jsonSchemaToActionParameters,
JSONSchema,
} from "../json-schema";
import { zodToJsonSchema } from "zod-to-json-schema";
import { Parameter } from "../../types";
describe("convertJsonSchemaToZodSchema", () => {
it("should convert a simple JSON schema to a Zod schema", () => {
const jsonSchema = {
type: "object",
properties: {
name: { type: "string" },
age: { type: "number" },
},
required: ["name", "age"],
};
const expectedSchema = z.object({
name: z.string(),
age: z.number(),
});
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
const resultSchemaJson = zodToJsonSchema(result);
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
});
it("should convert a JSON schema with nested objects to a Zod schema", () => {
const jsonSchema = {
type: "object",
properties: {
name: { type: "string" },
address: {
type: "object",
properties: {
street: { type: "string" },
city: { type: "string" },
},
required: ["street", "city"],
},
},
required: ["name", "address"],
};
const expectedSchema = z.object({
name: z.string(),
address: z.object({
street: z.string(),
city: z.string(),
}),
});
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
const resultSchemaJson = zodToJsonSchema(result);
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
});
it("should convert a JSON schema with arrays to a Zod schema", () => {
const jsonSchema = {
type: "object",
properties: {
names: {
type: "array",
items: { type: "string" },
},
},
required: ["names"],
};
const expectedSchema = z.object({
names: z.array(z.string()),
});
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
const resultSchemaJson = zodToJsonSchema(result);
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
});
it("should convert a JSON schema with optional properties to a Zod schema", () => {
const jsonSchema = {
type: "object",
properties: {
name: { type: "string" },
age: { type: "number", required: false },
},
};
const expectedSchema = z
.object({
name: z.string().optional(),
age: z.number().optional(),
})
.optional();
const result = convertJsonSchemaToZodSchema(jsonSchema, false);
console.log(convertJsonSchemaToZodSchema(jsonSchema, false));
const resultSchemaJson = zodToJsonSchema(result);
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
});
it("should convert a JSON schema with different types to a Zod schema", () => {
const jsonSchema = {
type: "object",
properties: {
name: { type: "string" },
age: { type: "number" },
isAdmin: { type: "boolean" },
},
required: ["name", "age", "isAdmin"],
};
const expectedSchema = z.object({
name: z.string(),
age: z.number(),
isAdmin: z.boolean(),
});
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
const resultSchemaJson = zodToJsonSchema(result);
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
});
it("should handle edge case where JSON schema has no properties", () => {
const jsonSchema = {
type: "object",
};
const expectedSchema = z.object({});
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
const resultSchemaJson = zodToJsonSchema(result);
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
});
it("should preserve string enum constraints", () => {
const jsonSchema = {
type: "object",
properties: {
status: {
type: "string",
description: "The status",
enum: ["todo", "done"],
},
},
required: ["status"],
};
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
const statusSchema = (result as z.ZodObject<any>).shape.status;
expect(statusSchema._def.typeName).toBe("ZodEnum");
expect(statusSchema._def.values).toEqual(["todo", "done"]);
});
it("should handle null-union type ['string', 'null'] as nullable", () => {
const jsonSchema = {
type: "object",
properties: {
nickname: {
type: ["string", "null"],
description: "Optional nickname",
},
},
required: ["nickname"],
};
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
const shape = (result as z.ZodObject<any>).shape;
// The nickname field should accept both string and null
expect(shape.nickname.safeParse("hello").success).toBe(true);
expect(shape.nickname.safeParse(null).success).toBe(true);
expect(shape.nickname.safeParse(42).success).toBe(false);
});
it("should handle null-union type ['number', 'null'] as nullable number", () => {
const jsonSchema = {
type: "object",
properties: {
score: {
type: ["number", "null"],
},
},
required: ["score"],
};
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
const shape = (result as z.ZodObject<any>).shape;
expect(shape.score.safeParse(42).success).toBe(true);
expect(shape.score.safeParse(null).success).toBe(true);
expect(shape.score.safeParse("hello").success).toBe(false);
});
it("should handle edge case where JSON schema has no required properties", () => {
const jsonSchema = {
type: "object",
properties: {
name: { type: "string" },
age: { type: "number" },
},
};
const expectedSchema = z
.object({
name: z.string().optional(),
age: z.number().optional(),
})
.optional();
const result = convertJsonSchemaToZodSchema(jsonSchema, false);
const resultSchemaJson = zodToJsonSchema(result);
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
});
it("should resolve non-circular $ref definitions correctly", () => {
const jsonSchema = {
type: "object",
properties: {
address: { $ref: "#/$defs/Address" },
},
required: ["address"],
$defs: {
Address: {
type: "object",
properties: {
street: { type: "string", description: "Street name" },
city: { type: "string", description: "City name" },
},
required: ["street", "city"],
description: "A postal address",
},
},
};
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
const resultJson = zodToJsonSchema(result);
const expectedSchema = z.object({
address: z
.object({
street: z.string().describe("Street name"),
city: z.string().describe("City name"),
})
.describe("A postal address"),
});
const expectedJson = zodToJsonSchema(expectedSchema);
expect(resultJson).toStrictEqual(expectedJson);
});
it("should handle circular $ref without crashing and return z.any()", () => {
// A schema where Node references itself — this would cause infinite
// recursion without cycle detection.
const jsonSchema = {
type: "object",
properties: {
root: { $ref: "#/$defs/Node" },
},
required: ["root"],
$defs: {
Node: {
type: "object",
properties: {
value: { type: "string", description: "Node value" },
child: { $ref: "#/$defs/Node" },
},
required: ["value"],
description: "A tree node",
},
},
};
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
// Must not throw or hang
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
expect(result).toBeDefined();
// The circular ref should have produced a console.warn
expect(warnSpy).toHaveBeenCalledWith(
expect.stringContaining("Circular $ref detected"),
);
// The top-level shape should still have a "root" key that is an object
const shape = (result as z.ZodObject<any>).shape;
expect(shape.root).toBeDefined();
// Inside root, "value" should be a string and "child" should be z.any()
// (child is optional since it's not in required[], so unwrap ZodOptional)
const rootShape = (shape.root as z.ZodObject<any>).shape;
expect(rootShape.value._def.typeName).toBe("ZodString");
const childDef = rootShape.child._def;
if (childDef.typeName === "ZodOptional") {
expect(childDef.innerType._def.typeName).toBe("ZodAny");
} else {
expect(childDef.typeName).toBe("ZodAny");
}
warnSpy.mockRestore();
});
it("should resolve the same $ref used by multiple sibling properties", () => {
// Two properties reference the same $def — the visited set must NOT
// mark the second usage as circular.
const jsonSchema = {
type: "object",
properties: {
billing: { $ref: "#/$defs/Address" },
shipping: { $ref: "#/$defs/Address" },
},
required: ["billing", "shipping"],
$defs: {
Address: {
type: "object",
properties: {
street: { type: "string", description: "Street" },
city: { type: "string", description: "City" },
},
required: ["street", "city"],
description: "An address",
},
},
};
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
const shape = (result as z.ZodObject<any>).shape;
// Both should be fully resolved objects, NOT z.any()
expect(shape.billing._def.typeName).toBe("ZodObject");
expect(shape.shipping._def.typeName).toBe("ZodObject");
// No circular-ref warning should have been emitted
expect(warnSpy).not.toHaveBeenCalled();
warnSpy.mockRestore();
});
it("should resolve a shared $ref used in different branches of a $ref chain", () => {
// Wrapper -> Container (via $ref) which has two children both using $ref to Leaf.
// Without set cloning, the second Leaf ref in Container would be wrongly flagged
// as circular because the first Leaf resolution already added it to the set.
const jsonSchema = {
type: "object",
properties: {
wrapper: { $ref: "#/$defs/Container" },
},
required: ["wrapper"],
$defs: {
Container: {
type: "object",
properties: {
first: { $ref: "#/$defs/Leaf" },
second: { $ref: "#/$defs/Leaf" },
},
required: ["first", "second"],
description: "A container with two leaves",
},
Leaf: {
type: "object",
properties: {
label: { type: "string", description: "Leaf label" },
},
required: ["label"],
description: "A leaf node",
},
},
};
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
const wrapperShape = (
(result as z.ZodObject<any>).shape.wrapper as z.ZodObject<any>
).shape;
// Both first and second should be fully resolved Leaf objects
expect(wrapperShape.first._def.typeName).toBe("ZodObject");
expect(wrapperShape.second._def.typeName).toBe("ZodObject");
// No circular-ref warning should have been emitted
expect(warnSpy).not.toHaveBeenCalled();
warnSpy.mockRestore();
});
it("should handle anyOf with $ref variants", () => {
const jsonSchema = {
type: "object",
properties: {
pet: {
anyOf: [{ $ref: "#/$defs/Cat" }, { $ref: "#/$defs/Dog" }],
description: "A pet",
},
},
required: ["pet"],
$defs: {
Cat: {
type: "object",
properties: { meow: { type: "boolean" } },
required: ["meow"],
},
Dog: {
type: "object",
properties: { bark: { type: "boolean" } },
required: ["bark"],
},
},
};
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
expect(result).toBeDefined();
// Should produce a union inside the "pet" property
const petSchema = (result as z.ZodObject<any>).shape.pet;
expect(petSchema._def.typeName).toBe("ZodUnion");
});
it("should handle integer type as z.number()", () => {
const jsonSchema = {
type: "object",
properties: {
count: { type: "integer", description: "A count" },
},
required: ["count"],
};
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
const shape = (result as z.ZodObject<any>).shape;
expect(shape.count._def.typeName).toBe("ZodNumber");
});
it("should handle null type", () => {
const jsonSchema = {
type: "object",
properties: {
empty: { type: "null", description: "Always null" },
},
required: ["empty"],
};
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
const shape = (result as z.ZodObject<any>).shape;
expect(shape.empty._def.typeName).toBe("ZodNull");
});
it("should warn and return z.any() for unsupported schema types", () => {
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
const jsonSchema = { type: "custom_unsupported" };
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
expect(result._def.typeName).toBe("ZodAny");
expect(warnSpy).toHaveBeenCalledWith(
expect.stringContaining(
'Unsupported JSON schema type "custom_unsupported"',
),
);
warnSpy.mockRestore();
});
});
describe("jsonSchemaToActionParameters", () => {
it("should convert a simple JSONSchema to Parameter array", () => {
const jsonSchema: JSONSchema = {
type: "object",
properties: {
name: { type: "string", description: "User name" },
age: { type: "number", description: "User age" },
},
required: ["name"],
};
const expectedParameters: Parameter[] = [
{ name: "name", type: "string", description: "User name" },
{ name: "age", type: "number", description: "User age", required: false },
];
const result = jsonSchemaToActionParameters(jsonSchema);
expect(result).toEqual(expectedParameters);
});
it("should convert JSONSchema with enum to Parameter array", () => {
const jsonSchema: JSONSchema = {
type: "object",
properties: {
role: {
type: "string",
enum: ["admin", "user", "guest"],
description: "User role",
},
},
required: ["role"],
};
const expectedParameters: Parameter[] = [
{
name: "role",
type: "string",
enum: ["admin", "user", "guest"],
description: "User role",
},
];
const result = jsonSchemaToActionParameters(jsonSchema);
expect(result).toEqual(expectedParameters);
});
it("should convert nested object JSONSchema to Parameter array", () => {
const jsonSchema: JSONSchema = {
type: "object",
properties: {
user: {
type: "object",
properties: {
name: { type: "string", description: "User name" },
age: { type: "number", description: "User age" },
},
required: ["name"],
description: "User information",
},
},
required: ["user"],
};
const expectedParameters: Parameter[] = [
{
name: "user",
type: "object",
description: "User information",
attributes: [
{ name: "name", type: "string", description: "User name" },
{
name: "age",
type: "number",
description: "User age",
required: false,
},
],
},
];
const result = jsonSchemaToActionParameters(jsonSchema);
expect(result).toEqual(expectedParameters);
});
it("should convert array JSONSchema to Parameter array", () => {
const jsonSchema: JSONSchema = {
type: "object",
properties: {
tags: {
type: "array",
items: { type: "string" },
description: "User tags",
},
},
required: ["tags"],
};
const expectedParameters: Parameter[] = [
{ name: "tags", type: "string[]", description: "User tags" },
];
const result = jsonSchemaToActionParameters(jsonSchema);
expect(result).toEqual(expectedParameters);
});
it("should convert object array JSONSchema to Parameter array", () => {
const jsonSchema: JSONSchema = {
type: "object",
properties: {
addresses: {
type: "array",
items: {
type: "object",
properties: {
street: { type: "string", description: "Street name" },
city: { type: "string", description: "City name" },
},
required: ["street"],
},
description: "User addresses",
},
},
required: ["addresses"],
};
const expectedParameters: Parameter[] = [
{
name: "addresses",
type: "object[]",
description: "User addresses",
attributes: [
{ name: "street", type: "string", description: "Street name" },
{
name: "city",
type: "string",
description: "City name",
required: false,
},
],
},
];
const result = jsonSchemaToActionParameters(jsonSchema);
expect(result).toEqual(expectedParameters);
});
it("should handle boolean types", () => {
const jsonSchema: JSONSchema = {
type: "object",
properties: {
isAdmin: { type: "boolean", description: "Is user an admin" },
},
required: ["isAdmin"],
};
const expectedParameters: Parameter[] = [
{ name: "isAdmin", type: "boolean", description: "Is user an admin" },
];
const result = jsonSchemaToActionParameters(jsonSchema);
expect(result).toEqual(expectedParameters);
});
it("should handle empty object schema", () => {
const jsonSchema: JSONSchema = {
type: "object",
};
const expectedParameters: Parameter[] = [];
const result = jsonSchemaToActionParameters(jsonSchema);
expect(result).toEqual(expectedParameters);
});
it("should throw error for nested arrays", () => {
const jsonSchema: JSONSchema = {
type: "object",
properties: {
nestedArray: {
type: "array",
items: {
type: "array",
items: { type: "string" },
},
description: "Matrix of strings",
},
},
required: ["nestedArray"],
};
expect(() => jsonSchemaToActionParameters(jsonSchema)).toThrow(
"Nested arrays are not supported",
);
});
it("should handle null-union type ['string', 'null'] as optional string", () => {
const jsonSchema = {
type: "object",
properties: {
nickname: {
type: ["string", "null"],
description: "Optional nickname",
},
},
required: ["nickname"],
};
const result = jsonSchemaToActionParameters(jsonSchema as any);
expect(result).toEqual([
{
name: "nickname",
type: "string",
description: "Optional nickname",
required: false,
},
]);
});
it("should handle null-union type ['object', 'null'] preserving properties", () => {
const jsonSchema = {
type: "object",
properties: {
metadata: {
type: ["object", "null"],
description: "Optional metadata",
properties: {
key: { type: "string" },
},
required: ["key"],
},
},
required: ["metadata"],
};
const result = jsonSchemaToActionParameters(jsonSchema as any);
expect(result).toEqual([
{
name: "metadata",
type: "object",
description: "Optional metadata",
required: false,
attributes: [{ name: "key", type: "string", description: undefined }],
},
]);
});
it("should handle null-union type when field is already optional", () => {
const jsonSchema = {
type: "object",
properties: {
nickname: {
type: ["string", "null"],
description: "Optional nickname",
},
},
// nickname is NOT in required
};
const result = jsonSchemaToActionParameters(jsonSchema as any);
expect(result).toEqual([
{
name: "nickname",
type: "string",
description: "Optional nickname",
required: false,
},
]);
});
it("should handle type array with only 'null' by falling back to string", () => {
const jsonSchema = {
type: "object",
properties: {
weird: {
type: ["null"],
description: "Null-only type",
},
},
required: ["weird"],
};
const result = jsonSchemaToActionParameters(jsonSchema as any);
expect(result).toEqual([
{
name: "weird",
type: "string",
description: "Null-only type",
required: false,
},
]);
});
it("should ensure round-trip conversion works", () => {
const originalParameters: Parameter[] = [
{ name: "name", type: "string", description: "User name" },
{ name: "age", type: "number", description: "User age", required: false },
{
name: "role",
type: "string",
enum: ["admin", "user"],
description: "User role",
},
{
name: "address",
type: "object",
description: "User address",
attributes: [
{ name: "street", type: "string", description: "Street name" },
{ name: "city", type: "string", description: "City name" },
],
},
{
name: "contacts",
type: "object[]",
description: "User contacts",
attributes: [
{ name: "type", type: "string", description: "Contact type" },
{ name: "value", type: "string", description: "Contact value" },
],
},
];
const jsonSchema = actionParametersToJsonSchema(originalParameters);
const roundTripParameters = jsonSchemaToActionParameters(jsonSchema);
expect(roundTripParameters).toEqual(originalParameters);
});
});