@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/cli.js
CHANGED
|
@@ -5225,7 +5225,7 @@ function escapeDescription(str) {
|
|
|
5225
5225
|
return str.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n");
|
|
5226
5226
|
}
|
|
5227
5227
|
function escapePattern(str) {
|
|
5228
|
-
return str.replace(
|
|
5228
|
+
return str.replace(/(?<!\\)\//g, "\\/");
|
|
5229
5229
|
}
|
|
5230
5230
|
function escapeJSDoc(str) {
|
|
5231
5231
|
return str.replace(/\*\//g, "*\\/");
|
|
@@ -6078,12 +6078,6 @@ ${properties.join(",\n")}
|
|
|
6078
6078
|
|
|
6079
6079
|
// src/validators/string-validator.ts
|
|
6080
6080
|
init_cjs_shims();
|
|
6081
|
-
var PATTERN_CACHE = new LRUCache(1e3);
|
|
6082
|
-
function configurePatternCache(size) {
|
|
6083
|
-
if (size > 0 && size !== PATTERN_CACHE.capacity) {
|
|
6084
|
-
PATTERN_CACHE = new LRUCache(size);
|
|
6085
|
-
}
|
|
6086
|
-
}
|
|
6087
6081
|
var DEFAULT_FORMAT_MAP = {
|
|
6088
6082
|
uuid: "z.uuid()",
|
|
6089
6083
|
email: "z.email()",
|
|
@@ -6112,19 +6106,13 @@ var DEFAULT_FORMAT_MAP = {
|
|
|
6112
6106
|
"json-pointer": 'z.string().refine((val) => val === "" || /^(\\/([^~/]|~0|~1)+)+$/.test(val), { message: "Must be a valid JSON Pointer (RFC 6901)" })',
|
|
6113
6107
|
"relative-json-pointer": 'z.string().refine((val) => /^(0|[1-9]\\d*)(#|(\\/([^~/]|~0|~1)+)*)$/.test(val), { message: "Must be a valid relative JSON Pointer" })'
|
|
6114
6108
|
};
|
|
6115
|
-
|
|
6116
|
-
...DEFAULT_FORMAT_MAP,
|
|
6117
|
-
"date-time": "z.iso.datetime()"
|
|
6118
|
-
};
|
|
6119
|
-
function configureDateTimeFormat(pattern) {
|
|
6109
|
+
function buildDateTimeValidation(pattern) {
|
|
6120
6110
|
if (!pattern) {
|
|
6121
|
-
|
|
6122
|
-
return;
|
|
6111
|
+
return "z.iso.datetime()";
|
|
6123
6112
|
}
|
|
6124
6113
|
const patternStr = pattern instanceof RegExp ? pattern.source : pattern;
|
|
6125
6114
|
if (patternStr === "") {
|
|
6126
|
-
|
|
6127
|
-
return;
|
|
6115
|
+
return "z.iso.datetime()";
|
|
6128
6116
|
}
|
|
6129
6117
|
try {
|
|
6130
6118
|
new RegExp(patternStr);
|
|
@@ -6134,10 +6122,16 @@ function configureDateTimeFormat(pattern) {
|
|
|
6134
6122
|
);
|
|
6135
6123
|
}
|
|
6136
6124
|
const escapedPattern = escapePattern(patternStr);
|
|
6137
|
-
|
|
6125
|
+
return `z.string().regex(/${escapedPattern}/)`;
|
|
6138
6126
|
}
|
|
6139
|
-
function generateStringValidation(schema, useDescribe) {
|
|
6140
|
-
let validation
|
|
6127
|
+
function generateStringValidation(schema, useDescribe, context) {
|
|
6128
|
+
let validation;
|
|
6129
|
+
const format = schema.format || "";
|
|
6130
|
+
if (format === "date-time") {
|
|
6131
|
+
validation = context.dateTimeValidation;
|
|
6132
|
+
} else {
|
|
6133
|
+
validation = DEFAULT_FORMAT_MAP[format] || "z.string()";
|
|
6134
|
+
}
|
|
6141
6135
|
if (schema.minLength !== void 0) {
|
|
6142
6136
|
validation += `.min(${schema.minLength})`;
|
|
6143
6137
|
}
|
|
@@ -6145,10 +6139,10 @@ function generateStringValidation(schema, useDescribe) {
|
|
|
6145
6139
|
validation += `.max(${schema.maxLength})`;
|
|
6146
6140
|
}
|
|
6147
6141
|
if (schema.pattern) {
|
|
6148
|
-
let escapedPattern =
|
|
6142
|
+
let escapedPattern = context.patternCache.get(schema.pattern);
|
|
6149
6143
|
if (escapedPattern === void 0) {
|
|
6150
6144
|
escapedPattern = escapePattern(schema.pattern);
|
|
6151
|
-
|
|
6145
|
+
context.patternCache.set(schema.pattern, escapedPattern);
|
|
6152
6146
|
}
|
|
6153
6147
|
validation += `.regex(/${escapedPattern}/)`;
|
|
6154
6148
|
}
|
|
@@ -6178,10 +6172,10 @@ function generateStringValidation(schema, useDescribe) {
|
|
|
6178
6172
|
validation += `.max(${schema.maxLength})`;
|
|
6179
6173
|
}
|
|
6180
6174
|
if (schema.pattern) {
|
|
6181
|
-
let escapedPattern =
|
|
6175
|
+
let escapedPattern = context.patternCache.get(schema.pattern);
|
|
6182
6176
|
if (escapedPattern === void 0) {
|
|
6183
6177
|
escapedPattern = escapePattern(schema.pattern);
|
|
6184
|
-
|
|
6178
|
+
context.patternCache.set(schema.pattern, escapedPattern);
|
|
6185
6179
|
}
|
|
6186
6180
|
validation += `.regex(/${escapedPattern}/)`;
|
|
6187
6181
|
}
|
|
@@ -6574,7 +6568,10 @@ var _PropertyGenerator = class _PropertyGenerator {
|
|
|
6574
6568
|
const primaryType = getPrimaryType(schema);
|
|
6575
6569
|
switch (primaryType) {
|
|
6576
6570
|
case "string":
|
|
6577
|
-
validation = generateStringValidation(schema, this.context.useDescribe
|
|
6571
|
+
validation = generateStringValidation(schema, this.context.useDescribe, {
|
|
6572
|
+
dateTimeValidation: this.context.dateTimeValidation,
|
|
6573
|
+
patternCache: this.context.patternCache
|
|
6574
|
+
});
|
|
6578
6575
|
break;
|
|
6579
6576
|
case "number":
|
|
6580
6577
|
validation = generateNumberValidation(schema, false, this.context.useDescribe);
|
|
@@ -6878,7 +6875,7 @@ var OpenApiGenerator = class {
|
|
|
6878
6875
|
this.schemaUsageMap = /* @__PURE__ */ new Map();
|
|
6879
6876
|
this.needsZodImport = true;
|
|
6880
6877
|
this.filterStats = createFilterStatistics();
|
|
6881
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
6878
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
6882
6879
|
if (!options.input) {
|
|
6883
6880
|
throw new ConfigurationError("Input path is required", { providedOptions: options });
|
|
6884
6881
|
}
|
|
@@ -6904,12 +6901,8 @@ var OpenApiGenerator = class {
|
|
|
6904
6901
|
batchSize: (_g = options.batchSize) != null ? _g : 10,
|
|
6905
6902
|
customDateTimeFormatRegex: options.customDateTimeFormatRegex
|
|
6906
6903
|
};
|
|
6907
|
-
|
|
6908
|
-
|
|
6909
|
-
}
|
|
6910
|
-
if (this.options.customDateTimeFormatRegex) {
|
|
6911
|
-
configureDateTimeFormat(this.options.customDateTimeFormatRegex);
|
|
6912
|
-
}
|
|
6904
|
+
this.patternCache = new LRUCache((_h = this.options.cacheSize) != null ? _h : 1e3);
|
|
6905
|
+
this.dateTimeValidation = buildDateTimeValidation(this.options.customDateTimeFormatRegex);
|
|
6913
6906
|
try {
|
|
6914
6907
|
const fs = require("fs");
|
|
6915
6908
|
if (!fs.existsSync(this.options.input)) {
|
|
@@ -6972,13 +6965,15 @@ var OpenApiGenerator = class {
|
|
|
6972
6965
|
mode: this.requestOptions.mode,
|
|
6973
6966
|
includeDescriptions: this.requestOptions.includeDescriptions,
|
|
6974
6967
|
useDescribe: this.requestOptions.useDescribe,
|
|
6975
|
-
defaultNullable: (
|
|
6976
|
-
emptyObjectBehavior: (
|
|
6968
|
+
defaultNullable: (_i = this.options.defaultNullable) != null ? _i : false,
|
|
6969
|
+
emptyObjectBehavior: (_j = this.options.emptyObjectBehavior) != null ? _j : "loose",
|
|
6977
6970
|
namingOptions: {
|
|
6978
6971
|
prefix: this.options.prefix,
|
|
6979
6972
|
suffix: this.options.suffix
|
|
6980
6973
|
},
|
|
6981
|
-
stripSchemaPrefix: this.options.stripSchemaPrefix
|
|
6974
|
+
stripSchemaPrefix: this.options.stripSchemaPrefix,
|
|
6975
|
+
dateTimeValidation: this.dateTimeValidation,
|
|
6976
|
+
patternCache: this.patternCache
|
|
6982
6977
|
});
|
|
6983
6978
|
}
|
|
6984
6979
|
/**
|
|
@@ -7359,7 +7354,9 @@ ${typeCode}`;
|
|
|
7359
7354
|
prefix: this.options.prefix,
|
|
7360
7355
|
suffix: this.options.suffix
|
|
7361
7356
|
},
|
|
7362
|
-
stripSchemaPrefix: this.options.stripSchemaPrefix
|
|
7357
|
+
stripSchemaPrefix: this.options.stripSchemaPrefix,
|
|
7358
|
+
dateTimeValidation: this.dateTimeValidation,
|
|
7359
|
+
patternCache: this.patternCache
|
|
7363
7360
|
});
|
|
7364
7361
|
const zodSchema = this.propertyGenerator.generatePropertySchema(schema, name, true);
|
|
7365
7362
|
const zodSchemaCode = `${jsdoc}export const ${schemaName} = ${zodSchema};`;
|