@cerios/openapi-to-zod 1.6.0 → 1.7.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
@@ -5,6 +5,14 @@ export { CircularReferenceError, CliOptionsError, ConfigValidationError, Executi
5
5
  * Re-export core types from @cerios/openapi-core
6
6
  */
7
7
 
8
+ /**
9
+ * UUID/GUID format option for controlling how `format: "uuid"` and `format: "guid"` are translated to Zod validators
10
+ *
11
+ * - `"uuid"`: Uses `z.uuid()` (default)
12
+ * - `"guid"`: Uses `z.guid()`
13
+ * - `"uuidv1"` through `"uuidv8"`: Uses `z.uuid({ version: "v1" })` through `z.uuid({ version: "v8" })`
14
+ */
15
+ type UuidFormat = "uuid" | "guid" | "uuidv1" | "uuidv2" | "uuidv3" | "uuidv4" | "uuidv5" | "uuidv6" | "uuidv7" | "uuidv8";
8
16
  /**
9
17
  * Common options shared by both request and response contexts
10
18
  */
@@ -162,6 +170,17 @@ interface OpenApiGeneratorOptions extends BaseGeneratorOptions {
162
170
  * @default "z.iso.datetime()" (requires Z suffix per ISO 8601)
163
171
  */
164
172
  customDateTimeFormatRegex?: string | RegExp;
173
+ /**
174
+ * UUID/GUID format for Zod validation
175
+ * Controls how OpenAPI `format: "uuid"` and `format: "guid"` are translated to Zod validators
176
+ *
177
+ * - `"uuid"`: Uses `z.uuid()` (default)
178
+ * - `"guid"`: Uses `z.guid()`
179
+ * - `"uuidv1"` through `"uuidv8"`: Uses `z.uuid({ version: "v1" })` through `z.uuid({ version: "v8" })`
180
+ *
181
+ * @default "uuid"
182
+ */
183
+ uuidFormat?: UuidFormat;
165
184
  /**
166
185
  * Output path for Zod schemas when using separate type/schema files.
167
186
  *
@@ -323,6 +342,11 @@ interface PropertyGeneratorContext {
323
342
  * @default "z.iso.datetime()"
324
343
  */
325
344
  dateTimeValidation: string;
345
+ /**
346
+ * Zod validation string for uuid/guid format fields
347
+ * @default "z.uuid()"
348
+ */
349
+ uuidValidation: string;
326
350
  /**
327
351
  * Instance-level cache for escaped regex patterns (parallel-safe)
328
352
  */
@@ -446,6 +470,8 @@ declare class OpenApiGenerator {
446
470
  private patternCache;
447
471
  /** Instance-level date-time validation string for parallel-safe execution */
448
472
  private dateTimeValidation;
473
+ /** Instance-level uuid validation string for parallel-safe execution */
474
+ private uuidValidation;
449
475
  /** Track schemas involved in circular dependency chains */
450
476
  private circularDependencies;
451
477
  /** Separate schemas mode - when outputZodSchemas is specified */
@@ -589,6 +615,19 @@ declare class OpenApiGenerator {
589
615
  * // RegExp literal (TypeScript configs)
590
616
  * buildDateTimeValidation(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/)
591
617
  */
618
+ /**
619
+ * Build the Zod validation string for uuid/guid format
620
+ * Pure function that returns the validation string without side effects
621
+ *
622
+ * @param format - UUID format option
623
+ * @returns Zod validation string
624
+ *
625
+ * @example
626
+ * buildUuidValidation() // Returns "z.uuid()"
627
+ * buildUuidValidation("guid") // Returns "z.guid()"
628
+ * buildUuidValidation("uuidv4") // Returns 'z.uuid({ version: "v4" })'
629
+ */
630
+ declare function buildUuidValidation(format?: UuidFormat): string;
592
631
  declare function buildDateTimeValidation(pattern?: string | RegExp): string;
593
632
 
594
- export { type CommonSchemaOptions, type ConfigFile, type InternalOpenApiGeneratorOptions, OpenApiGenerator, type OpenApiGeneratorOptions, PropertyGenerator, type PropertyGeneratorContext, type RequestOptions, type ResponseOptions, buildDateTimeValidation, defineConfig };
633
+ export { type CommonSchemaOptions, type ConfigFile, type InternalOpenApiGeneratorOptions, OpenApiGenerator, type OpenApiGeneratorOptions, PropertyGenerator, type PropertyGeneratorContext, type RequestOptions, type ResponseOptions, type UuidFormat, buildDateTimeValidation, buildUuidValidation, defineConfig };
package/dist/index.d.ts CHANGED
@@ -5,6 +5,14 @@ export { CircularReferenceError, CliOptionsError, ConfigValidationError, Executi
5
5
  * Re-export core types from @cerios/openapi-core
6
6
  */
7
7
 
8
+ /**
9
+ * UUID/GUID format option for controlling how `format: "uuid"` and `format: "guid"` are translated to Zod validators
10
+ *
11
+ * - `"uuid"`: Uses `z.uuid()` (default)
12
+ * - `"guid"`: Uses `z.guid()`
13
+ * - `"uuidv1"` through `"uuidv8"`: Uses `z.uuid({ version: "v1" })` through `z.uuid({ version: "v8" })`
14
+ */
15
+ type UuidFormat = "uuid" | "guid" | "uuidv1" | "uuidv2" | "uuidv3" | "uuidv4" | "uuidv5" | "uuidv6" | "uuidv7" | "uuidv8";
8
16
  /**
9
17
  * Common options shared by both request and response contexts
10
18
  */
@@ -162,6 +170,17 @@ interface OpenApiGeneratorOptions extends BaseGeneratorOptions {
162
170
  * @default "z.iso.datetime()" (requires Z suffix per ISO 8601)
163
171
  */
164
172
  customDateTimeFormatRegex?: string | RegExp;
173
+ /**
174
+ * UUID/GUID format for Zod validation
175
+ * Controls how OpenAPI `format: "uuid"` and `format: "guid"` are translated to Zod validators
176
+ *
177
+ * - `"uuid"`: Uses `z.uuid()` (default)
178
+ * - `"guid"`: Uses `z.guid()`
179
+ * - `"uuidv1"` through `"uuidv8"`: Uses `z.uuid({ version: "v1" })` through `z.uuid({ version: "v8" })`
180
+ *
181
+ * @default "uuid"
182
+ */
183
+ uuidFormat?: UuidFormat;
165
184
  /**
166
185
  * Output path for Zod schemas when using separate type/schema files.
167
186
  *
@@ -323,6 +342,11 @@ interface PropertyGeneratorContext {
323
342
  * @default "z.iso.datetime()"
324
343
  */
325
344
  dateTimeValidation: string;
345
+ /**
346
+ * Zod validation string for uuid/guid format fields
347
+ * @default "z.uuid()"
348
+ */
349
+ uuidValidation: string;
326
350
  /**
327
351
  * Instance-level cache for escaped regex patterns (parallel-safe)
328
352
  */
@@ -446,6 +470,8 @@ declare class OpenApiGenerator {
446
470
  private patternCache;
447
471
  /** Instance-level date-time validation string for parallel-safe execution */
448
472
  private dateTimeValidation;
473
+ /** Instance-level uuid validation string for parallel-safe execution */
474
+ private uuidValidation;
449
475
  /** Track schemas involved in circular dependency chains */
450
476
  private circularDependencies;
451
477
  /** Separate schemas mode - when outputZodSchemas is specified */
@@ -589,6 +615,19 @@ declare class OpenApiGenerator {
589
615
  * // RegExp literal (TypeScript configs)
590
616
  * buildDateTimeValidation(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/)
591
617
  */
618
+ /**
619
+ * Build the Zod validation string for uuid/guid format
620
+ * Pure function that returns the validation string without side effects
621
+ *
622
+ * @param format - UUID format option
623
+ * @returns Zod validation string
624
+ *
625
+ * @example
626
+ * buildUuidValidation() // Returns "z.uuid()"
627
+ * buildUuidValidation("guid") // Returns "z.guid()"
628
+ * buildUuidValidation("uuidv4") // Returns 'z.uuid({ version: "v4" })'
629
+ */
630
+ declare function buildUuidValidation(format?: UuidFormat): string;
592
631
  declare function buildDateTimeValidation(pattern?: string | RegExp): string;
593
632
 
594
- export { type CommonSchemaOptions, type ConfigFile, type InternalOpenApiGeneratorOptions, OpenApiGenerator, type OpenApiGeneratorOptions, PropertyGenerator, type PropertyGeneratorContext, type RequestOptions, type ResponseOptions, buildDateTimeValidation, defineConfig };
633
+ export { type CommonSchemaOptions, type ConfigFile, type InternalOpenApiGeneratorOptions, OpenApiGenerator, type OpenApiGeneratorOptions, PropertyGenerator, type PropertyGeneratorContext, type RequestOptions, type ResponseOptions, type UuidFormat, buildDateTimeValidation, buildUuidValidation, defineConfig };
package/dist/index.js CHANGED
@@ -30,6 +30,7 @@ __export(src_exports, {
30
30
  SchemaGenerationError: () => import_openapi_core7.SchemaGenerationError,
31
31
  SpecValidationError: () => import_openapi_core7.SpecValidationError,
32
32
  buildDateTimeValidation: () => buildDateTimeValidation,
33
+ buildUuidValidation: () => buildUuidValidation,
33
34
  defineConfig: () => defineConfig
34
35
  });
35
36
  module.exports = __toCommonJS(src_exports);
@@ -739,7 +740,6 @@ ${properties.join(",\n")}
739
740
  // src/validators/string-validator.ts
740
741
  var import_openapi_core3 = require("@cerios/openapi-core");
741
742
  var DEFAULT_FORMAT_MAP = {
742
- uuid: "z.uuid()",
743
743
  email: "z.email()",
744
744
  uri: "z.url()",
745
745
  url: "z.url()",
@@ -766,6 +766,16 @@ var DEFAULT_FORMAT_MAP = {
766
766
  "json-pointer": 'z.string().refine((val) => val === "" || /^(\\/([^~/]|~0|~1)+)+$/.test(val), { message: "Must be a valid JSON Pointer (RFC 6901)" })',
767
767
  "relative-json-pointer": 'z.string().refine((val) => /^(0|[1-9]\\d*)(#|(\\/([^~/]|~0|~1)+)*)$/.test(val), { message: "Must be a valid relative JSON Pointer" })'
768
768
  };
769
+ function buildUuidValidation(format) {
770
+ if (!format || format === "uuid") {
771
+ return "z.uuid()";
772
+ }
773
+ if (format === "guid") {
774
+ return "z.guid()";
775
+ }
776
+ const version = format.replace("uuid", "");
777
+ return `z.uuid({ version: "${version}" })`;
778
+ }
769
779
  function buildDateTimeValidation(pattern) {
770
780
  if (!pattern) {
771
781
  return "z.iso.datetime()";
@@ -789,6 +799,8 @@ function generateStringValidation(schema, useDescribe, context) {
789
799
  const format = schema.format || "";
790
800
  if (format === "date-time") {
791
801
  validation = context.dateTimeValidation;
802
+ } else if (format === "uuid" || format === "guid") {
803
+ validation = context.uuidValidation;
792
804
  } else {
793
805
  validation = DEFAULT_FORMAT_MAP[format] || "z.string()";
794
806
  }
@@ -1268,6 +1280,7 @@ var _PropertyGenerator = class _PropertyGenerator {
1268
1280
  case "string":
1269
1281
  validation = generateStringValidation(schema, this.context.useDescribe, {
1270
1282
  dateTimeValidation: this.context.dateTimeValidation,
1283
+ uuidValidation: this.context.uuidValidation,
1271
1284
  patternCache: this.context.patternCache
1272
1285
  });
1273
1286
  break;
@@ -1466,11 +1479,13 @@ var OpenApiGenerator = class {
1466
1479
  cacheSize: (_h = options.cacheSize) != null ? _h : 1e3,
1467
1480
  batchSize: (_i = options.batchSize) != null ? _i : 10,
1468
1481
  customDateTimeFormatRegex: options.customDateTimeFormatRegex,
1482
+ uuidFormat: options.uuidFormat,
1469
1483
  includeHeader: options.includeHeader,
1470
1484
  fileHeader: options.fileHeader
1471
1485
  };
1472
1486
  this.patternCache = new import_openapi_core6.LRUCache((_j = this.options.cacheSize) != null ? _j : 1e3);
1473
1487
  this.dateTimeValidation = buildDateTimeValidation(this.options.customDateTimeFormatRegex);
1488
+ this.uuidValidation = buildUuidValidation(this.options.uuidFormat);
1474
1489
  this.spec = (0, import_openapi_core6.loadOpenAPISpec)(this.options.input);
1475
1490
  this.validateSpec();
1476
1491
  this.requestOptions = this.resolveOptionsForContext("request");
@@ -1491,6 +1506,7 @@ var OpenApiGenerator = class {
1491
1506
  },
1492
1507
  stripSchemaPrefix: this.options.stripSchemaPrefix,
1493
1508
  dateTimeValidation: this.dateTimeValidation,
1509
+ uuidValidation: this.uuidValidation,
1494
1510
  patternCache: this.patternCache,
1495
1511
  separateTypesFile: this.separateSchemasMode,
1496
1512
  warn: (msg) => {
@@ -2032,6 +2048,7 @@ ${typeCode}`;
2032
2048
  },
2033
2049
  stripSchemaPrefix: this.options.stripSchemaPrefix,
2034
2050
  dateTimeValidation: this.dateTimeValidation,
2051
+ uuidValidation: this.uuidValidation,
2035
2052
  patternCache: this.patternCache,
2036
2053
  separateTypesFile: this.separateSchemasMode,
2037
2054
  warn: (msg) => {
@@ -2284,7 +2301,8 @@ ${propsCode}
2284
2301
  email: "z.email()",
2285
2302
  uri: "z.url()",
2286
2303
  url: "z.url()",
2287
- uuid: "z.uuid()"
2304
+ uuid: this.uuidValidation,
2305
+ guid: this.uuidValidation
2288
2306
  };
2289
2307
  if (schema.format && formatMap[schema.format]) {
2290
2308
  let zodType2 = formatMap[schema.format];
@@ -2536,6 +2554,7 @@ function defineConfig(config) {
2536
2554
  SchemaGenerationError,
2537
2555
  SpecValidationError,
2538
2556
  buildDateTimeValidation,
2557
+ buildUuidValidation,
2539
2558
  defineConfig
2540
2559
  });
2541
2560
  //# sourceMappingURL=index.js.map