@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/cli.mjs CHANGED
@@ -5197,8 +5197,26 @@ var init_name_utils = __esm({
5197
5197
  function generateEnum(name, values, options) {
5198
5198
  const schemaName = `${toCamelCase(name, options)}Schema`;
5199
5199
  const typeName = toPascalCase(name);
5200
- const enumValues = values.map((v) => `"${v}"`).join(", ");
5201
- const schemaCode = `export const ${schemaName} = z.enum([${enumValues}]);`;
5200
+ const allBooleans = values.every((v) => typeof v === "boolean");
5201
+ if (allBooleans) {
5202
+ const schemaCode2 = `export const ${schemaName} = z.boolean();`;
5203
+ const typeCode2 = `export type ${typeName} = z.infer<typeof ${schemaName}>;`;
5204
+ return { schemaCode: schemaCode2, typeCode: typeCode2 };
5205
+ }
5206
+ const allStrings = values.every((v) => typeof v === "string");
5207
+ if (allStrings) {
5208
+ const enumValues = values.map((v) => `"${v}"`).join(", ");
5209
+ const schemaCode2 = `export const ${schemaName} = z.enum([${enumValues}]);`;
5210
+ const typeCode2 = `export type ${typeName} = z.infer<typeof ${schemaName}>;`;
5211
+ return { schemaCode: schemaCode2, typeCode: typeCode2 };
5212
+ }
5213
+ const literalValues = values.map((v) => {
5214
+ if (typeof v === "string") {
5215
+ return `z.literal("${v}")`;
5216
+ }
5217
+ return `z.literal(${v})`;
5218
+ }).join(", ");
5219
+ const schemaCode = `export const ${schemaName} = z.union([${literalValues}]);`;
5202
5220
  const typeCode = `export type ${typeName} = z.infer<typeof ${schemaName}>;`;
5203
5221
  return { schemaCode, typeCode };
5204
5222
  }
@@ -5215,7 +5233,7 @@ function escapeDescription(str) {
5215
5233
  return str.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n");
5216
5234
  }
5217
5235
  function escapePattern(str) {
5218
- return str.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
5236
+ return str.replace(/\//g, "\\/");
5219
5237
  }
5220
5238
  function escapeJSDoc(str) {
5221
5239
  return str.replace(/\*\//g, "*\\/");
@@ -5360,61 +5378,36 @@ var init_lru_cache = __esm({
5360
5378
  });
5361
5379
 
5362
5380
  // src/utils/pattern-utils.ts
5363
- function isRegexPattern(pattern) {
5364
- if (pattern.startsWith("^") || pattern.endsWith("$")) {
5365
- return true;
5366
- }
5367
- if (/\\[dDwWsS]/.test(pattern)) {
5368
- return true;
5369
- }
5370
- if (/\.\*|\.\+/.test(pattern)) {
5371
- return true;
5372
- }
5373
- if (/[[\]()]/.test(pattern)) {
5374
- return true;
5375
- }
5376
- if (/[^/][+?*]\{/.test(pattern)) {
5381
+ import { minimatch } from "minimatch";
5382
+ function isValidGlobPattern(pattern) {
5383
+ try {
5384
+ new minimatch.Minimatch(pattern);
5377
5385
  return true;
5386
+ } catch {
5387
+ return false;
5378
5388
  }
5379
- return false;
5380
5389
  }
5381
- function patternToRegex(pattern) {
5382
- if (pattern instanceof RegExp) {
5383
- return pattern;
5384
- }
5385
- if (isRegexPattern(pattern)) {
5386
- try {
5387
- return new RegExp(pattern);
5388
- } catch (error) {
5389
- console.warn(`\u26A0\uFE0F Invalid regex pattern "${pattern}": ${error instanceof Error ? error.message : String(error)}`);
5390
- return null;
5391
- }
5392
- }
5393
- return null;
5390
+ function isGlobPattern(pattern) {
5391
+ return /[*?[\]{}!]/.test(pattern);
5394
5392
  }
5395
5393
  function stripPrefix(input, pattern, ensureLeadingChar) {
5396
5394
  if (!pattern) {
5397
5395
  return input;
5398
5396
  }
5399
- const regex = patternToRegex(pattern);
5400
- if (regex) {
5401
- const match = input.match(regex);
5402
- if (match && match.index === 0) {
5403
- const stripped = input.substring(match[0].length);
5404
- if (ensureLeadingChar) {
5405
- if (stripped === "") {
5406
- return ensureLeadingChar;
5407
- }
5408
- if (!stripped.startsWith(ensureLeadingChar)) {
5409
- return `${ensureLeadingChar}${stripped}`;
5410
- }
5397
+ if (isGlobPattern(pattern) && !isValidGlobPattern(pattern)) {
5398
+ console.warn(`\u26A0\uFE0F Invalid glob pattern "${pattern}": Pattern is malformed`);
5399
+ return input;
5400
+ }
5401
+ if (isGlobPattern(pattern)) {
5402
+ let longestMatch = -1;
5403
+ for (let i = 1; i <= input.length; i++) {
5404
+ const testPrefix = input.substring(0, i);
5405
+ if (minimatch(testPrefix, pattern)) {
5406
+ longestMatch = i;
5411
5407
  }
5412
- return stripped;
5413
5408
  }
5414
- } else {
5415
- const stringPattern = pattern;
5416
- if (input.startsWith(stringPattern)) {
5417
- const stripped = input.substring(stringPattern.length);
5409
+ if (longestMatch > 0) {
5410
+ const stripped = input.substring(longestMatch);
5418
5411
  if (ensureLeadingChar) {
5419
5412
  if (stripped === "") {
5420
5413
  return ensureLeadingChar;
@@ -5423,8 +5416,21 @@ function stripPrefix(input, pattern, ensureLeadingChar) {
5423
5416
  return `${ensureLeadingChar}${stripped}`;
5424
5417
  }
5425
5418
  }
5426
- return stripped;
5419
+ return stripped === "" && !ensureLeadingChar ? input : stripped;
5420
+ }
5421
+ return input;
5422
+ }
5423
+ if (input.startsWith(pattern)) {
5424
+ const stripped = input.substring(pattern.length);
5425
+ if (ensureLeadingChar) {
5426
+ if (stripped === "") {
5427
+ return ensureLeadingChar;
5428
+ }
5429
+ if (!stripped.startsWith(ensureLeadingChar)) {
5430
+ return `${ensureLeadingChar}${stripped}`;
5431
+ }
5427
5432
  }
5433
+ return stripped;
5428
5434
  }
5429
5435
  return input;
5430
5436
  }
@@ -6363,9 +6369,25 @@ var init_property_generator = __esm({
6363
6369
  return wrapNullable(zodLiteral, nullable);
6364
6370
  }
6365
6371
  if (schema.enum) {
6366
- const enumValues = schema.enum.map((v) => `"${v}"`).join(", ");
6367
- const zodEnum = `z.enum([${enumValues}])`;
6368
- return wrapNullable(zodEnum, nullable);
6372
+ const allBooleans = schema.enum.every((v) => typeof v === "boolean");
6373
+ if (allBooleans) {
6374
+ const zodBoolean = "z.boolean()";
6375
+ return wrapNullable(zodBoolean, nullable);
6376
+ }
6377
+ const allStrings = schema.enum.every((v) => typeof v === "string");
6378
+ if (allStrings) {
6379
+ const enumValues = schema.enum.map((v) => `"${v}"`).join(", ");
6380
+ const zodEnum = `z.enum([${enumValues}])`;
6381
+ return wrapNullable(zodEnum, nullable);
6382
+ }
6383
+ const literalValues = schema.enum.map((v) => {
6384
+ if (typeof v === "string") {
6385
+ return `z.literal("${v}")`;
6386
+ }
6387
+ return `z.literal(${v})`;
6388
+ }).join(", ");
6389
+ const zodUnion = `z.union([${literalValues}])`;
6390
+ return wrapNullable(zodUnion, nullable);
6369
6391
  }
6370
6392
  if (schema.allOf) {
6371
6393
  let composition = generateAllOf(
@@ -6498,7 +6520,7 @@ var init_property_generator = __esm({
6498
6520
  });
6499
6521
 
6500
6522
  // src/utils/operation-filters.ts
6501
- import { minimatch } from "minimatch";
6523
+ import { minimatch as minimatch2 } from "minimatch";
6502
6524
  function createFilterStatistics() {
6503
6525
  return {
6504
6526
  totalOperations: 0,
@@ -6517,7 +6539,7 @@ function matchesAnyPattern(value, patterns) {
6517
6539
  if (!value) {
6518
6540
  return false;
6519
6541
  }
6520
- return patterns.some((pattern) => minimatch(value, pattern));
6542
+ return patterns.some((pattern) => minimatch2(value, pattern));
6521
6543
  }
6522
6544
  function containsAny(arr, values) {
6523
6545
  if (!values || values.length === 0) {
@@ -6640,7 +6662,7 @@ var init_operation_filters = __esm({
6640
6662
  // src/openapi-generator.ts
6641
6663
  import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
6642
6664
  import { dirname, normalize } from "path";
6643
- import { minimatch as minimatch2 } from "minimatch";
6665
+ import { minimatch as minimatch3 } from "minimatch";
6644
6666
  import { parse } from "yaml";
6645
6667
  var OpenApiGenerator;
6646
6668
  var init_openapi_generator = __esm({
@@ -6767,6 +6789,9 @@ var init_openapi_generator = __esm({
6767
6789
  throw new SpecValidationError("No schemas found in OpenAPI spec", { filePath: this.options.input });
6768
6790
  }
6769
6791
  for (const [name, schema] of Object.entries(this.spec.components.schemas)) {
6792
+ if (this.options.operationFilters && this.schemaUsageMap.size > 0 && !this.schemaUsageMap.has(name)) {
6793
+ continue;
6794
+ }
6770
6795
  this.generateComponentSchema(name, schema);
6771
6796
  }
6772
6797
  this.generateQueryParameterSchemas();
@@ -7255,7 +7280,7 @@ ${propsCode}
7255
7280
  const headerLower = headerName.toLowerCase();
7256
7281
  return ignorePatterns.some((pattern) => {
7257
7282
  const patternLower = pattern.toLowerCase();
7258
- return minimatch2(headerLower, patternLower);
7283
+ return minimatch3(headerLower, patternLower);
7259
7284
  });
7260
7285
  }
7261
7286
  /**
@@ -7343,8 +7368,22 @@ ${propsCode}
7343
7368
  return `${schemaName}Schema`;
7344
7369
  }
7345
7370
  if (schema.enum) {
7346
- const enumValues = schema.enum.map((v) => typeof v === "string" ? `"${v}"` : v).join(", ");
7347
- return `z.enum([${enumValues}])`;
7371
+ const allBooleans = schema.enum.every((v) => typeof v === "boolean");
7372
+ if (allBooleans) {
7373
+ return "z.boolean()";
7374
+ }
7375
+ const allStrings = schema.enum.every((v) => typeof v === "string");
7376
+ if (allStrings) {
7377
+ const enumValues = schema.enum.map((v) => `"${v}"`).join(", ");
7378
+ return `z.enum([${enumValues}])`;
7379
+ }
7380
+ const literalValues = schema.enum.map((v) => {
7381
+ if (typeof v === "string") {
7382
+ return `z.literal("${v}")`;
7383
+ }
7384
+ return `z.literal(${v})`;
7385
+ }).join(", ");
7386
+ return `z.union([${literalValues}])`;
7348
7387
  }
7349
7388
  const type = schema.type;
7350
7389
  if (type === "string") {