@cerios/openapi-to-zod 1.3.2 → 1.4.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.mjs CHANGED
@@ -155,7 +155,7 @@ function escapeDescription(str) {
155
155
  return str.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n");
156
156
  }
157
157
  function escapePattern(str) {
158
- return str.replace(/\//g, "\\/");
158
+ return str.replace(/(?<!\\)\//g, "\\/");
159
159
  }
160
160
  function escapeJSDoc(str) {
161
161
  return str.replace(/\*\//g, "*\\/");
@@ -995,12 +995,6 @@ ${properties.join(",\n")}
995
995
  }
996
996
 
997
997
  // src/validators/string-validator.ts
998
- var PATTERN_CACHE = new LRUCache(1e3);
999
- function configurePatternCache(size) {
1000
- if (size > 0 && size !== PATTERN_CACHE.capacity) {
1001
- PATTERN_CACHE = new LRUCache(size);
1002
- }
1003
- }
1004
998
  var DEFAULT_FORMAT_MAP = {
1005
999
  uuid: "z.uuid()",
1006
1000
  email: "z.email()",
@@ -1029,19 +1023,13 @@ var DEFAULT_FORMAT_MAP = {
1029
1023
  "json-pointer": 'z.string().refine((val) => val === "" || /^(\\/([^~/]|~0|~1)+)+$/.test(val), { message: "Must be a valid JSON Pointer (RFC 6901)" })',
1030
1024
  "relative-json-pointer": 'z.string().refine((val) => /^(0|[1-9]\\d*)(#|(\\/([^~/]|~0|~1)+)*)$/.test(val), { message: "Must be a valid relative JSON Pointer" })'
1031
1025
  };
1032
- var FORMAT_MAP = {
1033
- ...DEFAULT_FORMAT_MAP,
1034
- "date-time": "z.iso.datetime()"
1035
- };
1036
- function configureDateTimeFormat(pattern) {
1026
+ function buildDateTimeValidation(pattern) {
1037
1027
  if (!pattern) {
1038
- FORMAT_MAP["date-time"] = "z.iso.datetime()";
1039
- return;
1028
+ return "z.iso.datetime()";
1040
1029
  }
1041
1030
  const patternStr = pattern instanceof RegExp ? pattern.source : pattern;
1042
1031
  if (patternStr === "") {
1043
- FORMAT_MAP["date-time"] = "z.iso.datetime()";
1044
- return;
1032
+ return "z.iso.datetime()";
1045
1033
  }
1046
1034
  try {
1047
1035
  new RegExp(patternStr);
@@ -1051,10 +1039,16 @@ function configureDateTimeFormat(pattern) {
1051
1039
  );
1052
1040
  }
1053
1041
  const escapedPattern = escapePattern(patternStr);
1054
- FORMAT_MAP["date-time"] = `z.string().regex(/${escapedPattern}/)`;
1042
+ return `z.string().regex(/${escapedPattern}/)`;
1055
1043
  }
1056
- function generateStringValidation(schema, useDescribe) {
1057
- let validation = FORMAT_MAP[schema.format || ""] || "z.string()";
1044
+ function generateStringValidation(schema, useDescribe, context) {
1045
+ let validation;
1046
+ const format = schema.format || "";
1047
+ if (format === "date-time") {
1048
+ validation = context.dateTimeValidation;
1049
+ } else {
1050
+ validation = DEFAULT_FORMAT_MAP[format] || "z.string()";
1051
+ }
1058
1052
  if (schema.minLength !== void 0) {
1059
1053
  validation += `.min(${schema.minLength})`;
1060
1054
  }
@@ -1062,10 +1056,10 @@ function generateStringValidation(schema, useDescribe) {
1062
1056
  validation += `.max(${schema.maxLength})`;
1063
1057
  }
1064
1058
  if (schema.pattern) {
1065
- let escapedPattern = PATTERN_CACHE.get(schema.pattern);
1059
+ let escapedPattern = context.patternCache.get(schema.pattern);
1066
1060
  if (escapedPattern === void 0) {
1067
1061
  escapedPattern = escapePattern(schema.pattern);
1068
- PATTERN_CACHE.set(schema.pattern, escapedPattern);
1062
+ context.patternCache.set(schema.pattern, escapedPattern);
1069
1063
  }
1070
1064
  validation += `.regex(/${escapedPattern}/)`;
1071
1065
  }
@@ -1095,10 +1089,10 @@ function generateStringValidation(schema, useDescribe) {
1095
1089
  validation += `.max(${schema.maxLength})`;
1096
1090
  }
1097
1091
  if (schema.pattern) {
1098
- let escapedPattern = PATTERN_CACHE.get(schema.pattern);
1092
+ let escapedPattern = context.patternCache.get(schema.pattern);
1099
1093
  if (escapedPattern === void 0) {
1100
1094
  escapedPattern = escapePattern(schema.pattern);
1101
- PATTERN_CACHE.set(schema.pattern, escapedPattern);
1095
+ context.patternCache.set(schema.pattern, escapedPattern);
1102
1096
  }
1103
1097
  validation += `.regex(/${escapedPattern}/)`;
1104
1098
  }
@@ -1491,7 +1485,10 @@ var _PropertyGenerator = class _PropertyGenerator {
1491
1485
  const primaryType = getPrimaryType(schema);
1492
1486
  switch (primaryType) {
1493
1487
  case "string":
1494
- validation = generateStringValidation(schema, this.context.useDescribe);
1488
+ validation = generateStringValidation(schema, this.context.useDescribe, {
1489
+ dateTimeValidation: this.context.dateTimeValidation,
1490
+ patternCache: this.context.patternCache
1491
+ });
1495
1492
  break;
1496
1493
  case "number":
1497
1494
  validation = generateNumberValidation(schema, false, this.context.useDescribe);
@@ -1793,7 +1790,7 @@ var OpenApiGenerator = class {
1793
1790
  this.schemaUsageMap = /* @__PURE__ */ new Map();
1794
1791
  this.needsZodImport = true;
1795
1792
  this.filterStats = createFilterStatistics();
1796
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
1793
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
1797
1794
  if (!options.input) {
1798
1795
  throw new ConfigurationError("Input path is required", { providedOptions: options });
1799
1796
  }
@@ -1819,12 +1816,8 @@ var OpenApiGenerator = class {
1819
1816
  batchSize: (_g = options.batchSize) != null ? _g : 10,
1820
1817
  customDateTimeFormatRegex: options.customDateTimeFormatRegex
1821
1818
  };
1822
- if (this.options.cacheSize) {
1823
- configurePatternCache(this.options.cacheSize);
1824
- }
1825
- if (this.options.customDateTimeFormatRegex) {
1826
- configureDateTimeFormat(this.options.customDateTimeFormatRegex);
1827
- }
1819
+ this.patternCache = new LRUCache((_h = this.options.cacheSize) != null ? _h : 1e3);
1820
+ this.dateTimeValidation = buildDateTimeValidation(this.options.customDateTimeFormatRegex);
1828
1821
  try {
1829
1822
  const fs = __require("fs");
1830
1823
  if (!fs.existsSync(this.options.input)) {
@@ -1887,13 +1880,15 @@ var OpenApiGenerator = class {
1887
1880
  mode: this.requestOptions.mode,
1888
1881
  includeDescriptions: this.requestOptions.includeDescriptions,
1889
1882
  useDescribe: this.requestOptions.useDescribe,
1890
- defaultNullable: (_h = this.options.defaultNullable) != null ? _h : false,
1891
- emptyObjectBehavior: (_i = this.options.emptyObjectBehavior) != null ? _i : "loose",
1883
+ defaultNullable: (_i = this.options.defaultNullable) != null ? _i : false,
1884
+ emptyObjectBehavior: (_j = this.options.emptyObjectBehavior) != null ? _j : "loose",
1892
1885
  namingOptions: {
1893
1886
  prefix: this.options.prefix,
1894
1887
  suffix: this.options.suffix
1895
1888
  },
1896
- stripSchemaPrefix: this.options.stripSchemaPrefix
1889
+ stripSchemaPrefix: this.options.stripSchemaPrefix,
1890
+ dateTimeValidation: this.dateTimeValidation,
1891
+ patternCache: this.patternCache
1897
1892
  });
1898
1893
  }
1899
1894
  /**
@@ -2274,7 +2269,9 @@ ${typeCode}`;
2274
2269
  prefix: this.options.prefix,
2275
2270
  suffix: this.options.suffix
2276
2271
  },
2277
- stripSchemaPrefix: this.options.stripSchemaPrefix
2272
+ stripSchemaPrefix: this.options.stripSchemaPrefix,
2273
+ dateTimeValidation: this.dateTimeValidation,
2274
+ patternCache: this.patternCache
2278
2275
  });
2279
2276
  const zodSchema = this.propertyGenerator.generatePropertySchema(schema, name, true);
2280
2277
  const zodSchemaCode = `${jsdoc}export const ${schemaName} = ${zodSchema};`;