@composio/json-schema-to-zod 0.3.2 → 0.3.3-beta.0

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.d.mts CHANGED
@@ -185,7 +185,7 @@ type Refs = JsonSchemaToZodOptions & {
185
185
  };
186
186
  //#endregion
187
187
  //#region src/json-schema-to-zod.d.ts
188
- declare const jsonSchemaToZod: (schema: JsonSchema, options?: JsonSchemaToZodOptions) => z.ZodType;
188
+ export declare const jsonSchemaToZod: (schema: JsonSchema, options?: JsonSchemaToZodOptions) => z.ZodType;
189
189
  /**
190
190
  * A simplified type for Zod raw shapes that avoids deep type instantiation issues.
191
191
  * This is compatible with ZodRawShape but uses a simpler type signature.
@@ -203,6 +203,24 @@ type SimpleZodRawShape = Record<string, ZodTypeAny>;
203
203
  * @param options - Optional configuration for the conversion
204
204
  * @returns A Zod raw shape object with property keys mapped to Zod types
205
205
  */
206
- declare const jsonSchemaToZodShape: (schema: JsonSchema, options?: JsonSchemaToZodOptions) => SimpleZodRawShape;
206
+ export declare const jsonSchemaToZodShape: (schema: JsonSchema, options?: JsonSchemaToZodOptions) => SimpleZodRawShape;
207
207
  //#endregion
208
- export { type JsonSchema, type JsonSchemaObject, type JsonSchemaToZodOptions, type ParserOverride, type ParserSelector, type Refs, type Serializable, jsonSchemaToZod, jsonSchemaToZodShape };
208
+ //#region src/utils/compile-pattern.d.ts
209
+ type InvalidPatternReason = 'syntax' | 'too-long';
210
+ /**
211
+ * Raised at conversion time when a schema `pattern` cannot be turned into a
212
+ * `RegExp`. Conversion fails eagerly, in line with how this package and
213
+ * `@composio/json-schema-to-effect-schema` treat other schema defects: a broken
214
+ * schema is the tool author's bug, so it surfaces once at construction rather
215
+ * than on every validation as if the caller's input were wrong.
216
+ */
217
+ export declare class InvalidPatternError extends Error {
218
+ readonly keyword: 'pattern' | 'patternProperties';
219
+ readonly pattern: string;
220
+ readonly path: ReadonlyArray<string | number>;
221
+ readonly reason: InvalidPatternReason;
222
+ readonly name = "InvalidPatternError";
223
+ constructor(keyword: 'pattern' | 'patternProperties', pattern: string, path: ReadonlyArray<string | number>, reason: InvalidPatternReason, detail: string, options?: ErrorOptions);
224
+ }
225
+ //#endregion
226
+ export type { InvalidPatternReason, JsonSchema, JsonSchemaObject, JsonSchemaToZodOptions, ParserOverride, ParserSelector, Refs, Serializable };
package/dist/index.mjs CHANGED
@@ -291,7 +291,8 @@ const omit = (obj, ...keys) => Object.keys(obj).reduce((acc, key) => {
291
291
  */
292
292
  const parseNullable = (jsonSchema, refs) => {
293
293
  const hasNullDefault = jsonSchema.default === null;
294
- const zodSchema = parseSchema(hasNullDefault ? omit(omit(jsonSchema, "nullable"), "default") : omit(jsonSchema, "nullable"), refs, true).nullable();
294
+ const baseSchema = hasNullDefault ? omit(omit(jsonSchema, "nullable"), "default") : omit(jsonSchema, "nullable");
295
+ const zodSchema = parseSchema(baseSchema, refs, true).nullable();
295
296
  return hasNullDefault ? zodSchema.default(null) : zodSchema;
296
297
  };
297
298
 
@@ -331,11 +332,15 @@ const parseNumber = (jsonSchema) => {
331
332
  isInteger = true;
332
333
  zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "format", (zs, _, errorMsg) => zs.int(errorMsg));
333
334
  }
334
- if (typeof jsonSchema.minimum === "number") if (jsonSchema.exclusiveMinimum === true) zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "minimum", (zs, minimum, errorMsg) => zs.gt(minimum, errorMsg));
335
- else zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "minimum", (zs, minimum, errorMsg) => zs.gte(minimum, errorMsg));
335
+ if (typeof jsonSchema.minimum === "number") {
336
+ if (jsonSchema.exclusiveMinimum === true) zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "minimum", (zs, minimum, errorMsg) => zs.gt(minimum, errorMsg));
337
+ else zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "minimum", (zs, minimum, errorMsg) => zs.gte(minimum, errorMsg));
338
+ }
336
339
  if (typeof jsonSchema.exclusiveMinimum === "number") zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "exclusiveMinimum", (zs, exclusiveMinimum, errorMsg) => zs.gt(exclusiveMinimum, errorMsg));
