@cerios/openapi-to-zod 1.0.0 → 1.1.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.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { O as OpenApiGeneratorOptions } from './types-BjoP91vk.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-BjoP91vk.mjs';
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';
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-BjoP91vk.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-BjoP91vk.js';
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';
3
3
 
4
4
  /**
5
5
  * Custom error classes for better error handling and debugging
package/dist/index.js CHANGED
@@ -98,8 +98,20 @@ var import_minimatch2 = require("minimatch");
98
98
  var import_yaml = require("yaml");
99
99
 
100
100
  // src/utils/name-utils.ts
101
+ function sanitizeIdentifier(str) {
102
+ return str.replace(/[^a-zA-Z0-9._\-\s]+/g, "_");
103
+ }
101
104
  function toCamelCase(str, options) {
102
- let name = str.charAt(0).toLowerCase() + str.slice(1);
105
+ const sanitized = sanitizeIdentifier(str);
106
+ const words = sanitized.split(/[.\-_\s]+/).filter((word) => word.length > 0);
107
+ let name;
108
+ if (words.length === 0) {
109
+ name = str.charAt(0).toLowerCase() + str.slice(1);
110
+ } else if (words.length === 1) {
111
+ name = words[0].charAt(0).toLowerCase() + words[0].slice(1);
112
+ } else {
113
+ name = words[0].charAt(0).toLowerCase() + words[0].slice(1) + words.slice(1).map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join("");
114
+ }
103
115
  if (options == null ? void 0 : options.prefix) {
104
116
  const prefix = options.prefix.toLowerCase();
105
117
  name = prefix + name.charAt(0).toUpperCase() + name.slice(1);
@@ -112,12 +124,23 @@ function toCamelCase(str, options) {
112
124
  }
113
125
  function toPascalCase(str) {
114
126
  const stringValue = String(str);
115
- let result = stringValue.replace(/[^a-zA-Z0-9_]+/g, "_").split(/[-_]+/).filter((word) => word.length > 0).map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join("");
127
+ const isAlreadyValidCase = /^[a-zA-Z][a-zA-Z0-9]*$/.test(stringValue);
128
+ if (isAlreadyValidCase) {
129
+ return stringValue.charAt(0).toUpperCase() + stringValue.slice(1);
130
+ }
131
+ const sanitized = sanitizeIdentifier(stringValue);
132
+ const words = sanitized.split(/[.\-_\s]+/).filter((word) => word.length > 0);
133
+ let result;
134
+ if (words.length === 0) {
135
+ result = "Value";
136
+ } else {
137
+ result = words.map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join("");
138
+ }
116
139
  if (/^\d/.test(result)) {
117
140
  result = `N${result}`;
118
141
  }
119
142
  if (!result || /^_+$/.test(result)) {
120
- result = "Value";
143
+ return "Value";
121
144
  }
122
145
  return result;
123
146
  }
@@ -129,9 +152,10 @@ function resolveRef(ref) {
129
152
  // src/generators/enum-generator.ts
130
153
  function generateEnum(name, values, options) {
131
154
  const schemaName = `${toCamelCase(name, options)}Schema`;
155
+ const typeName = toPascalCase(name);
132
156
  const enumValues = values.map((v) => `"${v}"`).join(", ");
133
157
  const schemaCode = `export const ${schemaName} = z.enum([${enumValues}]);`;
134
- const typeCode = `export type ${name} = z.infer<typeof ${schemaName}>;`;
158
+ const typeCode = `export type ${typeName} = z.infer<typeof ${schemaName}>;`;
135
159
  return { schemaCode, typeCode };
136
160
  }
137
161
 
@@ -264,6 +288,76 @@ var LRUCache = class {
264
288
  }
265
289
  };
266
290
 
291
+ // 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)) {
306
+ return true;
307
+ }
308
+ return false;
309
+ }
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;
323
+ }
324
+ function stripPrefix(input, pattern, ensureLeadingChar) {
325
+ if (!pattern) {
326
+ return input;
327
+ }
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
+ }
340
+ }
341
+ return stripped;
342
+ }
343
+ } else {
344
+ const stringPattern = pattern;
345
+ if (input.startsWith(stringPattern)) {
346
+ const stripped = input.substring(stringPattern.length);
347
+ if (ensureLeadingChar) {
348
+ if (stripped === "") {
349
+ return ensureLeadingChar;
350
+ }
351
+ if (!stripped.startsWith(ensureLeadingChar)) {
352
+ return `${ensureLeadingChar}${stripped}`;
353
+ }
354
+ }
355
+ return stripped;
356
+ }
357
+ }
358
+ return input;
359
+ }
360
+
267
361
  // src/validators/array-validator.ts
268
362
  function generateArrayValidation(schema, context) {
269
363
  var _a;
@@ -1120,8 +1214,9 @@ var _PropertyGenerator = class _PropertyGenerator {
1120
1214
  }
1121
1215
  (_a = this.context.schemaDependencies.get(currentSchema)) == null ? void 0 : _a.add(refName);
1122
1216
  }
1123
- const schemaName = `${toCamelCase(resolvedRefName, this.context.namingOptions)}Schema`;
1124
- if (currentSchema && this.isCircularThroughAlias(currentSchema, refName)) {
1217
+ const strippedRefName = stripPrefix(resolvedRefName, this.context.stripSchemaPrefix);
1218
+ const schemaName = `${toCamelCase(strippedRefName, this.context.namingOptions)}Schema`;
1219
+ if (currentSchema && (refName === currentSchema || this.isCircularThroughAlias(currentSchema, refName))) {
1125
1220
  const lazySchema = `z.lazy((): z.ZodTypeAny => ${schemaName})`;
1126
1221
  return wrapNullable(lazySchema, nullable);
1127
1222
  }
@@ -1421,6 +1516,7 @@ var OpenApiGenerator = class {
1421
1516
  schemaType: options.schemaType || "all",
1422
1517
  prefix: options.prefix,
1423
1518
  suffix: options.suffix,
1519
+ stripSchemaPrefix: options.stripSchemaPrefix,
1424
1520
  showStats: (_c = options.showStats) != null ? _c : true,
1425
1521
  request: options.request,
1426
1522
  response: options.response,
@@ -1497,7 +1593,8 @@ var OpenApiGenerator = class {
1497
1593
  namingOptions: {
1498
1594
  prefix: this.options.prefix,
1499
1595
  suffix: this.options.suffix
1500
- }
1596
+ },
1597
+ stripSchemaPrefix: this.options.stripSchemaPrefix
1501
1598
  });
1502
1599
  }
1503
1600
  /**
@@ -1531,9 +1628,11 @@ var OpenApiGenerator = class {
1531
1628
  const typeCode = this.types.get(name);
1532
1629
  if (schemaCode) {
1533
1630
  output.push(schemaCode);
1534
- if (!schemaCode.includes(`export type ${name}`)) {
1535
- const schemaName = `${toCamelCase(name, { prefix: this.options.prefix, suffix: this.options.suffix })}Schema`;
1536
- output.push(`export type ${name} = z.infer<typeof ${schemaName}>;`);
1631
+ const strippedName = stripPrefix(name, this.options.stripSchemaPrefix);
1632
+ const typeName = toPascalCase(strippedName);
1633
+ if (!schemaCode.includes(`export type ${typeName}`)) {
1634
+ const schemaName = `${toCamelCase(strippedName, { prefix: this.options.prefix, suffix: this.options.suffix })}Schema`;
1635
+ output.push(`export type ${typeName} = z.infer<typeof ${schemaName}>;`);
1537
1636
  }
1538
1637
  output.push("");
1539
1638
  } else if (typeCode) {
@@ -1849,7 +1948,8 @@ var OpenApiGenerator = class {
1849
1948
  const resolvedOptions = context === "response" ? this.responseOptions : this.requestOptions;
1850
1949
  if (schema.enum) {
1851
1950
  const jsdoc2 = generateJSDoc(schema, name, { includeDescriptions: resolvedOptions.includeDescriptions });
1852
- const { schemaCode, typeCode } = generateEnum(name, schema.enum, {
1951
+ const strippedName2 = stripPrefix(name, this.options.stripSchemaPrefix);
1952
+ const { schemaCode, typeCode } = generateEnum(strippedName2, schema.enum, {
1853
1953
  prefix: this.options.prefix,
1854
1954
  suffix: this.options.suffix
1855
1955
  });
@@ -1858,7 +1958,8 @@ ${typeCode}`;
1858
1958
  this.schemas.set(name, enumSchemaCode);
1859
1959
  return;
1860
1960
  }
1861
- const schemaName = `${toCamelCase(name, { prefix: this.options.prefix, suffix: this.options.suffix })}Schema`;
1961
+ const strippedName = stripPrefix(name, this.options.stripSchemaPrefix);
1962
+ const schemaName = `${toCamelCase(strippedName, { prefix: this.options.prefix, suffix: this.options.suffix })}Schema`;
1862
1963
  const jsdoc = generateJSDoc(schema, name, { includeDescriptions: resolvedOptions.includeDescriptions });
1863
1964
  if (schema.allOf && schema.allOf.length === 1 && schema.allOf[0].$ref) {
1864
1965
  const refName = resolveRef(schema.allOf[0].$ref);
@@ -1874,7 +1975,8 @@ ${typeCode}`;
1874
1975
  namingOptions: {
1875
1976
  prefix: this.options.prefix,
1876
1977
  suffix: this.options.suffix
1877
- }
1978
+ },
1979
+ stripSchemaPrefix: this.options.stripSchemaPrefix
1878
1980
  });
1879
1981
  const isAlias = !!(schema.$ref && !schema.properties && !schema.allOf && !schema.oneOf && !schema.anyOf);
1880
1982
  const zodSchema = this.propertyGenerator.generatePropertySchema(schema, name, isAlias);
@@ -2076,7 +2178,8 @@ ${propsCode}
2076
2178
  generateQueryParamType(schema, param) {
2077
2179
  if (schema.$ref) {
2078
2180
  const refName = resolveRef(schema.$ref);
2079
- const schemaName = toCamelCase(refName, { prefix: this.options.prefix, suffix: this.options.suffix });
2181
+ const strippedRefName = stripPrefix(refName, this.options.stripSchemaPrefix);
2182
+ const schemaName = toCamelCase(strippedRefName, { prefix: this.options.prefix, suffix: this.options.suffix });
2080
2183
  return `${schemaName}Schema`;
2081
2184
  }
2082
2185
  if (schema.enum) {