@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/cli.js +32 -35
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +34 -37
- package/dist/cli.mjs.map +1 -1
- package/dist/index.d.mts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +32 -35
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +32 -35
- package/dist/index.mjs.map +1 -1
- package/dist/internal.d.mts +159 -52
- package/dist/internal.d.ts +159 -52
- package/dist/internal.js +1508 -242
- package/dist/internal.js.map +1 -1
- package/dist/internal.mjs +1506 -240
- package/dist/internal.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -65,6 +65,10 @@ declare class OpenApiGenerator {
|
|
|
65
65
|
private responseOptions;
|
|
66
66
|
private needsZodImport;
|
|
67
67
|
private filterStats;
|
|
68
|
+
/** Instance-level pattern cache for parallel-safe execution */
|
|
69
|
+
private patternCache;
|
|
70
|
+
/** Instance-level date-time validation string for parallel-safe execution */
|
|
71
|
+
private dateTimeValidation;
|
|
68
72
|
constructor(options: OpenApiGeneratorOptions);
|
|
69
73
|
/**
|
|
70
74
|
* Generate schemas as a string (without writing to file)
|
package/dist/index.d.ts
CHANGED
|
@@ -65,6 +65,10 @@ declare class OpenApiGenerator {
|
|
|
65
65
|
private responseOptions;
|
|
66
66
|
private needsZodImport;
|
|
67
67
|
private filterStats;
|
|
68
|
+
/** Instance-level pattern cache for parallel-safe execution */
|
|
69
|
+
private patternCache;
|
|
70
|
+
/** Instance-level date-time validation string for parallel-safe execution */
|
|
71
|
+
private dateTimeValidation;
|
|
68
72
|
constructor(options: OpenApiGeneratorOptions);
|
|
69
73
|
/**
|
|
70
74
|
* Generate schemas as a string (without writing to file)
|
package/dist/index.js
CHANGED
|
@@ -182,7 +182,7 @@ function escapeDescription(str) {
|
|
|
182
182
|
return str.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n");
|
|
183
183
|
}
|
|
184
184
|
function escapePattern(str) {
|
|
185
|
-
return str.replace(
|
|
185
|
+
return str.replace(/(?<!\\)\//g, "\\/");
|
|
186
186
|
}
|
|
187
187
|
function escapeJSDoc(str) {
|
|
188
188
|
return str.replace(/\*\//g, "*\\/");
|
|
@@ -1022,12 +1022,6 @@ ${properties.join(",\n")}
|
|
|
1022
1022
|
}
|
|
1023
1023
|
|
|
1024
1024
|
// src/validators/string-validator.ts
|
|
1025
|
-
var PATTERN_CACHE = new LRUCache(1e3);
|
|
1026
|
-
function configurePatternCache(size) {
|
|
1027
|
-
if (size > 0 && size !== PATTERN_CACHE.capacity) {
|
|
1028
|
-
PATTERN_CACHE = new LRUCache(size);
|
|
1029
|
-
}
|
|
1030
|
-
}
|
|
1031
1025
|
var DEFAULT_FORMAT_MAP = {
|
|
1032
1026
|
uuid: "z.uuid()",
|
|
1033
1027
|
email: "z.email()",
|
|
@@ -1056,19 +1050,13 @@ var DEFAULT_FORMAT_MAP = {
|
|
|
1056
1050
|
"json-pointer": 'z.string().refine((val) => val === "" || /^(\\/([^~/]|~0|~1)+)+$/.test(val), { message: "Must be a valid JSON Pointer (RFC 6901)" })',
|
|
1057
1051
|
"relative-json-pointer": 'z.string().refine((val) => /^(0|[1-9]\\d*)(#|(\\/([^~/]|~0|~1)+)*)$/.test(val), { message: "Must be a valid relative JSON Pointer" })'
|
|
1058
1052
|
};
|
|
1059
|
-
|
|
1060
|
-
...DEFAULT_FORMAT_MAP,
|
|
1061
|
-
"date-time": "z.iso.datetime()"
|
|
1062
|
-
};
|
|
1063
|
-
function configureDateTimeFormat(pattern) {
|
|
1053
|
+
function buildDateTimeValidation(pattern) {
|
|
1064
1054
|
if (!pattern) {
|
|
1065
|
-
|
|
1066
|
-
return;
|
|
1055
|
+
return "z.iso.datetime()";
|
|
1067
1056
|
}
|
|
1068
1057
|
const patternStr = pattern instanceof RegExp ? pattern.source : pattern;
|
|
1069
1058
|
if (patternStr === "") {
|
|
1070
|
-
|
|
1071
|
-
return;
|
|
1059
|
+
return "z.iso.datetime()";
|
|
1072
1060
|
}
|
|
1073
1061
|
try {
|
|
1074
1062
|
new RegExp(patternStr);
|
|
@@ -1078,10 +1066,16 @@ function configureDateTimeFormat(pattern) {
|
|
|
1078
1066
|
);
|
|
1079
1067
|
}
|
|
1080
1068
|
const escapedPattern = escapePattern(patternStr);
|
|
1081
|
-
|
|
1069
|
+
return `z.string().regex(/${escapedPattern}/)`;
|
|
1082
1070
|
}
|
|
1083
|
-
function generateStringValidation(schema, useDescribe) {
|
|
1084
|
-
let validation
|
|
1071
|
+
function generateStringValidation(schema, useDescribe, context) {
|
|
1072
|
+
let validation;
|
|
1073
|
+
const format = schema.format || "";
|
|
1074
|
+
if (format === "date-time") {
|
|
1075
|
+
validation = context.dateTimeValidation;
|
|
1076
|
+
} else {
|
|
1077
|
+
validation = DEFAULT_FORMAT_MAP[format] || "z.string()";
|
|
1078
|
+
}
|
|
1085
1079
|
if (schema.minLength !== void 0) {
|
|
1086
1080
|
validation += `.min(${schema.minLength})`;
|
|
1087
1081
|
}
|
|
@@ -1089,10 +1083,10 @@ function generateStringValidation(schema, useDescribe) {
|
|
|
1089
1083
|
validation += `.max(${schema.maxLength})`;
|
|
1090
1084
|
}
|
|
1091
1085
|
if (schema.pattern) {
|
|
1092
|
-
let escapedPattern =
|
|
1086
|
+
let escapedPattern = context.patternCache.get(schema.pattern);
|
|
1093
1087
|
if (escapedPattern === void 0) {
|
|
1094
1088
|
escapedPattern = escapePattern(schema.pattern);
|
|
1095
|
-
|
|
1089
|
+
context.patternCache.set(schema.pattern, escapedPattern);
|
|
1096
1090
|
}
|
|
1097
1091
|
validation += `.regex(/${escapedPattern}/)`;
|
|
1098
1092
|
}
|
|
@@ -1122,10 +1116,10 @@ function generateStringValidation(schema, useDescribe) {
|
|
|
1122
1116
|
validation += `.max(${schema.maxLength})`;
|
|
1123
1117
|
}
|
|
1124
1118
|
if (schema.pattern) {
|
|
1125
|
-
let escapedPattern =
|
|
1119
|
+
let escapedPattern = context.patternCache.get(schema.pattern);
|
|
1126
1120
|
if (escapedPattern === void 0) {
|
|
1127
1121
|
escapedPattern = escapePattern(schema.pattern);
|
|
1128
|
-
|
|
1122
|
+
context.patternCache.set(schema.pattern, escapedPattern);
|
|
1129
1123
|
}
|
|
1130
1124
|
validation += `.regex(/${escapedPattern}/)`;
|
|
1131
1125
|
}
|
|
@@ -1518,7 +1512,10 @@ var _PropertyGenerator = class _PropertyGenerator {
|
|
|
1518
1512
|
const primaryType = getPrimaryType(schema);
|
|
1519
1513
|
switch (primaryType) {
|
|
1520
1514
|
case "string":
|
|
1521
|
-
validation = generateStringValidation(schema, this.context.useDescribe
|
|
1515
|
+
validation = generateStringValidation(schema, this.context.useDescribe, {
|
|
1516
|
+
dateTimeValidation: this.context.dateTimeValidation,
|
|
1517
|
+
patternCache: this.context.patternCache
|
|
1518
|
+
});
|
|
1522
1519
|
break;
|
|
1523
1520
|
case "number":
|
|
1524
1521
|
validation = generateNumberValidation(schema, false, this.context.useDescribe);
|
|
@@ -1820,7 +1817,7 @@ var OpenApiGenerator = class {
|
|
|
1820
1817
|
this.schemaUsageMap = /* @__PURE__ */ new Map();
|
|
1821
1818
|
this.needsZodImport = true;
|
|
1822
1819
|
this.filterStats = createFilterStatistics();
|
|
1823
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
1820
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
1824
1821
|
if (!options.input) {
|
|
1825
1822
|
throw new ConfigurationError("Input path is required", { providedOptions: options });
|
|
1826
1823
|
}
|
|
@@ -1846,12 +1843,8 @@ var OpenApiGenerator = class {
|
|
|
1846
1843
|
batchSize: (_g = options.batchSize) != null ? _g : 10,
|
|
1847
1844
|
customDateTimeFormatRegex: options.customDateTimeFormatRegex
|
|
1848
1845
|
};
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
}
|
|
1852
|
-
if (this.options.customDateTimeFormatRegex) {
|
|
1853
|
-
configureDateTimeFormat(this.options.customDateTimeFormatRegex);
|
|
1854
|
-
}
|
|
1846
|
+
this.patternCache = new LRUCache((_h = this.options.cacheSize) != null ? _h : 1e3);
|
|
1847
|
+
this.dateTimeValidation = buildDateTimeValidation(this.options.customDateTimeFormatRegex);
|
|
1855
1848
|
try {
|
|
1856
1849
|
const fs = require("fs");
|
|
1857
1850
|
if (!fs.existsSync(this.options.input)) {
|
|
@@ -1914,13 +1907,15 @@ var OpenApiGenerator = class {
|
|
|
1914
1907
|
mode: this.requestOptions.mode,
|
|
1915
1908
|
includeDescriptions: this.requestOptions.includeDescriptions,
|
|
1916
1909
|
useDescribe: this.requestOptions.useDescribe,
|
|
1917
|
-
defaultNullable: (
|
|
1918
|
-
emptyObjectBehavior: (
|
|
1910
|
+
defaultNullable: (_i = this.options.defaultNullable) != null ? _i : false,
|
|
1911
|
+
emptyObjectBehavior: (_j = this.options.emptyObjectBehavior) != null ? _j : "loose",
|
|
1919
1912
|
namingOptions: {
|
|
1920
1913
|
prefix: this.options.prefix,
|
|
1921
1914
|
suffix: this.options.suffix
|
|
1922
1915
|
},
|
|
1923
|
-
stripSchemaPrefix: this.options.stripSchemaPrefix
|
|
1916
|
+
stripSchemaPrefix: this.options.stripSchemaPrefix,
|
|
1917
|
+
dateTimeValidation: this.dateTimeValidation,
|
|
1918
|
+
patternCache: this.patternCache
|
|
1924
1919
|
});
|
|
1925
1920
|
}
|
|
1926
1921
|
/**
|
|
@@ -2301,7 +2296,9 @@ ${typeCode}`;
|
|
|
2301
2296
|
prefix: this.options.prefix,
|
|
2302
2297
|
suffix: this.options.suffix
|
|
2303
2298
|
},
|
|
2304
|
-
stripSchemaPrefix: this.options.stripSchemaPrefix
|
|
2299
|
+
stripSchemaPrefix: this.options.stripSchemaPrefix,
|
|
2300
|
+
dateTimeValidation: this.dateTimeValidation,
|
|
2301
|
+
patternCache: this.patternCache
|
|
2305
2302
|
});
|
|
2306
2303
|
const zodSchema = this.propertyGenerator.generatePropertySchema(schema, name, true);
|
|
2307
2304
|
const zodSchemaCode = `${jsdoc}export const ${schemaName} = ${zodSchema};`;
|