337
- if (typeof jsonSchema.maximum === "number") if (jsonSchema.exclusiveMaximum === true) zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "maximum", (zs, maximum, errorMsg) => zs.lt(maximum, errorMsg));
338
- else zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "maximum", (zs, maximum, errorMsg) => zs.lte(maximum, errorMsg));
340
+ if (typeof jsonSchema.maximum === "number") {
341
+ if (jsonSchema.exclusiveMaximum === true) zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "maximum", (zs, maximum, errorMsg) => zs.lt(maximum, errorMsg));
342
+ else zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "maximum", (zs, maximum, errorMsg) => zs.lte(maximum, errorMsg));
343
+ }
339
344
  if (typeof jsonSchema.exclusiveMaximum === "number") zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "exclusiveMaximum", (zs, exclusiveMaximum, errorMsg) => zs.lt(exclusiveMaximum, errorMsg));
340
345
  if (typeof jsonSchema.min === "number" && typeof jsonSchema.minimum !== "number") zodSchema = extendSchemaWithMessage(zodSchema, {
341
346
  ...jsonSchema,
@@ -384,6 +389,66 @@ const parseOneOf = (jsonSchema, refs) => {
384
389
  });
385
390
  };
386
391
 
392
+ //#endregion
393
+ //#region src/utils/compile-pattern.ts
394
+ /**
395
+ * Longest `pattern` / `patternProperties` key this package compiles. Tool
396
+ * schemas come from third-party toolkit definitions; anything past this is
397
+ * far outside what a format constraint needs and is rejected before it can
398
+ * cost anything at validation time.
399
+ */
400
+ const MAX_PATTERN_LENGTH = 1024;
401
+ /**
402
+ * Raised at conversion time when a schema `pattern` cannot be turned into a
403
+ * `RegExp`. Conversion fails eagerly, in line with how this package and
404
+ * `@composio/json-schema-to-effect-schema` treat other schema defects: a broken
405
+ * schema is the tool author's bug, so it surfaces once at construction rather
406
+ * than on every validation as if the caller's input were wrong.
407
+ */
408
+ var InvalidPatternError = class extends Error {
409
+ keyword;
410
+ pattern;
411
+ path;
412
+ reason;
413
+ name = "InvalidPatternError";
414
+ constructor(keyword, pattern, path, reason, detail, options) {
415
+ const location = path.length > 0 ? path.join(".") : "<root>";
416
+ super(`Invalid ${keyword} regular expression ${JSON.stringify(pattern)} at ${location}: ${detail}`, options);
417
+ this.keyword = keyword;
418
+ this.pattern = pattern;
419
+ this.path = path;
420
+ this.reason = reason;
421
+ }
422
+ };
423
+ /**
424
+ * Compile a schema pattern, rejecting anything that does not compile or that
425
+ * exceeds {@link MAX_PATTERN_LENGTH}.
426
+ *
427
+ * Both guards have no false positives: a pattern that fails here would never
428
+ * have worked. No backtracking (ReDoS) heuristic is applied on purpose — the
429
+ * syntactic checks that exist flag linear patterns such as `^(\d+\.)*\d+$`,
430
+ * and a wrong rejection fails the whole tool the schema belongs to. A hostile
431
+ * `pattern` that backtracks catastrophically remains a known limitation.
432
+ *
433
+ * No `u` flag is passed: existing tool schemas rely on identity escapes that
434
+ * Unicode mode refuses, and the goal here is to fail on defects, not to
435
+ * tighten which patterns are accepted.
436
+ */
437
+ const compilePattern = (keyword, pattern, refs) => {
438
+ const path = keyword === "pattern" ? refs.path : [
439
+ ...refs.path,
440
+ "patternProperties",
441
+ pattern
442
+ ];
443
+ if (pattern.length > 1024) throw new InvalidPatternError(keyword, pattern, path, "too-long", `pattern exceeds ${MAX_PATTERN_LENGTH} characters`);
444
+ try {
445
+ return new RegExp(pattern);
446
+ } catch (cause) {
447
+ const detail = cause instanceof Error ? cause.message : String(cause);
448
+ throw new InvalidPatternError(keyword, pattern, path, "syntax", detail, { cause });
449
+ }
450
+ };
451
+
387
452
  //#endregion
