@copilotkit/shared
Version:
<div align="center"> <a href="https://copilotkit.ai" target="_blank"> <img src="https://github.com/copilotkit/copilotkit/raw/main/assets/banner.png" alt="CopilotKit Logo"> </a>
1 lines • 8.27 kB
Source Map (JSON)
{"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,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,oEAAoE,MAAM;AAC3E,UAAM,aAAa;AAAA,MACjB,MAAM;AAAA,MACN,YAAY;AAAA,QACV,MAAM,EAAE,MAAM,SAAS;AAAA,QACvB,SAAS;AAAA,UACP,MAAM;AAAA,UACN,YAAY;AAAA,YACV,QAAQ,EAAE,MAAM,SAAS;AAAA,YACzB,MAAM,EAAE,MAAM,SAAS;AAAA,UACzB;AAAA,UACA,UAAU,CAAC,UAAU,MAAM;AAAA,QAC7B;AAAA,MACF;AAAA,MACA,UAAU,CAAC,QAAQ,SAAS;AAAA,IAC9B;AAEA,UAAM,iBAAiB,EAAE,OAAO;AAAA,MAC9B,MAAM,EAAE,OAAO;AAAA,MACf,SAAS,EAAE,OAAO;AAAA,QAChB,QAAQ,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