@composio/json-schema-to-zod 0.1.13 → 0.1.15

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
@@ -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
- return parseSchema(omit(jsonSchema, "nullable"), refs, true).nullable();
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
- properties[key] = propZodSchema.nullable().optional().default(null);
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
  }
@@ -465,6 +479,7 @@ function parseObject(objectSchema, refs) {
465
479
  ...refs,
466
480
  path: [...refs.path, "additionalProperties"]
467
481
  }) : void 0;
482
+ const isAdditionalPropertiesTrue = objectSchema.additionalProperties === true;
468
483
  if (objectSchema.patternProperties) {
469
484
  const parsedPatternProperties = Object.fromEntries(
470
485
  Object.entries(objectSchema.patternProperties).map(([key, value]) => {
@@ -545,6 +560,8 @@ function parseObject(objectSchema, refs) {
545
560
  } else if (additionalProperties) {
546
561
  if (additionalProperties instanceof z14.ZodNever) {
547
562
  output = propertiesSchema.strict();
563
+ } else if (isAdditionalPropertiesTrue) {
564
+ output = propertiesSchema.passthrough();
548
565
  } else {
549
566
  const isEmptyObject = Object.keys(propertiesSchema._def.shape()).length === 0;
550
567
  if (isEmptyObject) {
@@ -560,9 +577,15 @@ function parseObject(objectSchema, refs) {
560
577
  if (hasPatternProperties) {
561
578
  output = zodSchema;
562
579
  } else if (additionalProperties) {
563
- output = z14.record(additionalProperties);
580
+ if (additionalProperties instanceof z14.ZodNever) {
581
+ output = z14.object({}).strict();
582
+ } else if (isAdditionalPropertiesTrue) {
583
+ output = z14.object({}).passthrough();
584
+ } else {
585
+ output = z14.record(additionalProperties);
586
+ }
564
587
  } else {
565
- output = z14.object({});
588
+ output = z14.object({}).passthrough();
566
589
  }
567
590
  }
568
591
  if (its.an.anyOf(objectSchema)) {
@@ -708,8 +731,14 @@ ${exampleText}` : exampleText;
708
731
  }
709
732
  return zodSchema;
710
733
  };
711
- var addDefaults = (jsonSchema, zodSchema) => {
734
+ var addDefaults = (jsonSchema, zodSchema, refs) => {
712
735
  if (jsonSchema.default !== void 0) {
736
+ if (jsonSchema.default === null) {
737
+ const inAnyOfOrOneOf = refs?.path.some((segment) => segment === "anyOf" || segment === "oneOf");
738
+ if (inAnyOfOrOneOf && jsonSchema.type && jsonSchema.type !== "null" && !jsonSchema.nullable) {
739
+ return zodSchema;
740
+ }
741
+ }
713
742
  zodSchema = zodSchema.default(jsonSchema.default);
714
743
  }
715
744
  return zodSchema;
@@ -782,7 +811,7 @@ var parseSchema = (jsonSchema, refs = { seen: /* @__PURE__ */ new Map(), path: [
782
811
  parsedZodSchema = addDescribes(jsonSchema, parsedZodSchema);
783
812
  }
784
813
  if (!refs.withoutDefaults) {
785
- parsedZodSchema = addDefaults(jsonSchema, parsedZodSchema);
814
+ parsedZodSchema = addDefaults(jsonSchema, parsedZodSchema, refs);
786
815
  }
787
816
  parsedZodSchema = addAnnotations(jsonSchema, parsedZodSchema);
788
817
  }
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
- return parseSchema(omit(jsonSchema, "nullable"), refs, true).nullable();
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
- properties[key] = propZodSchema.nullable().optional().default(null);
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
  }
@@ -429,6 +443,7 @@ function parseObject(objectSchema, refs) {
429
443
  ...refs,
430
444
  path: [...refs.path, "additionalProperties"]
431
445
  }) : void 0;
446
+ const isAdditionalPropertiesTrue = objectSchema.additionalProperties === true;
432
447
  if (objectSchema.patternProperties) {
433
448
  const parsedPatternProperties = Object.fromEntries(
434
449
  Object.entries(objectSchema.patternProperties).map(([key, value]) => {
@@ -509,6 +524,8 @@ function parseObject(objectSchema, refs) {
509
524
  } else if (additionalProperties) {
510
525
  if (additionalProperties instanceof z14.ZodNever) {
511
526
  output = propertiesSchema.strict();
527
+ } else if (isAdditionalPropertiesTrue) {
528
+ output = propertiesSchema.passthrough();
512
529
  } else {
513
530
  const isEmptyObject = Object.keys(propertiesSchema._def.shape()).length === 0;
514
531
  if (isEmptyObject) {
@@ -524,9 +541,15 @@ function parseObject(objectSchema, refs) {
524
541
  if (hasPatternProperties) {
525
542
  output = zodSchema;
526
543
  } else if (additionalProperties) {
527
- output = z14.record(additionalProperties);
544
+ if (additionalProperties instanceof z14.ZodNever) {
545
+ output = z14.object({}).strict();
546
+ } else if (isAdditionalPropertiesTrue) {
547
+ output = z14.object({}).passthrough();
548
+ } else {
549
+ output = z14.record(additionalProperties);
550
+ }
528
551
  } else {
529
- output = z14.object({});
552
+ output = z14.object({}).passthrough();
530
553
  }
531
554
  }
532
555
  if (its.an.anyOf(objectSchema)) {
@@ -672,8 +695,14 @@ ${exampleText}` : exampleText;
672
695
  }
673
696
  return zodSchema;
674
697
  };
675
- var addDefaults = (jsonSchema, zodSchema) => {
698
+ var addDefaults = (jsonSchema, zodSchema, refs) => {
676
699
  if (jsonSchema.default !== void 0) {
700
+ if (jsonSchema.default === null) {
701
+ const inAnyOfOrOneOf = refs?.path.some((segment) => segment === "anyOf" || segment === "oneOf");
702
+ if (inAnyOfOrOneOf && jsonSchema.type && jsonSchema.type !== "null" && !jsonSchema.nullable) {
703
+ return zodSchema;
704
+ }
705
+ }
677
706
  zodSchema = zodSchema.default(jsonSchema.default);
678
707
  }
679
708
  return zodSchema;
@@ -746,7 +775,7 @@ var parseSchema = (jsonSchema, refs = { seen: /* @__PURE__ */ new Map(), path: [
746
775
  parsedZodSchema = addDescribes(jsonSchema, parsedZodSchema);
747
776
  }
748
777
  if (!refs.withoutDefaults) {
749
- parsedZodSchema = addDefaults(jsonSchema, parsedZodSchema);
778
+ parsedZodSchema = addDefaults(jsonSchema, parsedZodSchema, refs);
750
779
  }
751
780
  parsedZodSchema = addAnnotations(jsonSchema, parsedZodSchema);
752
781
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@composio/json-schema-to-zod",
3
- "version": "0.1.13",
3
+ "version": "0.1.15",
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",