@cerios/openapi-to-zod 1.3.1 → 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 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(/\//g, "\\/");
5228
+ return str.replace(/(?<!\\)\//g, "\\/");
5229
5229
  }
5230
5230
  function escapeJSDoc(str) {
5231
5231
  return str.replace(/\*\//g, "*\\/");
@@ -5506,7 +5506,7 @@ function generateUnion(schemas, discriminator, isNullable2, context, options, cu
5506
5506
  );
5507
5507
  }
5508
5508
  if (schemas.length === 1) {
5509
- let singleSchema = context.generatePropertySchema(schemas[0], currentSchema);
5509
+ let singleSchema = context.generatePropertySchema(schemas[0], currentSchema, false, true);
5510
5510
  if ((options == null ? void 0 : options.passthrough) && !singleSchema.includes(".catchall(")) {
5511
5511
  singleSchema = `${singleSchema}.catchall(z.unknown())`;
5512
5512
  }
@@ -5522,7 +5522,7 @@ function generateUnion(schemas, discriminator, isNullable2, context, options, cu
5522
5522
  console.warn(
5523
5523
  `[openapi-to-zod] Warning: Discriminator "${discriminator}" is not required in schemas: ${discriminatorCheck.invalidSchemas.join(", ")}. Falling back to z.union() instead of z.discriminatedUnion().`
5524
5524
  );
5525
- let schemaStrings3 = resolvedSchemas.map((s) => context.generatePropertySchema(s, currentSchema));
5525
+ let schemaStrings3 = resolvedSchemas.map((s) => context.generatePropertySchema(s, currentSchema, false, true));
5526
5526
  if (options == null ? void 0 : options.passthrough) {
5527
5527
  schemaStrings3 = schemaStrings3.map((s) => s.includes(".catchall(") ? s : `${s}.catchall(z.unknown())`);
5528
5528
  }
@@ -5530,14 +5530,14 @@ function generateUnion(schemas, discriminator, isNullable2, context, options, cu
5530
5530
  const union3 = `z.union([${schemaStrings3.join(", ")}]).describe("${fallbackDescription}")`;
5531
5531
  return wrapNullable(union3, isNullable2);
5532
5532
  }
5533
- let schemaStrings2 = resolvedSchemas.map((s) => context.generatePropertySchema(s, currentSchema));
5533
+ let schemaStrings2 = resolvedSchemas.map((s) => context.generatePropertySchema(s, currentSchema, false, true));
5534
5534
  if (options == null ? void 0 : options.passthrough) {
5535
5535
  schemaStrings2 = schemaStrings2.map((s) => s.includes(".catchall(") ? s : `${s}.catchall(z.unknown())`);
5536
5536
  }
5537
5537
  const union2 = `z.discriminatedUnion("${discriminator}", [${schemaStrings2.join(", ")}])`;
5538
5538
  return wrapNullable(union2, isNullable2);
5539
5539
  }
5540
- let schemaStrings = schemas.map((s) => context.generatePropertySchema(s, currentSchema));
5540
+ let schemaStrings = schemas.map((s) => context.generatePropertySchema(s, currentSchema, false, true));
5541
5541
  if (options == null ? void 0 : options.passthrough) {
5542
5542
  schemaStrings = schemaStrings.map((s) => s.includes(".catchall(") ? s : `${s}.catchall(z.unknown())`);
5543
5543
  }
@@ -5597,9 +5597,9 @@ function detectConflictingProperties(schemas, context) {
5597
5597
  }
5598
5598
  return conflicts;
5599
5599
  }
5600
- function generateAllOf(schemas, isNullable2, context, currentSchema, explicitNullableFalse = false) {
5600
+ function generateAllOf(schemas, isNullable2, context, currentSchema) {
5601
5601
  if (schemas.length === 1) {
5602
- const singleSchema = context.generatePropertySchema(schemas[0], currentSchema, false, explicitNullableFalse);
5602
+ const singleSchema = context.generatePropertySchema(schemas[0], currentSchema, false, true);
5603
5603
  return wrapNullable(singleSchema, isNullable2);
5604
5604
  }
5605
5605
  const conflicts = detectConflictingProperties(schemas, context);
@@ -5613,23 +5613,23 @@ function generateAllOf(schemas, isNullable2, context, currentSchema, explicitNul
5613
5613
  const allObjects = schemas.every((s) => s.type === "object" || s.properties || s.$ref || s.allOf);
5614
5614
  let result;
5615
5615
  if (allObjects) {
5616
- let merged = context.generatePropertySchema(schemas[0], currentSchema, false);
5616
+ let merged = context.generatePropertySchema(schemas[0], currentSchema, false, true);
5617
5617
  for (let i = 1; i < schemas.length; i++) {
5618
5618
  const schema = schemas[i];
5619
5619
  if (schema.$ref) {
5620
- const refSchema = context.generatePropertySchema(schema, currentSchema, false);
5620
+ const refSchema = context.generatePropertySchema(schema, currentSchema, false, true);
5621
5621
  merged = `${merged}.extend(${refSchema}.shape)`;
5622
5622
  } else if (context.generateInlineObjectShape && (schema.properties || schema.type === "object")) {
5623
5623
  const inlineShape = context.generateInlineObjectShape(schema, currentSchema);
5624
5624
  merged = `${merged}.extend(${inlineShape})`;
5625
5625
  } else {
5626
- const schemaString = context.generatePropertySchema(schema, currentSchema, false);
5626
+ const schemaString = context.generatePropertySchema(schema, currentSchema, false, true);
5627
5627
  merged = `${merged}.extend(${schemaString}.shape)`;
5628
5628
  }
5629
5629
  }
5630
5630
  result = merged;
5631
5631
  } else {
5632
- const schemaStrings = schemas.map((s) => context.generatePropertySchema(s, currentSchema, false));
5632
+ const schemaStrings = schemas.map((s) => context.generatePropertySchema(s, currentSchema, false, true));
5633
5633
  let merged = schemaStrings[0];
5634
5634
  for (let i = 1; i < schemaStrings.length; i++) {
5635
5635
  merged = `${merged}.and(${schemaStrings[i]})`;
@@ -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
- var FORMAT_MAP = {
6116
- ...DEFAULT_FORMAT_MAP,
6117
- "date-time": "z.iso.datetime()"
6118
- };
6119
- function configureDateTimeFormat(pattern) {
6109
+ function buildDateTimeValidation(pattern) {
6120
6110
  if (!pattern) {
6121
- FORMAT_MAP["date-time"] = "z.iso.datetime()";
6122
- return;
6111
+ return "z.iso.datetime()";
6123
6112
  }
6124
6113
  const patternStr = pattern instanceof RegExp ? pattern.source : pattern;
6125
6114
  if (patternStr === "") {
6126
- FORMAT_MAP["date-time"] = "z.iso.datetime()";
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
- FORMAT_MAP["date-time"] = `z.string().regex(/${escapedPattern}/)`;
6125
+ return `z.string().regex(/${escapedPattern}/)`;
6138
6126
  }
6139
- function generateStringValidation(schema, useDescribe) {
6140
- let validation = FORMAT_MAP[schema.format || ""] || "z.string()";
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 = PATTERN_CACHE.get(schema.pattern);
6142
+ let escapedPattern = context.patternCache.get(schema.pattern);
6149
6143
  if (escapedPattern === void 0) {
6150
6144
  escapedPattern = escapePattern(schema.pattern);
6151
- PATTERN_CACHE.set(schema.pattern, escapedPattern);
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 = PATTERN_CACHE.get(schema.pattern);
6175
+ let escapedPattern = context.patternCache.get(schema.pattern);
6182
6176
  if (escapedPattern === void 0) {
6183
6177
  escapedPattern = escapePattern(schema.pattern);
6184
- PATTERN_CACHE.set(schema.pattern, escapedPattern);
6178
+ context.patternCache.set(schema.pattern, escapedPattern);
6185
6179
  }
6186
6180
  validation += `.regex(/${escapedPattern}/)`;
6187
6181
  }
@@ -6496,17 +6490,16 @@ var _PropertyGenerator = class _PropertyGenerator {
6496
6490
  return wrapNullable(zodUnion, nullable);
6497
6491
  }
6498
6492
  if (schema.allOf) {
6499
- const explicitNullableFalse = schema.nullable === false;
6493
+ const compositionNullable = isNullable(schema, false);
6500
6494
  let composition = generateAllOf(
6501
6495
  schema.allOf,
6502
- nullable,
6496
+ compositionNullable,
6503
6497
  {
6504
6498
  generatePropertySchema: this.generatePropertySchema.bind(this),
6505
6499
  generateInlineObjectShape: this.generateInlineObjectShape.bind(this),
6506
6500
  resolveSchemaRef: this.resolveSchemaRef.bind(this)
6507
6501
  },
6508
- currentSchema,
6509
- explicitNullableFalse
6502
+ currentSchema
6510
6503
  );
6511
6504
  if (schema.unevaluatedProperties !== void 0) {
6512
6505
  composition = this.applyUnevaluatedProperties(composition, schema);
@@ -6514,11 +6507,12 @@ var _PropertyGenerator = class _PropertyGenerator {
6514
6507
  return composition;
6515
6508
  }
6516
6509
  if (schema.oneOf) {
6510
+ const compositionNullable = isNullable(schema, false);
6517
6511
  const needsPassthrough = schema.unevaluatedProperties !== void 0;
6518
6512
  let composition = generateUnion(
6519
6513
  schema.oneOf,
6520
6514
  (_b = schema.discriminator) == null ? void 0 : _b.propertyName,
6521
- nullable,
6515
+ compositionNullable,
6522
6516
  {
6523
6517
  generatePropertySchema: this.generatePropertySchema.bind(this),
6524
6518
  resolveDiscriminatorMapping: this.resolveDiscriminatorMapping.bind(this),
@@ -6536,11 +6530,12 @@ var _PropertyGenerator = class _PropertyGenerator {
6536
6530
  return composition;
6537
6531
  }
6538
6532
  if (schema.anyOf) {
6533
+ const compositionNullable = isNullable(schema, false);
6539
6534
  const needsPassthrough = schema.unevaluatedProperties !== void 0;
6540
6535
  let composition = generateUnion(
6541
6536
  schema.anyOf,
6542
6537
  (_d = schema.discriminator) == null ? void 0 : _d.propertyName,
6543
- nullable,
6538
+ compositionNullable,
6544
6539
  {
6545
6540
  generatePropertySchema: this.generatePropertySchema.bind(this),
6546
6541
  resolveDiscriminatorMapping: this.resolveDiscriminatorMapping.bind(this),
@@ -6573,7 +6568,10 @@ var _PropertyGenerator = class _PropertyGenerator {
6573
6568
  const primaryType = getPrimaryType(schema);
6574
6569
  switch (primaryType) {
6575
6570
  case "string":
6576
- validation = generateStringValidation(schema, this.context.useDescribe);
6571
+ validation = generateStringValidation(schema, this.context.useDescribe, {
6572
+ dateTimeValidation: this.context.dateTimeValidation,
6573
+ patternCache: this.context.patternCache
6574
+ });
6577
6575
  break;
6578
6576
  case "number":
6579
6577
  validation = generateNumberValidation(schema, false, this.context.useDescribe);
@@ -6877,7 +6875,7 @@ var OpenApiGenerator = class {
6877
6875
  this.schemaUsageMap = /* @__PURE__ */ new Map();
6878
6876
  this.needsZodImport = true;
6879
6877
  this.filterStats = createFilterStatistics();
6880
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
6878
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
6881
6879
  if (!options.input) {
6882
6880
  throw new ConfigurationError("Input path is required", { providedOptions: options });
6883
6881
  }
@@ -6903,12 +6901,8 @@ var OpenApiGenerator = class {
6903
6901
  batchSize: (_g = options.batchSize) != null ? _g : 10,
6904
6902
  customDateTimeFormatRegex: options.customDateTimeFormatRegex
6905
6903
  };
6906
- if (this.options.cacheSize) {
6907
- configurePatternCache(this.options.cacheSize);
6908
- }
6909
- if (this.options.customDateTimeFormatRegex) {
6910
- configureDateTimeFormat(this.options.customDateTimeFormatRegex);
6911
- }
6904
+ this.patternCache = new LRUCache((_h = this.options.cacheSize) != null ? _h : 1e3);
6905
+ this.dateTimeValidation = buildDateTimeValidation(this.options.customDateTimeFormatRegex);
6912
6906
  try {
6913
6907
  const fs = require("fs");
6914
6908
  if (!fs.existsSync(this.options.input)) {
@@ -6971,13 +6965,15 @@ var OpenApiGenerator = class {
6971
6965
  mode: this.requestOptions.mode,
6972
6966
  includeDescriptions: this.requestOptions.includeDescriptions,
6973
6967
  useDescribe: this.requestOptions.useDescribe,
6974
- defaultNullable: (_h = this.options.defaultNullable) != null ? _h : false,
6975
- emptyObjectBehavior: (_i = this.options.emptyObjectBehavior) != null ? _i : "loose",
6968
+ defaultNullable: (_i = this.options.defaultNullable) != null ? _i : false,
6969
+ emptyObjectBehavior: (_j = this.options.emptyObjectBehavior) != null ? _j : "loose",
6976
6970
  namingOptions: {
6977
6971
  prefix: this.options.prefix,
6978
6972
  suffix: this.options.suffix
6979
6973
  },
6980
- stripSchemaPrefix: this.options.stripSchemaPrefix
6974
+ stripSchemaPrefix: this.options.stripSchemaPrefix,
6975
+ dateTimeValidation: this.dateTimeValidation,
6976
+ patternCache: this.patternCache
6981
6977
  });
6982
6978
  }
6983
6979
  /**
@@ -7358,7 +7354,9 @@ ${typeCode}`;
7358
7354
  prefix: this.options.prefix,
7359
7355
  suffix: this.options.suffix
7360
7356
  },
7361
- stripSchemaPrefix: this.options.stripSchemaPrefix
7357
+ stripSchemaPrefix: this.options.stripSchemaPrefix,
7358
+ dateTimeValidation: this.dateTimeValidation,
7359
+ patternCache: this.patternCache
7362
7360
  });
7363
7361
  const zodSchema = this.propertyGenerator.generatePropertySchema(schema, name, true);
7364
7362
  const zodSchemaCode = `${jsdoc}export const ${schemaName} = ${zodSchema};`;