388
453
  //#region src/parsers/parse-object.ts
389
454
  /**
@@ -413,14 +478,15 @@ function parseObjectProperties(objectSchema, refs) {
413
478
  ]
414
479
  });
415
480
  const required = Array.isArray(objectSchema.required) ? objectSchema.required.includes(key) : false;
416
- if (!required && propJsonSchema && typeof propJsonSchema === "object" && "default" in propJsonSchema) if (propJsonSchema.default === null) {
417
- const hasAnyOfWithNull = propJsonSchema.anyOf && Array.isArray(propJsonSchema.anyOf) && propJsonSchema.anyOf.some((schema) => typeof schema === "object" && schema !== null && schema.type === "null");
418
- const hasOneOfWithNull = propJsonSchema.oneOf && Array.isArray(propJsonSchema.oneOf) && propJsonSchema.oneOf.some((schema) => typeof schema === "object" && schema !== null && schema.type === "null");
419
- const isNullable = "nullable" in propJsonSchema && propJsonSchema.nullable === true;
420
- if (hasAnyOfWithNull || hasOneOfWithNull || isNullable) properties[key] = propZodSchema.optional().default(null);
421
- else properties[key] = propZodSchema.nullable().optional().default(null);
422
- } else properties[key] = propZodSchema.optional().default(propJsonSchema.default);
423
- else properties[key] = required ? propZodSchema : propZodSchema.optional();
481
+ if (!required && propJsonSchema && typeof propJsonSchema === "object" && "default" in propJsonSchema) {
482
+ if (propJsonSchema.default === null) {
483
+ const hasAnyOfWithNull = propJsonSchema.anyOf && Array.isArray(propJsonSchema.anyOf) && propJsonSchema.anyOf.some((schema) => typeof schema === "object" && schema !== null && schema.type === "null");
484
+ const hasOneOfWithNull = propJsonSchema.oneOf && Array.isArray(propJsonSchema.oneOf) && propJsonSchema.oneOf.some((schema) => typeof schema === "object" && schema !== null && schema.type === "null");
485
+ const isNullable = "nullable" in propJsonSchema && propJsonSchema.nullable === true;
486
+ if (hasAnyOfWithNull || hasOneOfWithNull || isNullable) properties[key] = propZodSchema.optional().default(null);
487
+ else properties[key] = propZodSchema.nullable().optional().default(null);
488
+ } else properties[key] = propZodSchema.optional().default(propJsonSchema.default);
489
+ } else properties[key] = required ? propZodSchema : propZodSchema.optional();
424
490
  }
425
491
  return z$1.object(properties);
426
492
  }
@@ -434,7 +500,7 @@ function parseObjectProperties(objectSchema, refs) {
434
500
  */
