@composio/json-schema-to-zod 0.1.2 → 0.1.4

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/dist/index.cjs CHANGED
@@ -102,8 +102,55 @@ function extendSchemaWithMessage(zodSchema, jsonSchema, key, extend) {
102
102
  return zodSchema;
103
103
  }
104
104
 
105
+ // src/utils/its.ts
106
+ var its = {
107
+ an: {
108
+ object: (x) => x.type === "object",
109
+ array: (x) => x.type === "array",
110
+ anyOf: (x) => x.anyOf !== void 0,
111
+ allOf: (x) => x.allOf !== void 0,
112
+ enum: (x) => x.enum !== void 0
113
+ },
114
+ a: {
115
+ nullable: (x) => x.nullable === true,
116
+ multipleType: (x) => Array.isArray(x.type),
117
+ not: (x) => x.not !== void 0,
118
+ const: (x) => x.const !== void 0,
119
+ primitive: (x, p) => x.type === p,
120
+ conditional: (x) => Boolean("if" in x && x.if && "then" in x && "else" in x && x.then && x.else),
121
+ oneOf: (x) => x.oneOf !== void 0
122
+ }
123
+ };
124
+
105
125
  // src/parsers/parse-array.ts
106
126
  var parseArray = (jsonSchema, refs) => {
127
+ if (its.an.anyOf(jsonSchema)) {
128
+ const types = /* @__PURE__ */ new Set();
129
+ let items;
130
+ jsonSchema.anyOf.forEach((option) => {
131
+ if (typeof option === "object" && option.type) {
132
+ types.add(typeof option.type === "string" ? option.type : option.type[0]);
133
+ }
134
+ if (typeof option === "object" && option.items) {
135
+ const optionItems = option.items;
136
+ if (!Array.isArray(optionItems) && typeof optionItems === "object") {
137
+ items = optionItems;
138
+ }
139
+ }
140
+ });
141
+ const newSchema = {
142
+ type: Array.from(types),
143
+ ...items && { items }
144
+ };
145
+ const metadataFields = ["default", "description", "examples", "title"];
146
+ metadataFields.forEach((field) => {
147
+ const value = jsonSchema[field];
148
+ if (value !== void 0) {
149
+ newSchema[field] = value;
150
+ }
151
+ });
152
+ return parseSchema(newSchema, refs);
153
+ }
107
154
  if (Array.isArray(jsonSchema.items)) {
108
155
  return import_zod3.z.tuple(
109
156
  jsonSchema.items.map(
@@ -343,26 +390,6 @@ var parseOneOf = (jsonSchema, refs) => {
343
390
  });
344
391
  };
345
392
 
346
- // src/utils/its.ts
347
- var its = {
348
- an: {
349
- object: (x) => x.type === "object",
350
- array: (x) => x.type === "array",
351
- anyOf: (x) => x.anyOf !== void 0,
352
- allOf: (x) => x.allOf !== void 0,
353
- enum: (x) => x.enum !== void 0
354
- },
355
- a: {
356
- nullable: (x) => x.nullable === true,
357
- multipleType: (x) => Array.isArray(x.type),
358
- not: (x) => x.not !== void 0,
359
- const: (x) => x.const !== void 0,
360
- primitive: (x, p) => x.type === p,
361
- conditional: (x) => Boolean("if" in x && x.if && "then" in x && "else" in x && x.then && x.else),
362
- oneOf: (x) => x.oneOf !== void 0
363
- }
364
- };
365
-
366
393
  // src/parsers/parse-object.ts
367
394
  function parseObjectProperties(objectSchema, refs) {
368
395
  if (!objectSchema.properties) {
package/dist/index.d.cts CHANGED
@@ -1,56 +1,11 @@
1
1
  import { ZodTypeAny, z } from 'zod';
2
+ import { JSONSchema7 } from 'json-schema';
2
3
 
3
4
  type Serializable = {
4
5
  [key: string]: Serializable;
5
6
  } | Serializable[] | string | number | boolean | null;
6
7
  type JsonSchema = JsonSchemaObject | boolean;
7
- type JsonSchemaObject = {
8
- type?: string | string[];
9
- properties?: {
10
- [key: string]: JsonSchema;
11
- };
12
- additionalProperties?: JsonSchema;
13
- unevaluatedProperties?: JsonSchema;
14
- patternProperties?: {
15
- [key: string]: JsonSchema;
16
- };
17
- minProperties?: number;
18
- maxProperties?: number;
19
- required?: string[] | boolean;
20
- propertyNames?: JsonSchema;
21
- items?: JsonSchema | JsonSchema[];
22
- additionalItems?: JsonSchema;
23
- minItems?: number;
24
- maxItems?: number;
25
- uniqueItems?: boolean;
26
- minLength?: number;
27
- maxLength?: number;
28
- pattern?: string;
29
- format?: string;
30
- minimum?: number;
31
- maximum?: number;
32
- exclusiveMinimum?: number | boolean;
33
- exclusiveMaximum?: number | boolean;
34
- multipleOf?: number;
35
- anyOf?: JsonSchema[];
36
- allOf?: JsonSchema[];
37
- oneOf?: JsonSchema[];
38
- if?: JsonSchema;
39
- then?: JsonSchema;
40
- else?: JsonSchema;
41
- const?: Serializable;
42
- enum?: Serializable[];
43
- errorMessage?: {
44
- [key: string]: string | undefined;
45
- };
46
- description?: string;
47
- title?: string;
48
- default?: Serializable;
49
- readOnly?: boolean;
50
- not?: JsonSchema;
51
- contentEncoding?: string;
52
- nullable?: boolean;
53
- };
8
+ type JsonSchemaObject = JSONSchema7;
54
9
  type ParserSelector = (schema: JsonSchemaObject, refs: Refs) => ZodTypeAny;
55
10
  type ParserOverride = (schema: JsonSchemaObject, refs: Refs) => ZodTypeAny | undefined;
56
11
  type JsonSchemaToZodOptions = {
@@ -67,6 +22,6 @@ type Refs = JsonSchemaToZodOptions & {
67
22
  }>;
68
23
  };
69
24
 
70
- declare const jsonSchemaToZod: <T extends z.ZodTypeAny = z.ZodTypeAny>(schema: JsonSchema, options?: JsonSchemaToZodOptions) => T;
25
+ declare const jsonSchemaToZod: (schema: JsonSchema, options?: JsonSchemaToZodOptions) => z.ZodType;
71
26
 
72
27
  export { type JsonSchema, type JsonSchemaObject, type JsonSchemaToZodOptions, type ParserOverride, type ParserSelector, type Refs, type Serializable, jsonSchemaToZod };
package/dist/index.d.ts CHANGED
@@ -1,56 +1,11 @@
1
1
  import { ZodTypeAny, z } from 'zod';
2
+ import { JSONSchema7 } from 'json-schema';
2
3
 
3
4
  type Serializable = {
4
5
  [key: string]: Serializable;
5
6
  } | Serializable[] | string | number | boolean | null;
6
7
  type JsonSchema = JsonSchemaObject | boolean;
7
- type JsonSchemaObject = {
8
- type?: string | string[];
9
- properties?: {
10
- [key: string]: JsonSchema;
11
- };
12
- additionalProperties?: JsonSchema;
13
- unevaluatedProperties?: JsonSchema;
14
- patternProperties?: {
15
- [key: string]: JsonSchema;
16
- };
17
- minProperties?: number;
18
- maxProperties?: number;
19
- required?: string[] | boolean;
20
- propertyNames?: JsonSchema;
21
- items?: JsonSchema | JsonSchema[];
22
- additionalItems?: JsonSchema;
23
- minItems?: number;
24
- maxItems?: number;
25
- uniqueItems?: boolean;
26
- minLength?: number;
27
- maxLength?: number;
28
- pattern?: string;
29
- format?: string;
30
- minimum?: number;
31
- maximum?: number;
32
- exclusiveMinimum?: number | boolean;
33
- exclusiveMaximum?: number | boolean;
34
- multipleOf?: number;
35
- anyOf?: JsonSchema[];
36
- allOf?: JsonSchema[];
37
- oneOf?: JsonSchema[];
38
- if?: JsonSchema;
39
- then?: JsonSchema;
40
- else?: JsonSchema;
41
- const?: Serializable;
42
- enum?: Serializable[];
43
- errorMessage?: {
44
- [key: string]: string | undefined;
45
- };
46
- description?: string;
47
- title?: string;
48
- default?: Serializable;
49
- readOnly?: boolean;
50
- not?: JsonSchema;
51
- contentEncoding?: string;
52
- nullable?: boolean;
53
- };
8
+ type JsonSchemaObject = JSONSchema7;
54
9
  type ParserSelector = (schema: JsonSchemaObject, refs: Refs) => ZodTypeAny;
55
10
  type ParserOverride = (schema: JsonSchemaObject, refs: Refs) => ZodTypeAny | undefined;
56
11
  type JsonSchemaToZodOptions = {
@@ -67,6 +22,6 @@ type Refs = JsonSchemaToZodOptions & {
67
22
  }>;
68
23
  };
69
24
 
70
- declare const jsonSchemaToZod: <T extends z.ZodTypeAny = z.ZodTypeAny>(schema: JsonSchema, options?: JsonSchemaToZodOptions) => T;
25
+ declare const jsonSchemaToZod: (schema: JsonSchema, options?: JsonSchemaToZodOptions) => z.ZodType;
71
26
 
72
27
  export { type JsonSchema, type JsonSchemaObject, type JsonSchemaToZodOptions, type ParserOverride, type ParserSelector, type Refs, type Serializable, jsonSchemaToZod };
package/dist/index.js CHANGED
@@ -66,8 +66,55 @@ function extendSchemaWithMessage(zodSchema, jsonSchema, key, extend) {
66
66
  return zodSchema;
67
67
  }
68
68
 
69
+ // src/utils/its.ts
70
+ var its = {
71
+ an: {
72
+ object: (x) => x.type === "object",
73
+ array: (x) => x.type === "array",
74
+ anyOf: (x) => x.anyOf !== void 0,
75
+ allOf: (x) => x.allOf !== void 0,
76
+ enum: (x) => x.enum !== void 0
77
+ },
78
+ a: {
79
+ nullable: (x) => x.nullable === true,
80
+ multipleType: (x) => Array.isArray(x.type),
81
+ not: (x) => x.not !== void 0,
82
+ const: (x) => x.const !== void 0,
83
+ primitive: (x, p) => x.type === p,
84
+ conditional: (x) => Boolean("if" in x && x.if && "then" in x && "else" in x && x.then && x.else),
85
+ oneOf: (x) => x.oneOf !== void 0
86
+ }
87
+ };
88
+
69
89
  // src/parsers/parse-array.ts
70
90
  var parseArray = (jsonSchema, refs) => {
91
+ if (its.an.anyOf(jsonSchema)) {
92
+ const types = /* @__PURE__ */ new Set();
93
+ let items;
94
+ jsonSchema.anyOf.forEach((option) => {
95
+ if (typeof option === "object" && option.type) {
96
+ types.add(typeof option.type === "string" ? option.type : option.type[0]);
97
+ }
98
+ if (typeof option === "object" && option.items) {
99
+ const optionItems = option.items;
100
+ if (!Array.isArray(optionItems) && typeof optionItems === "object") {
101
+ items = optionItems;
102
+ }
103
+ }
104
+ });
105
+ const newSchema = {
106
+ type: Array.from(types),
107
+ ...items && { items }
108
+ };
109
+ const metadataFields = ["default", "description", "examples", "title"];
110
+ metadataFields.forEach((field) => {
111
+ const value = jsonSchema[field];
112
+ if (value !== void 0) {
113
+ newSchema[field] = value;
114
+ }
115
+ });
116
+ return parseSchema(newSchema, refs);
117
+ }
71
118
  if (Array.isArray(jsonSchema.items)) {
72
119
  return z3.tuple(
73
120
  jsonSchema.items.map(
@@ -307,26 +354,6 @@ var parseOneOf = (jsonSchema, refs) => {
307
354
  });
308
355
  };
309
356
 
310
- // src/utils/its.ts
311
- var its = {
312
- an: {
313
- object: (x) => x.type === "object",
314
- array: (x) => x.type === "array",
315
- anyOf: (x) => x.anyOf !== void 0,
316
- allOf: (x) => x.allOf !== void 0,
317
- enum: (x) => x.enum !== void 0
318
- },
319
- a: {
320
- nullable: (x) => x.nullable === true,
321
- multipleType: (x) => Array.isArray(x.type),
322
- not: (x) => x.not !== void 0,
323
- const: (x) => x.const !== void 0,
324
- primitive: (x, p) => x.type === p,
325
- conditional: (x) => Boolean("if" in x && x.if && "then" in x && "else" in x && x.then && x.else),
326
- oneOf: (x) => x.oneOf !== void 0
327
- }
328
- };
329
-
330
357
  // src/parsers/parse-object.ts
331
358
  function parseObjectProperties(objectSchema, refs) {
332
359
  if (!objectSchema.properties) {
package/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "@composio/json-schema-to-zod",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "",
5
- "main": "dist/index.js",
5
+ "main": "src/index.ts",
6
6
  "type": "module",
7
7
  "publishConfig": {
8
- "access": "public"
8
+ "access": "public",
9
+ "main": "dist/index.js",
10
+ "types": "dist/index.d.ts"
9
11
  },
10
12
  "exports": {
11
13
  ".": {
@@ -18,20 +20,22 @@
18
20
  "README.md",
19
21
  "dist"
20
22
  ],
23
+ "scripts": {
24
+ "build": "tsup",
25
+ "test": "vitest run"
26
+ },
21
27
  "keywords": [],
22
28
  "author": "",
23
29
  "license": "ISC",
30
+ "packageManager": "pnpm@10.8.0",
24
31
  "devDependencies": {
32
+ "@types/node": "^24.0.1",
25
33
  "tsup": "^8.4.0",
26
34
  "typescript": "^5.8.3",
27
35
  "vitest": "^3.1.4"
28
36
  },
29
37
  "dependencies": {
38
+ "@types/json-schema": "^7.0.15",
30
39
  "zod": "^3.24.2"
31
- },
32
- "scripts": {
33
- "build": "tsup",
34
- "test": "vitest run"
35
- },
36
- "types": "dist/index.d.ts"
37
- }
40
+ }
41
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export type * from './types';
2
+ export { jsonSchemaToZod } from './json-schema-to-zod';