@copilotkit/shared 1.4.1-pre.2 → 1.4.1-pre.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -3
- package/dist/{chunk-JP2M4U4G.mjs → chunk-CIPF7PMC.mjs} +2 -34
- package/dist/chunk-CIPF7PMC.mjs.map +1 -0
- package/dist/{chunk-3AJ2GKWG.mjs → chunk-GYZIHHE6.mjs} +2 -2
- package/dist/chunk-GYZIHHE6.mjs.map +1 -0
- package/dist/constants/index.d.ts +2 -2
- package/dist/constants/index.js +1 -1
- package/dist/constants/index.js.map +1 -1
- package/dist/constants/index.mjs +1 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.js +2 -35
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +4 -6
- package/dist/index.mjs.map +1 -1
- package/dist/utils/index.d.ts +1 -2
- package/dist/utils/index.js +0 -33
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +2 -4
- package/dist/utils/json-schema.d.ts +1 -3
- package/dist/utils/json-schema.js +2 -35
- package/dist/utils/json-schema.js.map +1 -1
- package/dist/utils/json-schema.mjs +3 -5
- package/package.json +4 -6
- package/src/constants/index.ts +1 -1
- package/src/utils/json-schema.ts +0 -34
- package/dist/chunk-3AJ2GKWG.mjs.map +0 -1
- package/dist/chunk-JP2M4U4G.mjs.map +0 -1
- package/dist/utils/json-schema.test.d.ts +0 -2
- package/dist/utils/json-schema.test.js +0 -173
- package/dist/utils/json-schema.test.js.map +0 -1
- package/dist/utils/json-schema.test.mjs +0 -140
- package/dist/utils/json-schema.test.mjs.map +0 -1
- package/src/utils/json-schema.test.ts +0 -169
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"publishConfig": {
|
|
10
10
|
"access": "public"
|
|
11
11
|
},
|
|
12
|
-
"version": "1.4.1-pre.
|
|
12
|
+
"version": "1.4.1-pre.5",
|
|
13
13
|
"sideEffects": false,
|
|
14
14
|
"main": "./dist/index.js",
|
|
15
15
|
"module": "./dist/index.mjs",
|
|
@@ -30,15 +30,13 @@
|
|
|
30
30
|
"ts-jest": "^29.1.1",
|
|
31
31
|
"tsup": "^6.7.0",
|
|
32
32
|
"typescript": "^5.2.3",
|
|
33
|
-
"eslint-config-custom": "1.4.1-pre.
|
|
34
|
-
"tsconfig": "1.4.1-pre.
|
|
33
|
+
"eslint-config-custom": "1.4.1-pre.5",
|
|
34
|
+
"tsconfig": "1.4.1-pre.5"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@segment/analytics-node": "^2.1.2",
|
|
38
38
|
"chalk": "4.1.2",
|
|
39
|
-
"uuid": "^10.0.0"
|
|
40
|
-
"zod": "^3.23.3",
|
|
41
|
-
"zod-to-json-schema": "^3.23.5"
|
|
39
|
+
"uuid": "^10.0.0"
|
|
42
40
|
},
|
|
43
41
|
"keywords": [
|
|
44
42
|
"copilotkit",
|
package/src/constants/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export const COPILOT_CLOUD_API_URL = "https://api.copilotkit.ai";
|
|
1
|
+
export const COPILOT_CLOUD_API_URL = "https://api.cloud.copilotkit.ai";
|
|
2
2
|
export const COPILOT_CLOUD_VERSION = "v1";
|
|
3
3
|
export const COPILOT_CLOUD_CHAT_URL = `${COPILOT_CLOUD_API_URL}/copilotkit/${COPILOT_CLOUD_VERSION}`;
|
|
4
4
|
export const COPILOT_CLOUD_PUBLIC_API_KEY_HEADER = "X-CopilotCloud-Public-Api-Key";
|
package/src/utils/json-schema.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
1
|
import { Parameter } from "../types";
|
|
3
2
|
|
|
4
3
|
export type JSONSchemaString = {
|
|
@@ -119,36 +118,3 @@ function convertAttribute(attribute: Parameter): JSONSchema {
|
|
|
119
118
|
};
|
|
120
119
|
}
|
|
121
120
|
}
|
|
122
|
-
|
|
123
|
-
export function convertJsonSchemaToZodSchema(jsonSchema: any, required: boolean): z.ZodSchema {
|
|
124
|
-
if (jsonSchema.type === "object") {
|
|
125
|
-
const spec: { [key: string]: z.ZodSchema } = {};
|
|
126
|
-
|
|
127
|
-
if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {
|
|
128
|
-
return !required ? z.object(spec).optional() : z.object(spec);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
for (const [key, value] of Object.entries(jsonSchema.properties)) {
|
|
132
|
-
spec[key] = convertJsonSchemaToZodSchema(
|
|
133
|
-
value,
|
|
134
|
-
jsonSchema.required ? jsonSchema.required.includes(key) : false,
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
let schema = z.object(spec).describe(jsonSchema.description);
|
|
138
|
-
return required ? schema : schema.optional();
|
|
139
|
-
} else if (jsonSchema.type === "string") {
|
|
140
|
-
let schema = z.string().describe(jsonSchema.description);
|
|
141
|
-
return required ? schema : schema.optional();
|
|
142
|
-
} else if (jsonSchema.type === "number") {
|
|
143
|
-
let schema = z.number().describe(jsonSchema.description);
|
|
144
|
-
return required ? schema : schema.optional();
|
|
145
|
-
} else if (jsonSchema.type === "boolean") {
|
|
146
|
-
let schema = z.boolean().describe(jsonSchema.description);
|
|
147
|
-
return required ? schema : schema.optional();
|
|
148
|
-
} else if (jsonSchema.type === "array") {
|
|
149
|
-
let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);
|
|
150
|
-
let schema = z.array(itemSchema).describe(jsonSchema.description);
|
|
151
|
-
return required ? schema : schema.optional();
|
|
152
|
-
}
|
|
153
|
-
throw new Error("Invalid JSON schema");
|
|
154
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/constants/index.ts"],"sourcesContent":["export const COPILOT_CLOUD_API_URL = \"https://api.copilotkit.ai\";\nexport const COPILOT_CLOUD_VERSION = \"v1\";\nexport const COPILOT_CLOUD_CHAT_URL = `${COPILOT_CLOUD_API_URL}/copilotkit/${COPILOT_CLOUD_VERSION}`;\nexport const COPILOT_CLOUD_PUBLIC_API_KEY_HEADER = \"X-CopilotCloud-Public-Api-Key\";\n"],"mappings":";AAAO,IAAM,wBAAwB;AAC9B,IAAM,wBAAwB;AAC9B,IAAM,yBAAyB,GAAG,oCAAoC;AACtE,IAAM,sCAAsC;","names":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/utils/json-schema.ts"],"sourcesContent":["import { z } from \"zod\";\nimport { Parameter } from \"../types\";\n\nexport type JSONSchemaString = {\n type: \"string\";\n description?: string;\n enum?: string[];\n};\n\nexport type JSONSchemaNumber = {\n type: \"number\";\n description?: string;\n};\n\nexport type JSONSchemaBoolean = {\n type: \"boolean\";\n description?: string;\n};\n\nexport type JSONSchemaObject = {\n type: \"object\";\n properties?: Record<string, JSONSchema>;\n required?: string[];\n description?: string;\n};\n\nexport type JSONSchemaArray = {\n type: \"array\";\n items: JSONSchema;\n description?: string;\n};\n\nexport type JSONSchema =\n | JSONSchemaString\n | JSONSchemaNumber\n | JSONSchemaBoolean\n | JSONSchemaObject\n | JSONSchemaArray;\n\nexport function actionParametersToJsonSchema(actionParameters: Parameter[]): JSONSchema {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let parameter of actionParameters || []) {\n parameters[parameter.name] = convertAttribute(parameter);\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of actionParameters || []) {\n if (arg.required !== false) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n return {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n };\n}\n\nfunction convertAttribute(attribute: Parameter): JSONSchema {\n switch (attribute.type) {\n case \"string\":\n return {\n type: \"string\",\n description: attribute.description,\n ...(attribute.enum && { enum: attribute.enum }),\n };\n case \"number\":\n case \"boolean\":\n return {\n type: attribute.type,\n description: attribute.description,\n };\n case \"object\":\n case \"object[]\":\n const properties = attribute.attributes?.reduce(\n (acc, attr) => {\n acc[attr.name] = convertAttribute(attr);\n return acc;\n },\n {} as Record<string, any>,\n );\n const required = attribute.attributes\n ?.filter((attr) => attr.required !== false)\n .map((attr) => attr.name);\n if (attribute.type === \"object[]\") {\n return {\n type: \"array\",\n items: {\n type: \"object\",\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n },\n description: attribute.description,\n };\n }\n return {\n type: \"object\",\n description: attribute.description,\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n };\n default:\n // Handle arrays of primitive types and undefined attribute.type\n if (attribute.type?.endsWith(\"[]\")) {\n const itemType = attribute.type.slice(0, -2);\n return {\n type: \"array\",\n items: { type: itemType as any },\n description: attribute.description,\n };\n }\n // Fallback for undefined type or any other unexpected type\n return {\n type: \"string\",\n description: attribute.description,\n };\n }\n}\n\nexport function convertJsonSchemaToZodSchema(jsonSchema: any, required: boolean): z.ZodSchema {\n if (jsonSchema.type === \"object\") {\n const spec: { [key: string]: z.ZodSchema } = {};\n\n if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {\n return !required ? z.object(spec).optional() : z.object(spec);\n }\n\n for (const [key, value] of Object.entries(jsonSchema.properties)) {\n spec[key] = convertJsonSchemaToZodSchema(\n value,\n jsonSchema.required ? jsonSchema.required.includes(key) : false,\n );\n }\n let schema = z.object(spec).describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"string\") {\n let schema = z.string().describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"number\") {\n let schema = z.number().describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"boolean\") {\n let schema = z.boolean().describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"array\") {\n let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);\n let schema = z.array(itemSchema).describe(jsonSchema.description);\n return required ? schema : schema.optional();\n }\n throw new Error(\"Invalid JSON schema\");\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O,WAAW,SAAS,OAAO,SAAS;AAAA,EAC7C,WAAW,WAAW,SAAS,WAAW;AACxC,QAAI,SAAS,EAAE,QAAQ,EAAE,SAAS,WAAW,WAAW;AACxD,WAAO,WAAW,SAAS,OAAO,SAAS;AAAA,EAC7C,WAAW,WAAW,SAAS,SAAS;AACtC,QAAI,aAAa,6BAA6B,WAAW,OAAO,IAAI;AACpE,QAAI,SAAS,EAAE,MAAM,UAAU,EAAE,SAAS,WAAW,WAAW;AAChE,WAAO,WAAW,SAAS,OAAO,SAAS;AAAA,EAC7C;AACA,QAAM,IAAI,MAAM,qBAAqB;AACvC;","names":[]}
|
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
// src/utils/json-schema.test.ts
|
|
4
|
-
var import_zod2 = require("zod");
|
|
5
|
-
|
|
6
|
-
// src/utils/json-schema.ts
|
|
7
|
-
var import_zod = require("zod");
|
|
8
|
-
function convertJsonSchemaToZodSchema(jsonSchema, required) {
|
|
9
|
-
if (jsonSchema.type === "object") {
|
|
10
|
-
const spec = {};
|
|
11
|
-
if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {
|
|
12
|
-
return !required ? import_zod.z.object(spec).optional() : import_zod.z.object(spec);
|
|
13
|
-
}
|
|
14
|
-
for (const [key, value] of Object.entries(jsonSchema.properties)) {
|
|
15
|
-
spec[key] = convertJsonSchemaToZodSchema(
|
|
16
|
-
value,
|
|
17
|
-
jsonSchema.required ? jsonSchema.required.includes(key) : false
|
|
18
|
-
);
|
|
19
|
-
}
|
|
20
|
-
let schema = import_zod.z.object(spec).describe(jsonSchema.description);
|
|
21
|
-
return required ? schema : schema.optional();
|
|
22
|
-
} else if (jsonSchema.type === "string") {
|
|
23
|
-
let schema = import_zod.z.string().describe(jsonSchema.description);
|
|
24
|
-
return required ? schema : schema.optional();
|
|
25
|
-
} else if (jsonSchema.type === "number") {
|
|
26
|
-
let schema = import_zod.z.number().describe(jsonSchema.description);
|
|
27
|
-
return required ? schema : schema.optional();
|
|
28
|
-
} else if (jsonSchema.type === "boolean") {
|
|
29
|
-
let schema = import_zod.z.boolean().describe(jsonSchema.description);
|
|
30
|
-
return required ? schema : schema.optional();
|
|
31
|
-
} else if (jsonSchema.type === "array") {
|
|
32
|
-
let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);
|
|
33
|
-
let schema = import_zod.z.array(itemSchema).describe(jsonSchema.description);
|
|
34
|
-
return required ? schema : schema.optional();
|
|
35
|
-
}
|
|
36
|
-
throw new Error("Invalid JSON schema");
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// src/utils/json-schema.test.ts
|
|
40
|
-
var import_zod_to_json_schema = require("zod-to-json-schema");
|
|
41
|
-
describe("convertJsonSchemaToZodSchema", () => {
|
|
42
|
-
it("should convert a simple JSON schema to a Zod schema", () => {
|
|
43
|
-
const jsonSchema = {
|
|
44
|
-
type: "object",
|
|
45
|
-
properties: {
|
|
46
|
-
name: { type: "string" },
|
|
47
|
-
age: { type: "number" }
|
|
48
|
-
},
|
|
49
|
-
required: ["name", "age"]
|
|
50
|
-
};
|
|
51
|
-
const expectedSchema = import_zod2.z.object({
|
|
52
|
-
name: import_zod2.z.string(),
|
|
53
|
-
age: import_zod2.z.number()
|
|
54
|
-
});
|
|
55
|
-
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
56
|
-
const resultSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(result);
|
|
57
|
-
const expectedSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(expectedSchema);
|
|
58
|
-
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
59
|
-
});
|
|
60
|
-
it("should convert a JSON schema with nested objects to a Zod schema", () => {
|
|
61
|
-
const jsonSchema = {
|
|
62
|
-
type: "object",
|
|
63
|
-
properties: {
|
|
64
|
-
name: { type: "string" },
|
|
65
|
-
address: {
|
|
66
|
-
type: "object",
|
|
67
|
-
properties: {
|
|
68
|
-
street: { type: "string" },
|
|
69
|
-
city: { type: "string" }
|
|
70
|
-
},
|
|
71
|
-
required: ["street", "city"]
|
|
72
|
-
}
|
|
73
|
-
},
|
|
74
|
-
required: ["name", "address"]
|
|
75
|
-
};
|
|
76
|
-
const expectedSchema = import_zod2.z.object({
|
|
77
|
-
name: import_zod2.z.string(),
|
|
78
|
-
address: import_zod2.z.object({
|
|
79
|
-
street: import_zod2.z.string(),
|
|
80
|
-
city: import_zod2.z.string()
|
|
81
|
-
})
|
|
82
|
-
});
|
|
83
|
-
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
84
|
-
const resultSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(result);
|
|
85
|
-
const expectedSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(expectedSchema);
|
|
86
|
-
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
87
|
-
});
|
|
88
|
-
it("should convert a JSON schema with arrays to a Zod schema", () => {
|
|
89
|
-
const jsonSchema = {
|
|
90
|
-
type: "object",
|
|
91
|
-
properties: {
|
|
92
|
-
names: {
|
|
93
|
-
type: "array",
|
|
94
|
-
items: { type: "string" }
|
|
95
|
-
}
|
|
96
|
-
},
|
|
97
|
-
required: ["names"]
|
|
98
|
-
};
|
|
99
|
-
const expectedSchema = import_zod2.z.object({
|
|
100
|
-
names: import_zod2.z.array(import_zod2.z.string())
|
|
101
|
-
});
|
|
102
|
-
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
103
|
-
const resultSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(result);
|
|
104
|
-
const expectedSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(expectedSchema);
|
|
105
|
-
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
106
|
-
});
|
|
107
|
-
it("should convert a JSON schema with optional properties to a Zod schema", () => {
|
|
108
|
-
const jsonSchema = {
|
|
109
|
-
type: "object",
|
|
110
|
-
properties: {
|
|
111
|
-
name: { type: "string" },
|
|
112
|
-
age: { type: "number", required: false }
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
const expectedSchema = import_zod2.z.object({
|
|
116
|
-
name: import_zod2.z.string().optional(),
|
|
117
|
-
age: import_zod2.z.number().optional()
|
|
118
|
-
}).optional();
|
|
119
|
-
const result = convertJsonSchemaToZodSchema(jsonSchema, false);
|
|
120
|
-
console.log(convertJsonSchemaToZodSchema(jsonSchema, false));
|
|
121
|
-
const resultSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(result);
|
|
122
|
-
const expectedSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(expectedSchema);
|
|
123
|
-
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
124
|
-
});
|
|
125
|
-
it("should convert a JSON schema with different types to a Zod schema", () => {
|
|
126
|
-
const jsonSchema = {
|
|
127
|
-
type: "object",
|
|
128
|
-
properties: {
|
|
129
|
-
name: { type: "string" },
|
|
130
|
-
age: { type: "number" },
|
|
131
|
-
isAdmin: { type: "boolean" }
|
|
132
|
-
},
|
|
133
|
-
required: ["name", "age", "isAdmin"]
|
|
134
|
-
};
|
|
135
|
-
const expectedSchema = import_zod2.z.object({
|
|
136
|
-
name: import_zod2.z.string(),
|
|
137
|
-
age: import_zod2.z.number(),
|
|
138
|
-
isAdmin: import_zod2.z.boolean()
|
|
139
|
-
});
|
|
140
|
-
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
141
|
-
const resultSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(result);
|
|
142
|
-
const expectedSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(expectedSchema);
|
|
143
|
-
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
144
|
-
});
|
|
145
|
-
it("should handle edge case where JSON schema has no properties", () => {
|
|
146
|
-
const jsonSchema = {
|
|
147
|
-
type: "object"
|
|
148
|
-
};
|
|
149
|
-
const expectedSchema = import_zod2.z.object({});
|
|
150
|
-
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
151
|
-
const resultSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(result);
|
|
152
|
-
const expectedSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(expectedSchema);
|
|
153
|
-
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
154
|
-
});
|
|
155
|
-
it("should handle edge case where JSON schema has no required properties", () => {
|
|
156
|
-
const jsonSchema = {
|
|
157
|
-
type: "object",
|
|
158
|
-
properties: {
|
|
159
|
-
name: { type: "string" },
|
|
160
|
-
age: { type: "number" }
|
|
161
|
-
}
|
|
162
|
-
};
|
|
163
|
-
const expectedSchema = import_zod2.z.object({
|
|
164
|
-
name: import_zod2.z.string().optional(),
|
|
165
|
-
age: import_zod2.z.number().optional()
|
|
166
|
-
}).optional();
|
|
167
|
-
const result = convertJsonSchemaToZodSchema(jsonSchema, false);
|
|
168
|
-
const resultSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(result);
|
|
169
|
-
const expectedSchemaJson = (0, import_zod_to_json_schema.zodToJsonSchema)(expectedSchema);
|
|
170
|
-
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
171
|
-
});
|
|
172
|
-
});
|
|
173
|
-
//# sourceMappingURL=json-schema.test.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/json-schema.test.ts","../../src/utils/json-schema.ts"],"sourcesContent":["import { z } from \"zod\";\nimport { convertJsonSchemaToZodSchema } from \"../utils/json-schema\";\nimport { zodToJsonSchema } from \"zod-to-json-schema\";\n\ndescribe(\"convertJsonSchemaToZodSchema\", () => {\n it(\"should convert a simple JSON schema to a Zod schema\", () => {\n const jsonSchema = {\n type: \"object\",\n properties: {\n name: { type: \"string\" },\n age: { type: \"number\" },\n },\n required: [\"name\", \"age\"],\n };\n\n const expectedSchema = z.object({\n name: z.string(),\n age: z.number(),\n });\n\n const result = convertJsonSchemaToZodSchema(jsonSchema, true);\n const resultSchemaJson = zodToJsonSchema(result);\n const expectedSchemaJson = zodToJsonSchema(expectedSchema);\n\n expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);\n });\n\n it(\"should convert a JSON schema with nested objects to a Zod schema\", () => {\n const jsonSchema = {\n type: \"object\",\n properties: {\n name: { type: \"string\" },\n address: {\n type: \"object\",\n properties: {\n street: { type: \"string\" },\n city: { type: \"string\" },\n },\n required: [\"street\", \"city\"],\n },\n },\n required: [\"name\", \"address\"],\n };\n\n const expectedSchema = z.object({\n name: z.string(),\n address: z.object({\n street: z.string(),\n city: z.string(),\n }),\n });\n\n const result = convertJsonSchemaToZodSchema(jsonSchema, true);\n const resultSchemaJson = zodToJsonSchema(result);\n const expectedSchemaJson = zodToJsonSchema(expectedSchema);\n\n expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);\n });\n\n it(\"should convert a JSON schema with arrays to a Zod schema\", () => {\n const jsonSchema = {\n type: \"object\",\n properties: {\n names: {\n type: \"array\",\n items: { type: \"string\" },\n },\n },\n required: [\"names\"],\n };\n\n const expectedSchema = z.object({\n names: z.array(z.string()),\n });\n\n const result = convertJsonSchemaToZodSchema(jsonSchema, true);\n const resultSchemaJson = zodToJsonSchema(result);\n const expectedSchemaJson = zodToJsonSchema(expectedSchema);\n\n expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);\n });\n\n it(\"should convert a JSON schema with optional properties to a Zod schema\", () => {\n const jsonSchema = {\n type: \"object\",\n properties: {\n name: { type: \"string\" },\n age: { type: \"number\", required: false },\n },\n };\n\n const expectedSchema = z\n .object({\n name: z.string().optional(),\n age: z.number().optional(),\n })\n .optional();\n\n const result = convertJsonSchemaToZodSchema(jsonSchema, false);\n\n console.log(convertJsonSchemaToZodSchema(jsonSchema, false));\n\n const resultSchemaJson = zodToJsonSchema(result);\n const expectedSchemaJson = zodToJsonSchema(expectedSchema);\n\n expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);\n });\n\n it(\"should convert a JSON schema with different types to a Zod schema\", () => {\n const jsonSchema = {\n type: \"object\",\n properties: {\n name: { type: \"string\" },\n age: { type: \"number\" },\n isAdmin: { type: \"boolean\" },\n },\n required: [\"name\", \"age\", \"isAdmin\"],\n };\n\n const expectedSchema = z.object({\n name: z.string(),\n age: z.number(),\n isAdmin: z.boolean(),\n });\n\n const result = convertJsonSchemaToZodSchema(jsonSchema, true);\n const resultSchemaJson = zodToJsonSchema(result);\n const expectedSchemaJson = zodToJsonSchema(expectedSchema);\n\n expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);\n });\n\n it(\"should handle edge case where JSON schema has no properties\", () => {\n const jsonSchema = {\n type: \"object\",\n };\n\n const expectedSchema = z.object({});\n\n const result = convertJsonSchemaToZodSchema(jsonSchema, true);\n const resultSchemaJson = zodToJsonSchema(result);\n const expectedSchemaJson = zodToJsonSchema(expectedSchema);\n\n expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);\n });\n\n it(\"should handle edge case where JSON schema has no required properties\", () => {\n const jsonSchema = {\n type: \"object\",\n properties: {\n name: { type: \"string\" },\n age: { type: \"number\" },\n },\n };\n\n const expectedSchema = z\n .object({\n name: z.string().optional(),\n age: z.number().optional(),\n })\n .optional();\n\n const result = convertJsonSchemaToZodSchema(jsonSchema, false);\n const resultSchemaJson = zodToJsonSchema(result);\n const expectedSchemaJson = zodToJsonSchema(expectedSchema);\n\n expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);\n });\n});\n","import { z } from \"zod\";\nimport { Parameter } from \"../types\";\n\nexport type JSONSchemaString = {\n type: \"string\";\n description?: string;\n enum?: string[];\n};\n\nexport type JSONSchemaNumber = {\n type: \"number\";\n description?: string;\n};\n\nexport type JSONSchemaBoolean = {\n type: \"boolean\";\n description?: string;\n};\n\nexport type JSONSchemaObject = {\n type: \"object\";\n properties?: Record<string, JSONSchema>;\n required?: string[];\n description?: string;\n};\n\nexport type JSONSchemaArray = {\n type: \"array\";\n items: JSONSchema;\n description?: string;\n};\n\nexport type JSONSchema =\n | JSONSchemaString\n | JSONSchemaNumber\n | JSONSchemaBoolean\n | JSONSchemaObject\n | JSONSchemaArray;\n\nexport function actionParametersToJsonSchema(actionParameters: Parameter[]): JSONSchema {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let parameter of actionParameters || []) {\n parameters[parameter.name] = convertAttribute(parameter);\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of actionParameters || []) {\n if (arg.required !== false) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n return {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n };\n}\n\nfunction convertAttribute(attribute: Parameter): JSONSchema {\n switch (attribute.type) {\n case \"string\":\n return {\n type: \"string\",\n description: attribute.description,\n ...(attribute.enum && { enum: attribute.enum }),\n };\n case \"number\":\n case \"boolean\":\n return {\n type: attribute.type,\n description: attribute.description,\n };\n case \"object\":\n case \"object[]\":\n const properties = attribute.attributes?.reduce(\n (acc, attr) => {\n acc[attr.name] = convertAttribute(attr);\n return acc;\n },\n {} as Record<string, any>,\n );\n const required = attribute.attributes\n ?.filter((attr) => attr.required !== false)\n .map((attr) => attr.name);\n if (attribute.type === \"object[]\") {\n return {\n type: \"array\",\n items: {\n type: \"object\",\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n },\n description: attribute.description,\n };\n }\n return {\n type: \"object\",\n description: attribute.description,\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n };\n default:\n // Handle arrays of primitive types and undefined attribute.type\n if (attribute.type?.endsWith(\"[]\")) {\n const itemType = attribute.type.slice(0, -2);\n return {\n type: \"array\",\n items: { type: itemType as any },\n description: attribute.description,\n };\n }\n // Fallback for undefined type or any other unexpected type\n return {\n type: \"string\",\n description: attribute.description,\n };\n }\n}\n\nexport function convertJsonSchemaToZodSchema(jsonSchema: any, required: boolean): z.ZodSchema {\n if (jsonSchema.type === \"object\") {\n const spec: { [key: string]: z.ZodSchema } = {};\n\n if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {\n return !required ? z.object(spec).optional() : z.object(spec);\n }\n\n for (const [key, value] of Object.entries(jsonSchema.properties)) {\n spec[key] = convertJsonSchemaToZodSchema(\n value,\n jsonSchema.required ? jsonSchema.required.includes(key) : false,\n );\n }\n let schema = z.object(spec).describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"string\") {\n let schema = z.string().describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"number\") {\n let schema = z.number().describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"boolean\") {\n let schema = z.boolean().describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"array\") {\n let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);\n let schema = z.array(itemSchema).describe(jsonSchema.description);\n return required ? schema : schema.optional();\n }\n throw new Error(\"Invalid JSON schema\");\n}\n"],"mappings":";;;AAAA,IAAAA,cAAkB;;;ACAlB,iBAAkB;AA0HX,SAAS,6BAA6B,YAAiB,UAAgC;AAC5F,MAAI,WAAW,SAAS,UAAU;AAChC,UAAM,OAAuC,CAAC;AAE9C,QAAI,CAAC,WAAW,cAAc,CAAC,OAAO,KAAK,WAAW,UAAU,EAAE,QAAQ;AACxE,aAAO,CAAC,WAAW,aAAE,OAAO,IAAI,EAAE,SAAS,IAAI,aAAE,OAAO,IAAI;AAAA,IAC9D;AAEA,eAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,WAAW,UAAU,GAAG;AAChE,WAAK,GAAG,IAAI;AAAA,QACV;AAAA,QACA,WAAW,WAAW,WAAW,SAAS,SAAS,GAAG,IAAI;AAAA,MAC5D;AAAA,IACF;AACA,QAAI,SAAS,aAAE,OAAO,IAAI,EAAE,SAAS,WAAW,WAAW;AAC3D,WAAO,WAAW,SAAS,OAAO,SAAS;AAAA,EAC7C,WAAW,WAA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B,EAAE,cAAc,kBAAkB;AAAA,EAC3D,CAAC;AAED,KAAG,wEAAwE,MAAM;AAC/E,UAAM,aAAa;AAAA,MACjB,MAAM;AAAA,MACN,YAAY;AAAA,QACV,MAAM,EAAE,MAAM,SAAS;AAAA,QACvB,KAAK,EAAE,MAAM,SAAS;AAAA,MACxB;AAAA,IACF;AAEA,UAAM,iBAAiB,cACpB,OAAO;AAAA,MACN,MAAM,cAAE,OAAO,EAAE,SAAS;AAAA,MAC1B,KAAK,cAAE,OAAO,EAAE,SAAS;AAAA,IAC3B,CAAC,EACA,SAAS;AAEZ,UAAM,SAAS,6BAA6B,YAAY,KAAK;AAC7D,UAAM,uBAAmB,2CAAgB,MAAM;AAC/C,UAAM,yBAAqB,2CAAgB,cAAc;AAEzD,WAAO,gBAAgB,EAAE,cAAc,kBAAkB;AAAA,EAC3D,CAAC;AACH,CAAC;","names":["import_zod"]}
|
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
convertJsonSchemaToZodSchema
|
|
3
|
-
} from "../chunk-JP2M4U4G.mjs";
|
|
4
|
-
|
|
5
|
-
// src/utils/json-schema.test.ts
|
|
6
|
-
import { z } from "zod";
|
|
7
|
-
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
8
|
-
describe("convertJsonSchemaToZodSchema", () => {
|
|
9
|
-
it("should convert a simple JSON schema to a Zod schema", () => {
|
|
10
|
-
const jsonSchema = {
|
|
11
|
-
type: "object",
|
|
12
|
-
properties: {
|
|
13
|
-
name: { type: "string" },
|
|
14
|
-
age: { type: "number" }
|
|
15
|
-
},
|
|
16
|
-
required: ["name", "age"]
|
|
17
|
-
};
|
|
18
|
-
const expectedSchema = z.object({
|
|
19
|
-
name: z.string(),
|
|
20
|
-
age: z.number()
|
|
21
|
-
});
|
|
22
|
-
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
23
|
-
const resultSchemaJson = zodToJsonSchema(result);
|
|
24
|
-
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
25
|
-
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
26
|
-
});
|
|
27
|
-
it("should convert a JSON schema with nested objects to a Zod schema", () => {
|
|
28
|
-
const jsonSchema = {
|
|
29
|
-
type: "object",
|
|
30
|
-
properties: {
|
|
31
|
-
name: { type: "string" },
|
|
32
|
-
address: {
|
|
33
|
-
type: "object",
|
|
34
|
-
properties: {
|
|
35
|
-
street: { type: "string" },
|
|
36
|
-
city: { type: "string" }
|
|
37
|
-
},
|
|
38
|
-
required: ["street", "city"]
|
|
39
|
-
}
|
|
40
|
-
},
|
|
41
|
-
required: ["name", "address"]
|
|
42
|
-
};
|
|
43
|
-
const expectedSchema = z.object({
|
|
44
|
-
name: z.string(),
|
|
45
|
-
address: z.object({
|
|
46
|
-
street: z.string(),
|
|
47
|
-
city: z.string()
|
|
48
|
-
})
|
|
49
|
-
});
|
|
50
|
-
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
51
|
-
const resultSchemaJson = zodToJsonSchema(result);
|
|
52
|
-
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
53
|
-
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
54
|
-
});
|
|
55
|
-
it("should convert a JSON schema with arrays to a Zod schema", () => {
|
|
56
|
-
const jsonSchema = {
|
|
57
|
-
type: "object",
|
|
58
|
-
properties: {
|
|
59
|
-
names: {
|
|
60
|
-
type: "array",
|
|
61
|
-
items: { type: "string" }
|
|
62
|
-
}
|
|
63
|
-
},
|
|
64
|
-
required: ["names"]
|
|
65
|
-
};
|
|
66
|
-
const expectedSchema = z.object({
|
|
67
|
-
names: z.array(z.string())
|
|
68
|
-
});
|
|
69
|
-
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
70
|
-
const resultSchemaJson = zodToJsonSchema(result);
|
|
71
|
-
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
72
|
-
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
73
|
-
});
|
|
74
|
-
it("should convert a JSON schema with optional properties to a Zod schema", () => {
|
|
75
|
-
const jsonSchema = {
|
|
76
|
-
type: "object",
|
|
77
|
-
properties: {
|
|
78
|
-
name: { type: "string" },
|
|
79
|
-
age: { type: "number", required: false }
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
const expectedSchema = z.object({
|
|
83
|
-
name: z.string().optional(),
|
|
84
|
-
age: z.number().optional()
|
|
85
|
-
}).optional();
|
|
86
|
-
const result = convertJsonSchemaToZodSchema(jsonSchema, false);
|
|
87
|
-
console.log(convertJsonSchemaToZodSchema(jsonSchema, false));
|
|
88
|
-
const resultSchemaJson = zodToJsonSchema(result);
|
|
89
|
-
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
90
|
-
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
91
|
-
});
|
|
92
|
-
it("should convert a JSON schema with different types to a Zod schema", () => {
|
|
93
|
-
const jsonSchema = {
|
|
94
|
-
type: "object",
|
|
95
|
-
properties: {
|
|
96
|
-
name: { type: "string" },
|
|
97
|
-
age: { type: "number" },
|
|
98
|
-
isAdmin: { type: "boolean" }
|
|
99
|
-
},
|
|
100
|
-
required: ["name", "age", "isAdmin"]
|
|
101
|
-
};
|
|
102
|
-
const expectedSchema = z.object({
|
|
103
|
-
name: z.string(),
|
|
104
|
-
age: z.number(),
|
|
105
|
-
isAdmin: z.boolean()
|
|
106
|
-
});
|
|
107
|
-
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
108
|
-
const resultSchemaJson = zodToJsonSchema(result);
|
|
109
|
-
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
110
|
-
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
111
|
-
});
|
|
112
|
-
it("should handle edge case where JSON schema has no properties", () => {
|
|
113
|
-
const jsonSchema = {
|
|
114
|
-
type: "object"
|
|
115
|
-
};
|
|
116
|
-
const expectedSchema = z.object({});
|
|
117
|
-
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
118
|
-
const resultSchemaJson = zodToJsonSchema(result);
|
|
119
|
-
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
120
|
-
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
121
|
-
});
|
|
122
|
-
it("should handle edge case where JSON schema has no required properties", () => {
|
|
123
|
-
const jsonSchema = {
|
|
124
|
-
type: "object",
|
|
125
|
-
properties: {
|
|
126
|
-
name: { type: "string" },
|
|
127
|
-
age: { type: "number" }
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
const expectedSchema = z.object({
|
|
131
|
-
name: z.string().optional(),
|
|
132
|
-
age: z.number().optional()
|
|
133
|
-
}).optional();
|
|
134
|
-
const result = convertJsonSchemaToZodSchema(jsonSchema, false);
|
|
135
|
-
const resultSchemaJson = zodToJsonSchema(result);
|
|
136
|
-
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
137
|
-
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
138
|
-
});
|
|
139
|
-
});
|
|
140
|
-
//# sourceMappingURL=json-schema.test.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/json-schema.test.ts"],"sourcesContent":["import { z } from \"zod\";\nimport { convertJsonSchemaToZodSchema } from \"../utils/json-schema\";\nimport { zodToJsonSchema } from \"zod-to-json-schema\";\n\ndescribe(\"convertJsonSchemaToZodSchema\", () => {\n it(\"should convert a simple JSON schema to a Zod schema\", () => {\n const jsonSchema = {\n type: \"object\",\n properties: {\n name: { type: \"string\" },\n age: { type: \"number\" },\n },\n required: [\"name\", \"age\"],\n };\n\n const expectedSchema = z.object({\n name: z.string(),\n age: z.number(),\n });\n\n const result = convertJsonSchemaToZodSchema(jsonSchema, true);\n const resultSchemaJson = zodToJsonSchema(result);\n const expectedSchemaJson = zodToJsonSchema(expectedSchema);\n\n expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);\n });\n\n it(\"should convert a JSON schema with nested objects to a Zod schema\", () => {\n const jsonSchema = {\n type: \"object\",\n properties: {\n name: { type: \"string\" },\n address: {\n type: \"object\",\n properties: {\n street: { type: \"string\" },\n city: { type: \"string\" },\n },\n required: [\"street\", \"city\"],\n },\n },\n required: [\"name\", \"address\"],\n };\n\n const expectedSchema = z.object({\n name: z.string(),\n address: z.object({\n street: z.string(),\n city: z.string(),\n }),\n });\n\n const result = convertJsonSchemaToZodSchema(jsonSchema, true);\n const resultSchemaJson = zodToJsonSchema(result);\n const expectedSchemaJson = zodToJsonSchema(expectedSchema);\n\n expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);\n });\n\n it(\"should convert a JSON schema with arrays to a Zod schema\", () => {\n const jsonSchema = {\n type: \"object\",\n properties: {\n names: {\n type: \"array\",\n items: { type: \"string\" },\n },\n },\n required: [\"names\"],\n };\n\n const expectedSchema = z.object({\n names: z.array(z.string()),\n });\n\n const result = convertJsonSchemaToZodSchema(jsonSchema, true);\n const resultSchemaJson = zodToJsonSchema(result);\n const expectedSchemaJson = zodToJsonSchema(expectedSchema);\n\n expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);\n });\n\n it(\"should convert a JSON schema with optional properties to a Zod schema\", () => {\n const jsonSchema = {\n type: \"object\",\n properties: {\n name: { type: \"string\" },\n age: { type: \"number\", required: false },\n },\n };\n\n const expectedSchema = z\n .object({\n name: z.string().optional(),\n age: z.number().optional(),\n })\n .optional();\n\n const result = convertJsonSchemaToZodSchema(jsonSchema, false);\n\n console.log(convertJsonSchemaToZodSchema(jsonSchema, false));\n\n const resultSchemaJson = zodToJsonSchema(result);\n const expectedSchemaJson = zodToJsonSchema(expectedSchema);\n\n expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);\n });\n\n it(\"should convert a JSON schema with different types to a Zod schema\", () => {\n const jsonSchema = {\n type: \"object\",\n properties: {\n name: { type: \"string\" },\n age: { type: \"number\" },\n isAdmin: { type: \"boolean\" },\n },\n required: [\"name\", \"age\", \"isAdmin\"],\n };\n\n const expectedSchema = z.object({\n name: z.string(),\n age: z.number(),\n isAdmin: z.boolean(),\n });\n\n const result = convertJsonSchemaToZodSchema(jsonSchema, true);\n const resultSchemaJson = zodToJsonSchema(result);\n const expectedSchemaJson = zodToJsonSchema(expectedSchema);\n\n expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);\n });\n\n it(\"should handle edge case where JSON schema has no properties\", () => {\n const jsonSchema = {\n type: \"object\",\n };\n\n const expectedSchema = z.object({});\n\n const result = convertJsonSchemaToZodSchema(jsonSchema, true);\n const resultSchemaJson = zodToJsonSchema(result);\n const expectedSchemaJson = zodToJsonSchema(expectedSchema);\n\n expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);\n });\n\n it(\"should handle edge case where JSON schema has no required properties\", () => {\n const jsonSchema = {\n type: \"object\",\n properties: {\n name: { type: \"string\" },\n age: { type: \"number\" },\n },\n };\n\n const expectedSchema = z\n .object({\n name: z.string().optional(),\n age: z.number().optional(),\n })\n .optional();\n\n const result = convertJsonSchemaToZodSchema(jsonSchema, false);\n const resultSchemaJson = zodToJsonSchema(result);\n const expectedSchemaJson = zodToJsonSchema(expectedSchema);\n\n expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);\n });\n});\n"],"mappings":";;;;;AAAA,SAAS,SAAS;AAElB,SAAS,uBAAuB;AAEhC,SAAS,gCAAgC,MAAM;AAC7C,KAAG,uDAAuD,MAAM;AAC9D,UAAM,aAAa;AAAA,MACjB,MAAM;AAAA,MACN,YAAY;AAAA,QACV,MAAM,EAAE,MAAM,SAAS;AAAA,QACvB,KAAK,EAAE,MAAM,SAAS;AAAA,MACxB;AAAA,MACA,UAAU,CAAC,QAAQ,KAAK;AAAA,IAC1B;AAEA,UAAM,iBAAiB,EAAE,OAAO;AAAA,MAC9B,MAAM,EAAE,OAAO;AAAA,MACf,KAAK,EAAE,OAAO;AAAA,IAChB,CAAC;AAED,UAAM,SAAS,6BAA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AC;AAED,KAAG,+DAA+D,MAAM;AACtE,UAAM,aAAa;AAAA,MACjB,MAAM;AAAA,IACR;AAEA,UAAM,iBAAiB,EAAE,OAAO,CAAC,CAAC;AAElC,UAAM,SAAS,6BAA6B,YAAY,IAAI;AAC5D,UAAM,mBAAmB,gBAAgB,MAAM;AAC/C,UAAM,qBAAqB,gBAAgB,cAAc;AAEzD,WAAO,gBAAgB,EAAE,cAAc,kBAAkB;AAAA,EAC3D,CAAC;AAED,KAAG,wEAAwE,MAAM;AAC/E,UAAM,aAAa;AAAA,MACjB,MAAM;AAAA,MACN,YAAY;AAAA,QACV,MAAM,EAAE,MAAM,SAAS;AAAA,QACvB,KAAK,EAAE,MAAM,SAAS;AAAA,MACxB;AAAA,IACF;AAEA,UAAM,iBAAiB,EACpB,OAAO;AAAA,MACN,MAAM,EAAE,OAAO,EAAE,SAAS;AAAA,MAC1B,KAAK,EAAE,OAAO,EAAE,SAAS;AAAA,IAC3B,CAAC,EACA,SAAS;AAEZ,UAAM,SAAS,6BAA6B,YAAY,KAAK;AAC7D,UAAM,mBAAmB,gBAAgB,MAAM;AAC/C,UAAM,qBAAqB,gBAAgB,cAAc;AAEzD,WAAO,gBAAgB,EAAE,cAAc,kBAAkB;AAAA,EAC3D,CAAC;AACH,CAAC;","names":[]}
|
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
import { convertJsonSchemaToZodSchema } from "../utils/json-schema";
|
|
3
|
-
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
4
|
-
|
|
5
|
-
describe("convertJsonSchemaToZodSchema", () => {
|
|
6
|
-
it("should convert a simple JSON schema to a Zod schema", () => {
|
|
7
|
-
const jsonSchema = {
|
|
8
|
-
type: "object",
|
|
9
|
-
properties: {
|
|
10
|
-
name: { type: "string" },
|
|
11
|
-
age: { type: "number" },
|
|
12
|
-
},
|
|
13
|
-
required: ["name", "age"],
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
const expectedSchema = z.object({
|
|
17
|
-
name: z.string(),
|
|
18
|
-
age: z.number(),
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
22
|
-
const resultSchemaJson = zodToJsonSchema(result);
|
|
23
|
-
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
24
|
-
|
|
25
|
-
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it("should convert a JSON schema with nested objects to a Zod schema", () => {
|
|
29
|
-
const jsonSchema = {
|
|
30
|
-
type: "object",
|
|
31
|
-
properties: {
|
|
32
|
-
name: { type: "string" },
|
|
33
|
-
address: {
|
|
34
|
-
type: "object",
|
|
35
|
-
properties: {
|
|
36
|
-
street: { type: "string" },
|
|
37
|
-
city: { type: "string" },
|
|
38
|
-
},
|
|
39
|
-
required: ["street", "city"],
|
|
40
|
-
},
|
|
41
|
-
},
|
|
42
|
-
required: ["name", "address"],
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
const expectedSchema = z.object({
|
|
46
|
-
name: z.string(),
|
|
47
|
-
address: z.object({
|
|
48
|
-
street: z.string(),
|
|
49
|
-
city: z.string(),
|
|
50
|
-
}),
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
54
|
-
const resultSchemaJson = zodToJsonSchema(result);
|
|
55
|
-
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
56
|
-
|
|
57
|
-
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it("should convert a JSON schema with arrays to a Zod schema", () => {
|
|
61
|
-
const jsonSchema = {
|
|
62
|
-
type: "object",
|
|
63
|
-
properties: {
|
|
64
|
-
names: {
|
|
65
|
-
type: "array",
|
|
66
|
-
items: { type: "string" },
|
|
67
|
-
},
|
|
68
|
-
},
|
|
69
|
-
required: ["names"],
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
const expectedSchema = z.object({
|
|
73
|
-
names: z.array(z.string()),
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
77
|
-
const resultSchemaJson = zodToJsonSchema(result);
|
|
78
|
-
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
79
|
-
|
|
80
|
-
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
it("should convert a JSON schema with optional properties to a Zod schema", () => {
|
|
84
|
-
const jsonSchema = {
|
|
85
|
-
type: "object",
|
|
86
|
-
properties: {
|
|
87
|
-
name: { type: "string" },
|
|
88
|
-
age: { type: "number", required: false },
|
|
89
|
-
},
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
const expectedSchema = z
|
|
93
|
-
.object({
|
|
94
|
-
name: z.string().optional(),
|
|
95
|
-
age: z.number().optional(),
|
|
96
|
-
})
|
|
97
|
-
.optional();
|
|
98
|
-
|
|
99
|
-
const result = convertJsonSchemaToZodSchema(jsonSchema, false);
|
|
100
|
-
|
|
101
|
-
console.log(convertJsonSchemaToZodSchema(jsonSchema, false));
|
|
102
|
-
|
|
103
|
-
const resultSchemaJson = zodToJsonSchema(result);
|
|
104
|
-
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
105
|
-
|
|
106
|
-
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it("should convert a JSON schema with different types to a Zod schema", () => {
|
|
110
|
-
const jsonSchema = {
|
|
111
|
-
type: "object",
|
|
112
|
-
properties: {
|
|
113
|
-
name: { type: "string" },
|
|
114
|
-
age: { type: "number" },
|
|
115
|
-
isAdmin: { type: "boolean" },
|
|
116
|
-
},
|
|
117
|
-
required: ["name", "age", "isAdmin"],
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
const expectedSchema = z.object({
|
|
121
|
-
name: z.string(),
|
|
122
|
-
age: z.number(),
|
|
123
|
-
isAdmin: z.boolean(),
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
127
|
-
const resultSchemaJson = zodToJsonSchema(result);
|
|
128
|
-
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
129
|
-
|
|
130
|
-
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
it("should handle edge case where JSON schema has no properties", () => {
|
|
134
|
-
const jsonSchema = {
|
|
135
|
-
type: "object",
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
const expectedSchema = z.object({});
|
|
139
|
-
|
|
140
|
-
const result = convertJsonSchemaToZodSchema(jsonSchema, true);
|
|
141
|
-
const resultSchemaJson = zodToJsonSchema(result);
|
|
142
|
-
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
143
|
-
|
|
144
|
-
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
it("should handle edge case where JSON schema has no required properties", () => {
|
|
148
|
-
const jsonSchema = {
|
|
149
|
-
type: "object",
|
|
150
|
-
properties: {
|
|
151
|
-
name: { type: "string" },
|
|
152
|
-
age: { type: "number" },
|
|
153
|
-
},
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
const expectedSchema = z
|
|
157
|
-
.object({
|
|
158
|
-
name: z.string().optional(),
|
|
159
|
-
age: z.number().optional(),
|
|
160
|
-
})
|
|
161
|
-
.optional();
|
|
162
|
-
|
|
163
|
-
const result = convertJsonSchemaToZodSchema(jsonSchema, false);
|
|
164
|
-
const resultSchemaJson = zodToJsonSchema(result);
|
|
165
|
-
const expectedSchemaJson = zodToJsonSchema(expectedSchema);
|
|
166
|
-
|
|
167
|
-
expect(resultSchemaJson).toStrictEqual(expectedSchemaJson);
|
|
168
|
-
});
|
|
169
|
-
});
|