@cerios/openapi-to-zod 1.1.0 → 1.1.1

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
@@ -1,5 +1,5 @@
1
- import { O as OpenApiGeneratorOptions } from './types-B7ePTDjr.mjs';
2
- export { C as CommonSchemaOptions, a as ConfigFile, E as ExecutionMode, b as OpenAPISchema, c as OpenAPISpec, d as OperationFilters, R as RequestOptions, e as ResponseOptions, f as defineConfig } from './types-B7ePTDjr.mjs';
1
+ import { O as OpenApiGeneratorOptions } from './types-CI48CjiU.mjs';
2
+ export { C as CommonSchemaOptions, a as ConfigFile, E as ExecutionMode, b as OpenAPISchema, c as OpenAPISpec, d as OperationFilters, R as RequestOptions, e as ResponseOptions, f as defineConfig } from './types-CI48CjiU.mjs';
3
3
 
4
4
  /**
5
5
  * Custom error classes for better error handling and debugging
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { O as OpenApiGeneratorOptions } from './types-B7ePTDjr.js';
2
- export { C as CommonSchemaOptions, a as ConfigFile, E as ExecutionMode, b as OpenAPISchema, c as OpenAPISpec, d as OperationFilters, R as RequestOptions, e as ResponseOptions, f as defineConfig } from './types-B7ePTDjr.js';
1
+ import { O as OpenApiGeneratorOptions } from './types-CI48CjiU.js';
2
+ export { C as CommonSchemaOptions, a as ConfigFile, E as ExecutionMode, b as OpenAPISchema, c as OpenAPISpec, d as OperationFilters, R as RequestOptions, e as ResponseOptions, f as defineConfig } from './types-CI48CjiU.js';
3
3
 
4
4
  /**
5
5
  * Custom error classes for better error handling and debugging
package/dist/index.js CHANGED
@@ -94,7 +94,7 @@ var ConfigurationError = class extends GeneratorError {
94
94
  // src/openapi-generator.ts
95
95
  var import_node_fs = require("fs");
96
96
  var import_node_path = require("path");
97
- var import_minimatch2 = require("minimatch");
97
+ var import_minimatch3 = require("minimatch");
98
98
  var import_yaml = require("yaml");
99
99
 
100
100
  // src/utils/name-utils.ts
@@ -153,8 +153,26 @@ function resolveRef(ref) {
153
153
  function generateEnum(name, values, options) {
154
154
  const schemaName = `${toCamelCase(name, options)}Schema`;
155
155
  const typeName = toPascalCase(name);
156
- const enumValues = values.map((v) => `"${v}"`).join(", ");
157
- const schemaCode = `export const ${schemaName} = z.enum([${enumValues}]);`;
156
+ const allBooleans = values.every((v) => typeof v === "boolean");
157
+ if (allBooleans) {
158
+ const schemaCode2 = `export const ${schemaName} = z.boolean();`;
159
+ const typeCode2 = `export type ${typeName} = z.infer<typeof ${schemaName}>;`;
160
+ return { schemaCode: schemaCode2, typeCode: typeCode2 };
161
+ }
162
+ const allStrings = values.every((v) => typeof v === "string");
163
+ if (allStrings) {
164
+ const enumValues = values.map((v) => `"${v}"`).join(", ");
165
+ const schemaCode2 = `export const ${schemaName} = z.enum([${enumValues}]);`;
166
+ const typeCode2 = `export type ${typeName} = z.infer<typeof ${schemaName}>;`;
167
+ return { schemaCode: schemaCode2, typeCode: typeCode2 };
168
+ }
169
+ const literalValues = values.map((v) => {
170
+ if (typeof v === "string") {
171
+ return `z.literal("${v}")`;
172
+ }
173
+ return `z.literal(${v})`;
174
+ }).join(", ");
175
+ const schemaCode = `export const ${schemaName} = z.union([${literalValues}]);`;
158
176
  const typeCode = `export type ${typeName} = z.infer<typeof ${schemaName}>;`;
159
177
  return { schemaCode, typeCode };
160
178
  }
@@ -164,7 +182,7 @@ function escapeDescription(str) {
164
182
  return str.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n");
165
183
  }
166
184
  function escapePattern(str) {
167
- return str.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
185
+ return str.replace(/\//g, "\\/");
168
186
  }
169
187
  function escapeJSDoc(str) {
170
188
  return str.replace(/\*\//g, "*\\/");
@@ -289,61 +307,36 @@ var LRUCache = class {
289
307
  };
290
308
 
291
309
  // src/utils/pattern-utils.ts
292
- function isRegexPattern(pattern) {
293
- if (pattern.startsWith("^") || pattern.endsWith("$")) {
294
- return true;
295
- }
296
- if (/\\[dDwWsS]/.test(pattern)) {
297
- return true;
298
- }
299
- if (/\.\*|\.\+/.test(pattern)) {
300
- return true;
301
- }
302
- if (/[[\]()]/.test(pattern)) {
303
- return true;
304
- }
305
- if (/[^/][+?*]\{/.test(pattern)) {
310
+ var import_minimatch = require("minimatch");
311
+ function isValidGlobPattern(pattern) {
312
+ try {
313
+ new import_minimatch.minimatch.Minimatch(pattern);
306
314
  return true;
315
+ } catch {
316
+ return false;
307
317
  }
308
- return false;
309
318
  }
310
- function patternToRegex(pattern) {
311
- if (pattern instanceof RegExp) {
312
- return pattern;
313
- }
314
- if (isRegexPattern(pattern)) {
315
- try {
316
- return new RegExp(pattern);
317
- } catch (error) {
318
- console.warn(`\u26A0\uFE0F Invalid regex pattern "${pattern}": ${error instanceof Error ? error.message : String(error)}`);
319
- return null;
320
- }
321
- }
322
- return null;
319
+ function isGlobPattern(pattern) {
320
+ return /[*?[\]{}!]/.test(pattern);
323
321
  }
324
322
  function stripPrefix(input, pattern, ensureLeadingChar) {
325
323
  if (!pattern) {
326
324
  return input;
327
325
  }
328
- const regex = patternToRegex(pattern);
329
- if (regex) {
330
- const match = input.match(regex);
331
- if (match && match.index === 0) {
332
- const stripped = input.substring(match[0].length);
333
- if (ensureLeadingChar) {
334
- if (stripped === "") {
335
- return ensureLeadingChar;
336
- }
337
- if (!stripped.startsWith(ensureLeadingChar)) {
338
- return `${ensureLeadingChar}${stripped}`;
339
- }
326
+ if (isGlobPattern(pattern) && !isValidGlobPattern(pattern)) {
327
+ console.warn(`\u26A0\uFE0F Invalid glob pattern "${pattern}": Pattern is malformed`);
328
+ return input;
329
+ }
330
+ if (isGlobPattern(pattern)) {
331
+ let longestMatch = -1;
332
+ for (let i = 1; i <= input.length; i++) {
333
+ const testPrefix = input.substring(0, i);
334
+ if ((0, import_minimatch.minimatch)(testPrefix, pattern)) {
335
+ longestMatch = i;
340
336
  }
341
- return stripped;
342
337
  }
343
- } else {
344
- const stringPattern = pattern;
345
- if (input.startsWith(stringPattern)) {
346
- const stripped = input.substring(stringPattern.length);
338
+ if (longestMatch > 0) {
339
+ const stripped = input.substring(longestMatch);
347
340
  if (ensureLeadingChar) {
348
341
  if (stripped === "") {
349
342
  return ensureLeadingChar;
@@ -352,8 +345,21 @@ function stripPrefix(input, pattern, ensureLeadingChar) {
352
345
  return `${ensureLeadingChar}${stripped}`;
353
346
  }
354
347
  }
355
- return stripped;
348
+ return stripped === "" && !ensureLeadingChar ? input : stripped;
349
+ }
350
+ return input;
351
+ }
352
+ if (input.startsWith(pattern)) {
353
+ const stripped = input.substring(pattern.length);
354
+ if (ensureLeadingChar) {
355
+ if (stripped === "") {
356
+ return ensureLeadingChar;
357
+ }
358
+ if (!stripped.startsWith(ensureLeadingChar)) {
359
+ return `${ensureLeadingChar}${stripped}`;
360
+ }
356
361
  }
362
+ return stripped;
357
363
  }
358
364
  return input;
359
365
  }
@@ -1228,9 +1234,25 @@ var _PropertyGenerator = class _PropertyGenerator {
1228
1234
  return wrapNullable(zodLiteral, nullable);
1229
1235
  }
1230
1236
  if (schema.enum) {
1231
- const enumValues = schema.enum.map((v) => `"${v}"`).join(", ");
1232
- const zodEnum = `z.enum([${enumValues}])`;
1233
- return wrapNullable(zodEnum, nullable);
1237
+ const allBooleans = schema.enum.every((v) => typeof v === "boolean");
1238
+ if (allBooleans) {
1239
+ const zodBoolean = "z.boolean()";
1240
+ return wrapNullable(zodBoolean, nullable);
1241
+ }
1242
+ const allStrings = schema.enum.every((v) => typeof v === "string");
1243
+ if (allStrings) {
1244
+ const enumValues = schema.enum.map((v) => `"${v}"`).join(", ");
1245
+ const zodEnum = `z.enum([${enumValues}])`;
1246
+ return wrapNullable(zodEnum, nullable);
1247
+ }
1248
+ const literalValues = schema.enum.map((v) => {
1249
+ if (typeof v === "string") {
1250
+ return `z.literal("${v}")`;
1251
+ }
1252
+ return `z.literal(${v})`;
1253
+ }).join(", ");
1254
+ const zodUnion = `z.union([${literalValues}])`;
1255
+ return wrapNullable(zodUnion, nullable);
1234
1256
  }
1235
1257
  if (schema.allOf) {
1236
1258
  let composition = generateAllOf(
@@ -1361,7 +1383,7 @@ _PropertyGenerator.INCLUSION_RULES = {
1361
1383
  var PropertyGenerator = _PropertyGenerator;
1362
1384
 
1363
1385
  // src/utils/operation-filters.ts
1364
- var import_minimatch = require("minimatch");
1386
+ var import_minimatch2 = require("minimatch");
1365
1387
  function createFilterStatistics() {
1366
1388
  return {
1367
1389
  totalOperations: 0,
@@ -1380,7 +1402,7 @@ function matchesAnyPattern(value, patterns) {
1380
1402
  if (!value) {
1381
1403
  return false;
1382
1404
  }
1383
- return patterns.some((pattern) => (0, import_minimatch.minimatch)(value, pattern));
1405
+ return patterns.some((pattern) => (0, import_minimatch2.minimatch)(value, pattern));
1384
1406
  }
1385
1407
  function containsAny(arr, values) {
1386
1408
  if (!values || values.length === 0) {
@@ -1607,6 +1629,9 @@ var OpenApiGenerator = class {
1607
1629
  throw new SpecValidationError("No schemas found in OpenAPI spec", { filePath: this.options.input });
1608
1630
  }
1609
1631
  for (const [name, schema] of Object.entries(this.spec.components.schemas)) {
1632
+ if (this.options.operationFilters && this.schemaUsageMap.size > 0 && !this.schemaUsageMap.has(name)) {
1633
+ continue;
1634
+ }
1610
1635
  this.generateComponentSchema(name, schema);
1611
1636
  }
1612
1637
  this.generateQueryParameterSchemas();
@@ -2095,7 +2120,7 @@ ${propsCode}
2095
2120
  const headerLower = headerName.toLowerCase();
2096
2121
  return ignorePatterns.some((pattern) => {
2097
2122
  const patternLower = pattern.toLowerCase();
2098
- return (0, import_minimatch2.minimatch)(headerLower, patternLower);
2123
+ return (0, import_minimatch3.minimatch)(headerLower, patternLower);
2099
2124
  });
2100
2125
  }
2101
2126
  /**
@@ -2183,8 +2208,22 @@ ${propsCode}
2183
2208
  return `${schemaName}Schema`;
2184
2209
  }
2185
2210
  if (schema.enum) {
2186
- const enumValues = schema.enum.map((v) => typeof v === "string" ? `"${v}"` : v).join(", ");
2187
- return `z.enum([${enumValues}])`;
2211
+ const allBooleans = schema.enum.every((v) => typeof v === "boolean");
2212
+ if (allBooleans) {
2213
+ return "z.boolean()";
2214
+ }
2215
+ const allStrings = schema.enum.every((v) => typeof v === "string");
2216
+ if (allStrings) {
2217
+ const enumValues = schema.enum.map((v) => `"${v}"`).join(", ");
2218
+ return `z.enum([${enumValues}])`;
2219
+ }
2220
+ const literalValues = schema.enum.map((v) => {
2221
+ if (typeof v === "string") {
2222
+ return `z.literal("${v}")`;
2223
+ }
2224
+ return `z.literal(${v})`;
2225
+ }).join(", ");
2226
+ return `z.union([${literalValues}])`;
2188
2227
  }
2189
2228
  const type = schema.type;
2190
2229
  if (type === "string") {