@composio/json-schema-to-zod 0.1.13 → 0.1.14
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 +24 -4
- package/dist/index.js +24 -4
- package/package.json +3 -2
package/dist/index.cjs
CHANGED
|
@@ -287,7 +287,10 @@ var omit = (obj, ...keys) => Object.keys(obj).reduce((acc, key) => {
|
|
|
287
287
|
|
|
288
288
|
// src/parsers/parse-nullable.ts
|
|
289
289
|
var parseNullable = (jsonSchema, refs) => {
|
|
290
|
-
|
|
290
|
+
const hasNullDefault = jsonSchema.default === null;
|
|
291
|
+
const baseSchema = hasNullDefault ? omit(omit(jsonSchema, "nullable"), "default") : omit(jsonSchema, "nullable");
|
|
292
|
+
const zodSchema = parseSchema(baseSchema, refs, true).nullable();
|
|
293
|
+
return hasNullDefault ? zodSchema.default(null) : zodSchema;
|
|
291
294
|
};
|
|
292
295
|
|
|
293
296
|
// src/parsers/parse-number.ts
|
|
@@ -447,7 +450,18 @@ function parseObjectProperties(objectSchema, refs) {
|
|
|
447
450
|
const required = Array.isArray(objectSchema.required) ? objectSchema.required.includes(key) : false;
|
|
448
451
|
if (!required && propJsonSchema && typeof propJsonSchema === "object" && "default" in propJsonSchema) {
|
|
449
452
|
if (propJsonSchema.default === null) {
|
|
450
|
-
|
|
453
|
+
const hasAnyOfWithNull = propJsonSchema.anyOf && Array.isArray(propJsonSchema.anyOf) && propJsonSchema.anyOf.some(
|
|
454
|
+
(schema) => typeof schema === "object" && schema !== null && schema.type === "null"
|
|
455
|
+
);
|
|
456
|
+
const hasOneOfWithNull = propJsonSchema.oneOf && Array.isArray(propJsonSchema.oneOf) && propJsonSchema.oneOf.some(
|
|
457
|
+
(schema) => typeof schema === "object" && schema !== null && schema.type === "null"
|
|
458
|
+
);
|
|
459
|
+
const isNullable = "nullable" in propJsonSchema && propJsonSchema.nullable === true;
|
|
460
|
+
if (hasAnyOfWithNull || hasOneOfWithNull || isNullable) {
|
|
461
|
+
properties[key] = propZodSchema.optional().default(null);
|
|
462
|
+
} else {
|
|
463
|
+
properties[key] = propZodSchema.nullable().optional().default(null);
|
|
464
|
+
}
|
|
451
465
|
} else {
|
|
452
466
|
properties[key] = propZodSchema.optional().default(propJsonSchema.default);
|
|
453
467
|
}
|
|
@@ -708,8 +722,14 @@ ${exampleText}` : exampleText;
|
|
|
708
722
|
}
|
|
709
723
|
return zodSchema;
|
|
710
724
|
};
|
|
711
|
-
var addDefaults = (jsonSchema, zodSchema) => {
|
|
725
|
+
var addDefaults = (jsonSchema, zodSchema, refs) => {
|
|
712
726
|
if (jsonSchema.default !== void 0) {
|
|
727
|
+
if (jsonSchema.default === null) {
|
|
728
|
+
const inAnyOfOrOneOf = refs?.path.some((segment) => segment === "anyOf" || segment === "oneOf");
|
|
729
|
+
if (inAnyOfOrOneOf && jsonSchema.type && jsonSchema.type !== "null" && !jsonSchema.nullable) {
|
|
730
|
+
return zodSchema;
|
|
731
|
+
}
|
|
732
|
+
}
|
|
713
733
|
zodSchema = zodSchema.default(jsonSchema.default);
|
|
714
734
|
}
|
|
715
735
|
return zodSchema;
|
|
@@ -782,7 +802,7 @@ var parseSchema = (jsonSchema, refs = { seen: /* @__PURE__ */ new Map(), path: [
|
|
|
782
802
|
parsedZodSchema = addDescribes(jsonSchema, parsedZodSchema);
|
|
783
803
|
}
|
|
784
804
|
if (!refs.withoutDefaults) {
|
|
785
|
-
parsedZodSchema = addDefaults(jsonSchema, parsedZodSchema);
|
|
805
|
+
parsedZodSchema = addDefaults(jsonSchema, parsedZodSchema, refs);
|
|
786
806
|
}
|
|
787
807
|
parsedZodSchema = addAnnotations(jsonSchema, parsedZodSchema);
|
|
788
808
|
}
|
package/dist/index.js
CHANGED
|
@@ -251,7 +251,10 @@ var omit = (obj, ...keys) => Object.keys(obj).reduce((acc, key) => {
|
|
|
251
251
|
|
|
252
252
|
// src/parsers/parse-nullable.ts
|
|
253
253
|
var parseNullable = (jsonSchema, refs) => {
|
|
254
|
-
|
|
254
|
+
const hasNullDefault = jsonSchema.default === null;
|
|
255
|
+
const baseSchema = hasNullDefault ? omit(omit(jsonSchema, "nullable"), "default") : omit(jsonSchema, "nullable");
|
|
256
|
+
const zodSchema = parseSchema(baseSchema, refs, true).nullable();
|
|
257
|
+
return hasNullDefault ? zodSchema.default(null) : zodSchema;
|
|
255
258
|
};
|
|
256
259
|
|
|
257
260
|
// src/parsers/parse-number.ts
|
|
@@ -411,7 +414,18 @@ function parseObjectProperties(objectSchema, refs) {
|
|
|
411
414
|
const required = Array.isArray(objectSchema.required) ? objectSchema.required.includes(key) : false;
|
|
412
415
|
if (!required && propJsonSchema && typeof propJsonSchema === "object" && "default" in propJsonSchema) {
|
|
413
416
|
if (propJsonSchema.default === null) {
|
|
414
|
-
|
|
417
|
+
const hasAnyOfWithNull = propJsonSchema.anyOf && Array.isArray(propJsonSchema.anyOf) && propJsonSchema.anyOf.some(
|
|
418
|
+
(schema) => typeof schema === "object" && schema !== null && schema.type === "null"
|
|
419
|
+
);
|
|
420
|
+
const hasOneOfWithNull = propJsonSchema.oneOf && Array.isArray(propJsonSchema.oneOf) && propJsonSchema.oneOf.some(
|
|
421
|
+
(schema) => typeof schema === "object" && schema !== null && schema.type === "null"
|
|
422
|
+
);
|
|
423
|
+
const isNullable = "nullable" in propJsonSchema && propJsonSchema.nullable === true;
|
|
424
|
+
if (hasAnyOfWithNull || hasOneOfWithNull || isNullable) {
|
|
425
|
+
properties[key] = propZodSchema.optional().default(null);
|
|
426
|
+
} else {
|
|
427
|
+
properties[key] = propZodSchema.nullable().optional().default(null);
|
|
428
|
+
}
|
|
415
429
|
} else {
|
|
416
430
|
properties[key] = propZodSchema.optional().default(propJsonSchema.default);
|
|
417
431
|
}
|
|
@@ -672,8 +686,14 @@ ${exampleText}` : exampleText;
|
|
|
672
686
|
}
|
|
673
687
|
return zodSchema;
|
|
674
688
|
};
|
|
675
|
-
var addDefaults = (jsonSchema, zodSchema) => {
|
|
689
|
+
var addDefaults = (jsonSchema, zodSchema, refs) => {
|
|
676
690
|
if (jsonSchema.default !== void 0) {
|
|
691
|
+
if (jsonSchema.default === null) {
|
|
692
|
+
const inAnyOfOrOneOf = refs?.path.some((segment) => segment === "anyOf" || segment === "oneOf");
|
|
693
|
+
if (inAnyOfOrOneOf && jsonSchema.type && jsonSchema.type !== "null" && !jsonSchema.nullable) {
|
|
694
|
+
return zodSchema;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
677
697
|
zodSchema = zodSchema.default(jsonSchema.default);
|
|
678
698
|
}
|
|
679
699
|
return zodSchema;
|
|
@@ -746,7 +766,7 @@ var parseSchema = (jsonSchema, refs = { seen: /* @__PURE__ */ new Map(), path: [
|
|
|
746
766
|
parsedZodSchema = addDescribes(jsonSchema, parsedZodSchema);
|
|
747
767
|
}
|
|
748
768
|
if (!refs.withoutDefaults) {
|
|
749
|
-
parsedZodSchema = addDefaults(jsonSchema, parsedZodSchema);
|
|
769
|
+
parsedZodSchema = addDefaults(jsonSchema, parsedZodSchema, refs);
|
|
750
770
|
}
|
|
751
771
|
parsedZodSchema = addAnnotations(jsonSchema, parsedZodSchema);
|
|
752
772
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@composio/json-schema-to-zod",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.14",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -25,7 +25,8 @@
|
|
|
25
25
|
"@types/node": "^24.0.1",
|
|
26
26
|
"tsup": "^8.4.0",
|
|
27
27
|
"typescript": "^5.8.3",
|
|
28
|
-
"vitest": "^3.1.4"
|
|
28
|
+
"vitest": "^3.1.4",
|
|
29
|
+
"zod-to-json-schema": "^3.24.5"
|
|
29
30
|
},
|
|
30
31
|
"dependencies": {
|
|
31
32
|
"@types/json-schema": "^7.0.15",
|