435
501
  function parseDynamicKeyObject(normalizedSchema, propertiesSchema, additionalProperties, refs) {
436
502
  const patterns = Object.entries(normalizedSchema.patternProperties ?? {}).map(([pattern, schema]) => ({
437
- regex: new RegExp(pattern),
503
+ regex: compilePattern("patternProperties", pattern, refs),
438
504
  parser: parseSchema(schema, {
439
505
  ...refs,
440
506
  path: [
@@ -498,14 +564,17 @@ function parseObject(objectSchema, refs) {
498
564
  const isAdditionalPropertiesTrue = normalizedSchema.additionalProperties === true;
499
565
  let output;
500
566
  if (hasPatternProperties) output = parseDynamicKeyObject(normalizedSchema, propertiesSchema, additionalProperties, refs);
501
- else if (propertiesSchema) if (additionalProperties) if (additionalProperties instanceof z$1.ZodNever) output = propertiesSchema.strict();
502
- else if (isAdditionalPropertiesTrue) output = propertiesSchema.passthrough();
503
- else output = propertiesSchema.catchall(additionalProperties);
504
- else output = propertiesSchema.strict();
505
- else if (additionalProperties) if (additionalProperties instanceof z$1.ZodNever) output = z$1.object({}).strict();
506
- else if (isAdditionalPropertiesTrue) output = z$1.object({}).passthrough();
507
- else output = z$1.record(additionalProperties);
508
- else output = z$1.object({}).passthrough();
567
+ else if (propertiesSchema) {
568
+ if (additionalProperties) {
569
+ if (additionalProperties instanceof z$1.ZodNever) output = propertiesSchema.strict();
570
+ else if (isAdditionalPropertiesTrue) output = propertiesSchema.passthrough();
571
+ else output = propertiesSchema.catchall(additionalProperties);
572
+ } else output = propertiesSchema.strict();
573
+ } else if (additionalProperties) {
574
+ if (additionalProperties instanceof z$1.ZodNever) output = z$1.object({}).strict();
575
+ else if (isAdditionalPropertiesTrue) output = z$1.object({}).passthrough();
576
+ else output = z$1.record(additionalProperties);
577
+ } else output = z$1.object({}).passthrough();
509
578
  if (its.an.anyOf(objectSchema)) output = output.and(parseAnyOf({
510
579
  ...objectSchema,
511
580
  anyOf: objectSchema.anyOf.map((x) => typeof x === "object" && !x.type && (x.properties ?? x.additionalProperties ?? x.patternProperties) ? {
@@ -532,7 +601,7 @@ function parseObject(objectSchema, refs) {
532
601
 
533
602
  //#endregion
534
603
  //#region src/parsers/parse-string.ts
535
- const parseString = (jsonSchema) => {
604
+ const parseString = (jsonSchema, refs) => {
536
605
  let zodSchema = z.string();
537
606
  zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "format", (zs, format, errorMsg) => {
538
607
  switch (format) {
@@ -560,7 +629,7 @@ const parseString = (jsonSchema) => {
560
629
  }
561
630
  });
562
631
  zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "contentEncoding", (zs, _, errorMsg) => zs.base64(errorMsg));
563
- zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "pattern", (zs, pattern, errorMsg) => zs.regex(new RegExp(pattern), errorMsg));
632
+ zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "pattern", (zs, pattern, errorMsg) => zs.regex(compilePattern("pattern", pattern, refs), errorMsg));
564
633
  const errorMessages = jsonSchema.errorMessage;
565
634
  const minLength = typeof jsonSchema.minLength === "number" ? jsonSchema.minLength : typeof jsonSchema.min === "number" ? jsonSchema.min : void 0;
566
635
  const maxLength = typeof jsonSchema.maxLength === "number" ? jsonSchema.maxLength : typeof jsonSchema.max === "number" ? jsonSchema.max : void 0;
@@ -589,11 +658,11 @@ const hasTypelessScalarConstraints = (schema) => schema.type === void 0 && SCALA
589
658
  * schema declares no `type`: `{ "minLength": 2 }` restricts strings while
590
659
  * leaving every non-string value untouched.
591
660
  */
592
- const parseTypelessConstraints = (schema) => {
661
+ const parseTypelessConstraints = (schema, refs) => {
593
662
  const stringSchema = parseString({
594
663
  ...schema,
595
664
  type: "string"
596
- });
665
+ }, refs);
597
666
  const numberSchema = parseNumber({
598
667
  ...schema,
599
668
  type: "number"
@@ -806,9 +875,9 @@ const selectParser = (schema, refs) => {
806
875
  else if (its.an.allOf(schema)) return parseAllOf(schema, refs);
807
876
  else if (its.a.oneOf(schema)) return parseOneOf(schema, refs);
808
877
  else if (its.a.not(schema)) return parseNot(schema, refs);
809
- else if (hasTypelessScalarConstraints(schema)) return parseTypelessConstraints(schema);
878
+ else if (hasTypelessScalarConstraints(schema)) return parseTypelessConstraints(schema, refs);
810
879
  else if (its.a.multipleType(schema)) return parseMultipleType(schema, refs);
811
- else if (its.a.primitive(schema, "string")) return parseString(schema);
880
+ else if (its.a.primitive(schema, "string")) return parseString(schema, refs);
812
881
  else if (its.a.primitive(schema, "number") || its.a.primitive(schema, "integer")) return parseNumber(schema);
813
882
  else if (its.a.primitive(schema, "boolean")) return parseBoolean(schema);
814
883
  else if (its.a.primitive(schema, "null")) return parseNull(schema);
@@ -875,14 +944,15 @@ function parseObjectShape(objectSchema, refs) {
875
944
  const parsedProperty = parseSchema(propJsonSchema, propRefs);
876
945
  const propZodSchema = requiresWholeSchemaValidation(propJsonSchema) ? withWholeSchemaValidation(guardSchemaAt(propJsonSchema, propRefs), parsedProperty) : parsedProperty;
877
946
  const required = Array.isArray(objectSchema.required) ? objectSchema.required.includes(key) : false;
878
- if (!required && propJsonSchema && typeof propJsonSchema === "object" && "default" in propJsonSchema) if (propJsonSchema.default === null) {
879
- const hasAnyOfWithNull = propJsonSchema.anyOf && Array.isArray(propJsonSchema.anyOf) && propJsonSchema.anyOf.some((schema) => typeof schema === "object" && schema !== null && schema.type === "null");
880
- const hasOneOfWithNull = propJsonSchema.oneOf && Array.isArray(propJsonSchema.oneOf) && propJsonSchema.oneOf.some((schema) => typeof schema === "object" && schema !== null && schema.type === "null");
881
- const isNullable = "nullable" in propJsonSchema && propJsonSchema.nullable === true;
882
- if (hasAnyOfWithNull || hasOneOfWithNull || isNullable) properties[key] = propZodSchema.optional().default(null);
883
- else properties[key] = propZodSchema.nullable().optional().default(null);
884
- } else properties[key] = propZodSchema.optional().default(propJsonSchema.default);
885
- else properties[key] = required ? propZodSchema : propZodSchema.optional();
947
+ if (!required && propJsonSchema && typeof propJsonSchema === "object" && "default" in propJsonSchema) {
948
+ if (propJsonSchema.default === null) {
949
+ const hasAnyOfWithNull = propJsonSchema.anyOf && Array.isArray(propJsonSchema.anyOf) && propJsonSchema.anyOf.some((schema) => typeof schema === "object" && schema !== null && schema.type === "null");
950
+ const hasOneOfWithNull = propJsonSchema.oneOf && Array.isArray(propJsonSchema.oneOf) && propJsonSchema.oneOf.some((schema) => typeof schema === "object" && schema !== null && schema.type === "null");
951
+ const isNullable = "nullable" in propJsonSchema && propJsonSchema.nullable === true;
952
+ if (hasAnyOfWithNull || hasOneOfWithNull || isNullable) properties[key] = propZodSchema.optional().default(null);
953
+ else properties[key] = propZodSchema.nullable().optional().default(null);
954
+ } else properties[key] = propZodSchema.optional().default(propJsonSchema.default);
955
+ } else properties[key] = required ? propZodSchema : propZodSchema.optional();
886
956
  }
887
957
  return properties;
888
958
  }
@@ -922,4 +992,4 @@ const jsonSchemaToZodShape = (schema, options = {}) => {
922
992
  };
923
993
 
924
994
  //#endregion
925
- export { jsonSchemaToZod, jsonSchemaToZodShape };
995
+ export { InvalidPatternError, jsonSchemaToZod, jsonSchemaToZodShape };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@composio/json-schema-to-zod",
3
- "version": "0.3.2",
3
+ "version": "0.3.3-beta.0",
4
4
  "description": "",
5
5
  "main": "dist/index.mjs",
6
6
  "type": "module",
@@ -37,13 +37,13 @@
37
37
  "license": "ISC",
38
38
  "devDependencies": {
39
39
  "@types/json-schema": "^7.0.15",
40
- "@types/node": "^26.3.0",
40
+ "@types/node": "^26.4.1",
41
41
  "ajv": "^8.20.0",
42
42
  "fast-check": "^4.9.0",
43
- "tsdown": "^0.22.14",
43
+ "tsdown": "^0.23.0",
44
44
  "typescript": "^7.0.2",
45
45
  "vitest": "^4.1.11",
46
- "zod": "^4.4.3"
46
+ "zod": "^4.5.4"
47
47
  },
48
48
  "peerDependencies": {
49
49
  "zod": ">=3.25.76 <5"