@composio/json-schema-to-zod 0.1.12 → 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 +32 -7
- package/dist/index.js +32 -7
- 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
|
}
|
|
@@ -546,10 +560,15 @@ function parseObject(objectSchema, refs) {
|
|
|
546
560
|
if (additionalProperties instanceof z14.ZodNever) {
|
|
547
561
|
output = propertiesSchema.strict();
|
|
548
562
|
} else {
|
|
549
|
-
|
|
563
|
+
const isEmptyObject = Object.keys(propertiesSchema._def.shape()).length === 0;
|
|
564
|
+
if (isEmptyObject) {
|
|
565
|
+
output = propertiesSchema.passthrough();
|
|
566
|
+
} else {
|
|
567
|
+
output = propertiesSchema.catchall(additionalProperties);
|
|
568
|
+
}
|
|
550
569
|
}
|
|
551
570
|
} else {
|
|
552
|
-
output = propertiesSchema.
|
|
571
|
+
output = propertiesSchema.passthrough();
|
|
553
572
|
}
|
|
554
573
|
} else {
|
|
555
574
|
if (hasPatternProperties) {
|
|
@@ -557,7 +576,7 @@ function parseObject(objectSchema, refs) {
|
|
|
557
576
|
} else if (additionalProperties) {
|
|
558
577
|
output = z14.record(additionalProperties);
|
|
559
578
|
} else {
|
|
560
|
-
output = z14.
|
|
579
|
+
output = z14.object({});
|
|
561
580
|
}
|
|
562
581
|
}
|
|
563
582
|
if (its.an.anyOf(objectSchema)) {
|
|
@@ -703,8 +722,14 @@ ${exampleText}` : exampleText;
|
|
|
703
722
|
}
|
|
704
723
|
return zodSchema;
|
|
705
724
|
};
|
|
706
|
-
var addDefaults = (jsonSchema, zodSchema) => {
|
|
725
|
+
var addDefaults = (jsonSchema, zodSchema, refs) => {
|
|
707
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
|
+
}
|
|
708
733
|
zodSchema = zodSchema.default(jsonSchema.default);
|
|
709
734
|
}
|
|
710
735
|
return zodSchema;
|
|
@@ -777,7 +802,7 @@ var parseSchema = (jsonSchema, refs = { seen: /* @__PURE__ */ new Map(), path: [
|
|
|
777
802
|
parsedZodSchema = addDescribes(jsonSchema, parsedZodSchema);
|
|
778
803
|
}
|
|
779
804
|
if (!refs.withoutDefaults) {
|
|
780
|
-
parsedZodSchema = addDefaults(jsonSchema, parsedZodSchema);
|
|
805
|
+
parsedZodSchema = addDefaults(jsonSchema, parsedZodSchema, refs);
|
|
781
806
|
}
|
|
782
807
|
parsedZodSchema = addAnnotations(jsonSchema, parsedZodSchema);
|
|
783
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
|
}
|
|
@@ -510,10 +524,15 @@ function parseObject(objectSchema, refs) {
|
|
|
510
524
|
if (additionalProperties instanceof z14.ZodNever) {
|
|
511
525
|
output = propertiesSchema.strict();
|
|
512
526
|
} else {
|
|
513
|
-
|
|
527
|
+
const isEmptyObject = Object.keys(propertiesSchema._def.shape()).length === 0;
|
|
528
|
+
if (isEmptyObject) {
|
|
529
|
+
output = propertiesSchema.passthrough();
|
|
530
|
+
} else {
|
|
531
|
+
output = propertiesSchema.catchall(additionalProperties);
|
|
532
|
+
}
|
|
514
533
|
}
|
|
515
534
|
} else {
|
|
516
|
-
output = propertiesSchema.
|
|
535
|
+
output = propertiesSchema.passthrough();
|
|
517
536
|
}
|
|
518
537
|
} else {
|
|
519
538
|
if (hasPatternProperties) {
|
|
@@ -521,7 +540,7 @@ function parseObject(objectSchema, refs) {
|
|
|
521
540
|
} else if (additionalProperties) {
|
|
522
541
|
output = z14.record(additionalProperties);
|
|
523
542
|
} else {
|
|
524
|
-
output = z14.
|
|
543
|
+
output = z14.object({});
|
|
525
544
|
}
|
|
526
545
|
}
|
|
527
546
|
if (its.an.anyOf(objectSchema)) {
|
|
@@ -667,8 +686,14 @@ ${exampleText}` : exampleText;
|
|
|
667
686
|
}
|
|
668
687
|
return zodSchema;
|
|
669
688
|
};
|
|
670
|
-
var addDefaults = (jsonSchema, zodSchema) => {
|
|
689
|
+
var addDefaults = (jsonSchema, zodSchema, refs) => {
|
|
671
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
|
+
}
|
|
672
697
|
zodSchema = zodSchema.default(jsonSchema.default);
|
|
673
698
|
}
|
|
674
699
|
return zodSchema;
|
|
@@ -741,7 +766,7 @@ var parseSchema = (jsonSchema, refs = { seen: /* @__PURE__ */ new Map(), path: [
|
|
|
741
766
|
parsedZodSchema = addDescribes(jsonSchema, parsedZodSchema);
|
|
742
767
|
}
|
|
743
768
|
if (!refs.withoutDefaults) {
|
|
744
|
-
parsedZodSchema = addDefaults(jsonSchema, parsedZodSchema);
|
|
769
|
+
parsedZodSchema = addDefaults(jsonSchema, parsedZodSchema, refs);
|
|
745
770
|
}
|
|
746
771
|
parsedZodSchema = addAnnotations(jsonSchema, parsedZodSchema);
|
|
747
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",
|