@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.mjs CHANGED
@@ -5233,7 +5233,7 @@ function escapeDescription(str) {
5233
5233
  return str.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n");
5234
5234
  }
5235
5235
  function escapePattern(str) {
5236
- return str.replace(/\//g, "\\/");
5236
+ return str.replace(/(?<!\\)\//g, "\\/");
5237
5237
  }
5238
5238
  function escapeJSDoc(str) {
5239
5239
  return str.replace(/\*\//g, "*\\/");
@@ -5540,7 +5540,7 @@ function generateUnion(schemas, discriminator, isNullable2, context, options, cu
5540
5540
  );
5541
5541
  }
5542
5542
  if (schemas.length === 1) {
5543
- let singleSchema = context.generatePropertySchema(schemas[0], currentSchema);
5543
+ let singleSchema = context.generatePropertySchema(schemas[0], currentSchema, false, true);
5544
5544
  if ((options == null ? void 0 : options.passthrough) && !singleSchema.includes(".catchall(")) {
5545
5545
  singleSchema = `${singleSchema}.catchall(z.unknown())`;
5546
5546
  }
@@ -5556,7 +5556,7 @@ function generateUnion(schemas, discriminator, isNullable2, context, options, cu
5556
5556
  console.warn(
5557
5557
  `[openapi-to-zod] Warning: Discriminator "${discriminator}" is not required in schemas: ${discriminatorCheck.invalidSchemas.join(", ")}. Falling back to z.union() instead of z.discriminatedUnion().`
5558
5558
  );
5559
- let schemaStrings3 = resolvedSchemas.map((s) => context.generatePropertySchema(s, currentSchema));
5559
+ let schemaStrings3 = resolvedSchemas.map((s) => context.generatePropertySchema(s, currentSchema, false, true));
5560
5560
  if (options == null ? void 0 : options.passthrough) {
5561
5561
  schemaStrings3 = schemaStrings3.map((s) => s.includes(".catchall(") ? s : `${s}.catchall(z.unknown())`);
5562
5562
  }
@@ -5564,14 +5564,14 @@ function generateUnion(schemas, discriminator, isNullable2, context, options, cu
5564
5564
  const union3 = `z.union([${schemaStrings3.join(", ")}]).describe("${fallbackDescription}")`;
5565
5565
  return wrapNullable(union3, isNullable2);
5566
5566
  }
5567
- let schemaStrings2 = resolvedSchemas.map((s) => context.generatePropertySchema(s, currentSchema));
5567
+ let schemaStrings2 = resolvedSchemas.map((s) => context.generatePropertySchema(s, currentSchema, false, true));
5568
5568
  if (options == null ? void 0 : options.passthrough) {
5569
5569
  schemaStrings2 = schemaStrings2.map((s) => s.includes(".catchall(") ? s : `${s}.catchall(z.unknown())`);
5570
5570
  }
5571
5571
  const union2 = `z.discriminatedUnion("${discriminator}", [${schemaStrings2.join(", ")}])`;
5572
5572
  return wrapNullable(union2, isNullable2);
5573
5573
  }
5574
- let schemaStrings = schemas.map((s) => context.generatePropertySchema(s, currentSchema));
5574
+ let schemaStrings = schemas.map((s) => context.generatePropertySchema(s, currentSchema, false, true));
5575
5575
  if (options == null ? void 0 : options.passthrough) {
5576
5576
  schemaStrings = schemaStrings.map((s) => s.includes(".catchall(") ? s : `${s}.catchall(z.unknown())`);
5577
5577
  }
@@ -5631,9 +5631,9 @@ function detectConflictingProperties(schemas, context) {
5631
5631
  }
5632
5632
  return conflicts;
5633
5633
  }
5634
- function generateAllOf(schemas, isNullable2, context, currentSchema, explicitNullableFalse = false) {
5634
+ function generateAllOf(schemas, isNullable2, context, currentSchema) {
5635
5635
  if (schemas.length === 1) {
5636
- const singleSchema = context.generatePropertySchema(schemas[0], currentSchema, false, explicitNullableFalse);
5636
+ const singleSchema = context.generatePropertySchema(schemas[0], currentSchema, false, true);
5637
5637
  return wrapNullable(singleSchema, isNullable2);
5638
5638
  }
5639
5639
  const conflicts = detectConflictingProperties(schemas, context);
@@ -5647,23 +5647,23 @@ function generateAllOf(schemas, isNullable2, context, currentSchema, explicitNul
5647
5647
  const allObjects = schemas.every((s) => s.type === "object" || s.properties || s.$ref || s.allOf);
5648
5648
  let result;
5649
5649
  if (allObjects) {
5650
- let merged = context.generatePropertySchema(schemas[0], currentSchema, false);
5650
+ let merged = context.generatePropertySchema(schemas[0], currentSchema, false, true);
5651
5651
  for (let i = 1; i < schemas.length; i++) {
5652
5652
  const schema = schemas[i];
5653
5653
  if (schema.$ref) {
5654
- const refSchema = context.generatePropertySchema(schema, currentSchema, false);
5654
+ const refSchema = context.generatePropertySchema(schema, currentSchema, false, true);
5655
5655
  merged = `${merged}.extend(${refSchema}.shape)`;
5656
5656
  } else if (context.generateInlineObjectShape && (schema.properties || schema.type === "object")) {
5657
5657
  const inlineShape = context.generateInlineObjectShape(schema, currentSchema);
5658
5658
  merged = `${merged}.extend(${inlineShape})`;
5659
5659
  } else {
5660
- const schemaString = context.generatePropertySchema(schema, currentSchema, false);
5660
+ const schemaString = context.generatePropertySchema(schema, currentSchema, false, true);
5661
5661
  merged = `${merged}.extend(${schemaString}.shape)`;
5662
5662
  }
5663
5663
  }
5664
5664
  result = merged;
5665
5665
  } else {
5666
- const schemaStrings = schemas.map((s) => context.generatePropertySchema(s, currentSchema, false));
5666
+ const schemaStrings = schemas.map((s) => context.generatePropertySchema(s, currentSchema, false, true));
5667
5667
  let merged = schemaStrings[0];
5668
5668
  for (let i = 1; i < schemaStrings.length; i++) {
5669
5669
  merged = `${merged}.and(${schemaStrings[i]})`;
@@ -6134,20 +6134,13 @@ var init_object_validator = __esm({
6134
6134
  });
6135
6135
 
6136
6136
  // src/validators/string-validator.ts
6137
- function configurePatternCache(size) {
6138
- if (size > 0 && size !== PATTERN_CACHE.capacity) {
6139
- PATTERN_CACHE = new LRUCache(size);
6140
- }
6141
- }
6142
- function configureDateTimeFormat(pattern) {
6137
+ function buildDateTimeValidation(pattern) {
6143
6138
  if (!pattern) {
6144
- FORMAT_MAP["date-time"] = "z.iso.datetime()";
6145
- return;
6139
+ return "z.iso.datetime()";
6146
6140
  }
6147
6141
  const patternStr = pattern instanceof RegExp ? pattern.source : pattern;
6148
6142
  if (patternStr === "") {
6149
- FORMAT_MAP["date-time"] = "z.iso.datetime()";
6150
- return;
6143
+ return "z.iso.datetime()";
6151
6144
  }
6152
6145
  try {
6153
6146
  new RegExp(patternStr);
@@ -6157,10 +6150,16 @@ function configureDateTimeFormat(pattern) {
6157
6150
  );
6158
6151
  }
6159
6152
  const escapedPattern = escapePattern(patternStr);
6160
- FORMAT_MAP["date-time"] = `z.string().regex(/${escapedPattern}/)`;
6153
+ return `z.string().regex(/${escapedPattern}/)`;
6161
6154
  }
6162
- function generateStringValidation(schema, useDescribe) {
6163
- let validation = FORMAT_MAP[schema.format || ""] || "z.string()";
6155
+ function generateStringValidation(schema, useDescribe, context) {
6156
+ let validation;
6157
+ const format = schema.format || "";
6158
+ if (format === "date-time") {
6159
+ validation = context.dateTimeValidation;
6160
+ } else {
6161
+ validation = DEFAULT_FORMAT_MAP[format] || "z.string()";
6162
+ }
6164
6163
  if (schema.minLength !== void 0) {
6165
6164
  validation += `.min(${schema.minLength})`;
6166
6165
  }
@@ -6168,10 +6167,10 @@ function generateStringValidation(schema, useDescribe) {
6168
6167
  validation += `.max(${schema.maxLength})`;
6169
6168
  }
6170
6169
  if (schema.pattern) {
6171
- let escapedPattern = PATTERN_CACHE.get(schema.pattern);
6170
+ let escapedPattern = context.patternCache.get(schema.pattern);
6172
6171
  if (escapedPattern === void 0) {
6173
6172
  escapedPattern = escapePattern(schema.pattern);
6174
- PATTERN_CACHE.set(schema.pattern, escapedPattern);
6173
+ context.patternCache.set(schema.pattern, escapedPattern);
6175
6174
  }
6176
6175
  validation += `.regex(/${escapedPattern}/)`;
6177
6176
  }
@@ -6201,10 +6200,10 @@ function generateStringValidation(schema, useDescribe) {
6201
6200
  validation += `.max(${schema.maxLength})`;
6202
6201
  }
6203
6202
  if (schema.pattern) {
6204
- let escapedPattern = PATTERN_CACHE.get(schema.pattern);
6203
+ let escapedPattern = context.patternCache.get(schema.pattern);
6205
6204
  if (escapedPattern === void 0) {
6206
6205
  escapedPattern = escapePattern(schema.pattern);
6207
- PATTERN_CACHE.set(schema.pattern, escapedPattern);
6206
+ context.patternCache.set(schema.pattern, escapedPattern);
6208
6207
  }
6209
6208
  validation += `.regex(/${escapedPattern}/)`;
6210
6209
  }
@@ -6224,14 +6223,12 @@ function generateStringValidation(schema, useDescribe) {
6224
6223
  }
6225
6224
  return addDescription(validation, schema.description, useDescribe);
6226
6225
  }
6227
- var PATTERN_CACHE, DEFAULT_FORMAT_MAP, FORMAT_MAP;
6226
+ var DEFAULT_FORMAT_MAP;
6228
6227
  var init_string_validator = __esm({
6229
6228
  "src/validators/string-validator.ts"() {
6230
6229
  "use strict";
6231
6230
  init_esm_shims();
6232
- init_lru_cache();
6233
6231
  init_string_utils();
6234
- PATTERN_CACHE = new LRUCache(1e3);
6235
6232
  DEFAULT_FORMAT_MAP = {
6236
6233
  uuid: "z.uuid()",
6237
6234
  email: "z.email()",
@@ -6260,10 +6257,6 @@ var init_string_validator = __esm({
6260
6257
  "json-pointer": 'z.string().refine((val) => val === "" || /^(\\/([^~/]|~0|~1)+)+$/.test(val), { message: "Must be a valid JSON Pointer (RFC 6901)" })',
6261
6258
  "relative-json-pointer": 'z.string().refine((val) => /^(0|[1-9]\\d*)(#|(\\/([^~/]|~0|~1)+)*)$/.test(val), { message: "Must be a valid relative JSON Pointer" })'
6262
6259
  };
6263
- FORMAT_MAP = {
6264
- ...DEFAULT_FORMAT_MAP,
6265
- "date-time": "z.iso.datetime()"
6266
- };
6267
6260
  }
6268
6261
  });
6269
6262
 
@@ -6575,17 +6568,16 @@ var init_property_generator = __esm({
6575
6568
  return wrapNullable(zodUnion, nullable);
6576
6569
  }
6577
6570
  if (schema.allOf) {
6578
- const explicitNullableFalse = schema.nullable === false;
6571
+ const compositionNullable = isNullable(schema, false);
6579
6572
  let composition = generateAllOf(
6580
6573
  schema.allOf,
6581
- nullable,
6574
+ compositionNullable,
6582
6575
  {
6583
6576
  generatePropertySchema: this.generatePropertySchema.bind(this),
6584
6577
  generateInlineObjectShape: this.generateInlineObjectShape.bind(this),
6585
6578
  resolveSchemaRef: this.resolveSchemaRef.bind(this)
6586
6579
  },
6587
- currentSchema,
6588
- explicitNullableFalse
6580
+ currentSchema
6589
6581
  );
6590
6582
  if (schema.unevaluatedProperties !== void 0) {
6591
6583
  composition = this.applyUnevaluatedProperties(composition, schema);
@@ -6593,11 +6585,12 @@ var init_property_generator = __esm({
6593
6585
  return composition;
6594
6586
  }
6595
6587
  if (schema.oneOf) {
6588
+ const compositionNullable = isNullable(schema, false);
6596
6589
  const needsPassthrough = schema.unevaluatedProperties !== void 0;
6597
6590
  let composition = generateUnion(
6598
6591
  schema.oneOf,
6599
6592
  (_b = schema.discriminator) == null ? void 0 : _b.propertyName,
6600
- nullable,
6593
+ compositionNullable,
6601
6594
  {
6602
6595
  generatePropertySchema: this.generatePropertySchema.bind(this),
6603
6596
  resolveDiscriminatorMapping: this.resolveDiscriminatorMapping.bind(this),
@@ -6615,11 +6608,12 @@ var init_property_generator = __esm({
6615
6608
  return composition;
6616
6609
  }
6617
6610
  if (schema.anyOf) {
6611
+ const compositionNullable = isNullable(schema, false);
6618
6612
  const needsPassthrough = schema.unevaluatedProperties !== void 0;
6619
6613
  let composition = generateUnion(
6620
6614
  schema.anyOf,
6621
6615
  (_d = schema.discriminator) == null ? void 0 : _d.propertyName,
6622
- nullable,
6616
+ compositionNullable,
6623
6617
  {
6624
6618
  generatePropertySchema: this.generatePropertySchema.bind(this),
6625
6619
  resolveDiscriminatorMapping: this.resolveDiscriminatorMapping.bind(this),
@@ -6652,7 +6646,10 @@ var init_property_generator = __esm({
6652
6646
  const primaryType = getPrimaryType(schema);
6653
6647
  switch (primaryType) {
6654
6648
  case "string":
6655
- validation = generateStringValidation(schema, this.context.useDescribe);
6649
+ validation = generateStringValidation(schema, this.context.useDescribe, {
6650
+ dateTimeValidation: this.context.dateTimeValidation,
6651
+ patternCache: this.context.patternCache
6652
+ });
6656
6653
  break;
6657
6654
  case "number":
6658
6655
  validation = generateNumberValidation(schema, false, this.context.useDescribe);
@@ -6973,6 +6970,7 @@ var init_openapi_generator = __esm({
6973
6970
  init_enum_generator();
6974
6971
  init_jsdoc_generator();
6975
6972
  init_property_generator();
6973
+ init_lru_cache();
6976
6974
  init_name_utils();
6977
6975
  init_operation_filters();
6978
6976
  init_pattern_utils();
@@ -6986,7 +6984,7 @@ var init_openapi_generator = __esm({
6986
6984
  this.schemaUsageMap = /* @__PURE__ */ new Map();
6987
6985
  this.needsZodImport = true;
6988
6986
  this.filterStats = createFilterStatistics();
6989
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
6987
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
6990
6988
  if (!options.input) {
6991
6989
  throw new ConfigurationError("Input path is required", { providedOptions: options });
6992
6990
  }
@@ -7012,12 +7010,8 @@ var init_openapi_generator = __esm({
7012
7010
  batchSize: (_g = options.batchSize) != null ? _g : 10,
7013
7011
  customDateTimeFormatRegex: options.customDateTimeFormatRegex
7014
7012
  };
7015
- if (this.options.cacheSize) {
7016
- configurePatternCache(this.options.cacheSize);
7017
- }
7018
- if (this.options.customDateTimeFormatRegex) {
7019
- configureDateTimeFormat(this.options.customDateTimeFormatRegex);
7020
- }
7013
+ this.patternCache = new LRUCache((_h = this.options.cacheSize) != null ? _h : 1e3);
7014
+ this.dateTimeValidation = buildDateTimeValidation(this.options.customDateTimeFormatRegex);
7021
7015
  try {
7022
7016
  const fs = __require("fs");
7023
7017
  if (!fs.existsSync(this.options.input)) {
@@ -7080,13 +7074,15 @@ var init_openapi_generator = __esm({
7080
7074
  mode: this.requestOptions.mode,
7081
7075
  includeDescriptions: this.requestOptions.includeDescriptions,
7082
7076
  useDescribe: this.requestOptions.useDescribe,
7083
- defaultNullable: (_h = this.options.defaultNullable) != null ? _h : false,
7084
- emptyObjectBehavior: (_i = this.options.emptyObjectBehavior) != null ? _i : "loose",
7077
+ defaultNullable: (_i = this.options.defaultNullable) != null ? _i : false,
7078
+ emptyObjectBehavior: (_j = this.options.emptyObjectBehavior) != null ? _j : "loose",
7085
7079
  namingOptions: {
7086
7080
  prefix: this.options.prefix,
7087
7081
  suffix: this.options.suffix
7088
7082
  },
7089
- stripSchemaPrefix: this.options.stripSchemaPrefix
7083
+ stripSchemaPrefix: this.options.stripSchemaPrefix,
7084
+ dateTimeValidation: this.dateTimeValidation,
7085
+ patternCache: this.patternCache
7090
7086
  });
7091
7087
  }
7092
7088
  /**
@@ -7467,7 +7463,9 @@ ${typeCode}`;
7467
7463
  prefix: this.options.prefix,
7468
7464
  suffix: this.options.suffix
7469
7465
  },
7470
- stripSchemaPrefix: this.options.stripSchemaPrefix
7466
+ stripSchemaPrefix: this.options.stripSchemaPrefix,
7467
+ dateTimeValidation: this.dateTimeValidation,
7468
+ patternCache: this.patternCache
7471
7469
  });
7472
7470
  const zodSchema = this.propertyGenerator.generatePropertySchema(schema, name, true);
7473
7471
  const zodSchemaCode = `${jsdoc}export const ${schemaName} = ${zodSchema};`;