@cerios/openapi-to-zod 1.1.1 → 1.3.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.
@@ -1,4 +1,4 @@
1
- import { E as ExecutionMode, d as OperationFilters } from './types-CI48CjiU.mjs';
1
+ import { E as ExecutionMode, g as OperationFilters, f as OpenAPISpec, b as OpenAPIParameter, c as OpenAPIRequestBody, d as OpenAPIResponse, e as OpenAPISchema } from './types-B3GgqGzM.mjs';
2
2
  import { z } from 'zod';
3
3
  import { Loader } from 'cosmiconfig';
4
4
 
@@ -59,6 +59,7 @@ declare const RequestResponseOptionsSchema: z.ZodObject<{
59
59
  }>>;
60
60
  useDescribe: z.ZodOptional<z.ZodBoolean>;
61
61
  includeDescriptions: z.ZodOptional<z.ZodBoolean>;
62
+ defaultNullable: z.ZodOptional<z.ZodBoolean>;
62
63
  }, z.core.$strict>;
63
64
  /**
64
65
  * @shared Base Zod schema for operation filters (without status codes)
@@ -225,6 +226,55 @@ declare function stripPrefix(input: string, pattern: string | undefined, ensureL
225
226
  */
226
227
  declare function stripPathPrefix(path: string, pattern: string | undefined): string;
227
228
 
229
+ /**
230
+ * OpenAPI $ref resolution utilities
231
+ *
232
+ * Provides functions to resolve $ref references to component definitions
233
+ * Supports: parameters, requestBodies, responses, schemas
234
+ *
235
+ * @internal Used by core and playwright packages
236
+ */
237
+
238
+ /**
239
+ * Type for any resolvable component
240
+ */
241
+ type ResolvableComponent = OpenAPIParameter | OpenAPIRequestBody | OpenAPIResponse | OpenAPISchema | any;
242
+ /**
243
+ * Resolve a $ref to a component definition
244
+ * Handles nested $refs by recursively resolving until no more refs found
245
+ *
246
+ * @param obj - Object that may contain a $ref
247
+ * @param spec - The OpenAPI specification
248
+ * @param maxDepth - Maximum recursion depth to prevent infinite loops (default: 10)
249
+ * @returns The resolved component, or the original object if not a reference
250
+ */
251
+ declare function resolveRef<T extends ResolvableComponent>(obj: T | any, spec: OpenAPISpec, maxDepth?: number): T;
252
+ /**
253
+ * Resolve a parameter reference
254
+ * Convenience wrapper for resolveRef with parameter type
255
+ */
256
+ declare function resolveParameterRef(param: any, spec: OpenAPISpec): OpenAPIParameter | any;
257
+ /**
258
+ * Resolve a request body reference
259
+ * Convenience wrapper for resolveRef with request body type
260
+ */
261
+ declare function resolveRequestBodyRef(requestBody: any, spec: OpenAPISpec): OpenAPIRequestBody | any;
262
+ /**
263
+ * Resolve a response reference
264
+ * Convenience wrapper for resolveRef with response type
265
+ */
266
+ declare function resolveResponseRef(response: any, spec: OpenAPISpec): OpenAPIResponse | any;
267
+ /**
268
+ * Merge path-level parameters with operation-level parameters
269
+ * Operation parameters override path-level parameters with the same name and location
270
+ *
271
+ * @param pathParams - Parameters defined at the path level
272
+ * @param operationParams - Parameters defined at the operation level
273
+ * @param spec - The OpenAPI specification for resolving $refs
274
+ * @returns Merged array of resolved parameters
275
+ */
276
+ declare function mergeParameters(pathParams: any[] | undefined, operationParams: any[] | undefined, spec: OpenAPISpec): any[];
277
+
228
278
  /**
229
279
  * String utility functions for escaping and formatting
230
280
  */
@@ -248,4 +298,24 @@ declare function escapeJSDoc(str: string): string;
248
298
  */
249
299
  declare function createTypeScriptLoader(): Loader;
250
300
 
251
- export { type BaseOperationFilters, type FilterStatistics, type Generator, LRUCache, OperationFiltersSchema, type RequestResponseOptions, RequestResponseOptionsSchema, createFilterStatistics, createTypeScriptLoader, escapeJSDoc, executeBatch, formatConfigValidationError, formatFilterStatistics, getBatchExitCode, shouldIncludeOperation, stripPathPrefix, stripPrefix, toCamelCase, toPascalCase, validateFilters };
301
+ /**
302
+ * Configure custom date-time format validation
303
+ * Overrides the default z.iso.datetime() with a custom regex pattern
304
+ *
305
+ * @param pattern - Regex pattern (string or RegExp) for date-time validation
306
+ * @throws {Error} If the provided pattern is not a valid regular expression
307
+ * @example
308
+ * // String pattern (required for JSON/YAML configs)
309
+ * configureDateTimeFormat('^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}$')
310
+ *
311
+ * @example
312
+ * // RegExp literal (TypeScript configs only)
313
+ * configureDateTimeFormat(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/)
314
+ */
315
+ declare function configureDateTimeFormat(pattern?: string | RegExp): void;
316
+ /**
317
+ * Reset format map to defaults (useful for testing)
318
+ */
319
+ declare function resetFormatMap(): void;
320
+
321
+ export { type BaseOperationFilters, type FilterStatistics, type Generator, LRUCache, OperationFiltersSchema, type RequestResponseOptions, RequestResponseOptionsSchema, configureDateTimeFormat, createFilterStatistics, createTypeScriptLoader, escapeJSDoc, executeBatch, formatConfigValidationError, formatFilterStatistics, getBatchExitCode, mergeParameters, resetFormatMap, resolveParameterRef, resolveRef, resolveRequestBodyRef, resolveResponseRef, shouldIncludeOperation, stripPathPrefix, stripPrefix, toCamelCase, toPascalCase, validateFilters };
@@ -1,4 +1,4 @@
1
- import { E as ExecutionMode, d as OperationFilters } from './types-CI48CjiU.js';
1
+ import { E as ExecutionMode, g as OperationFilters, f as OpenAPISpec, b as OpenAPIParameter, c as OpenAPIRequestBody, d as OpenAPIResponse, e as OpenAPISchema } from './types-B3GgqGzM.js';
2
2
  import { z } from 'zod';
3
3
  import { Loader } from 'cosmiconfig';
4
4
 
@@ -59,6 +59,7 @@ declare const RequestResponseOptionsSchema: z.ZodObject<{
59
59
  }>>;
60
60
  useDescribe: z.ZodOptional<z.ZodBoolean>;
61
61
  includeDescriptions: z.ZodOptional<z.ZodBoolean>;
62
+ defaultNullable: z.ZodOptional<z.ZodBoolean>;
62
63
  }, z.core.$strict>;
63
64
  /**
64
65
  * @shared Base Zod schema for operation filters (without status codes)
@@ -225,6 +226,55 @@ declare function stripPrefix(input: string, pattern: string | undefined, ensureL
225
226
  */
226
227
  declare function stripPathPrefix(path: string, pattern: string | undefined): string;
227
228
 
229
+ /**
230
+ * OpenAPI $ref resolution utilities
231
+ *
232
+ * Provides functions to resolve $ref references to component definitions
233
+ * Supports: parameters, requestBodies, responses, schemas
234
+ *
235
+ * @internal Used by core and playwright packages
236
+ */
237
+
238
+ /**
239
+ * Type for any resolvable component
240
+ */
241
+ type ResolvableComponent = OpenAPIParameter | OpenAPIRequestBody | OpenAPIResponse | OpenAPISchema | any;
242
+ /**
243
+ * Resolve a $ref to a component definition
244
+ * Handles nested $refs by recursively resolving until no more refs found
245
+ *
246
+ * @param obj - Object that may contain a $ref
247
+ * @param spec - The OpenAPI specification
248
+ * @param maxDepth - Maximum recursion depth to prevent infinite loops (default: 10)
249
+ * @returns The resolved component, or the original object if not a reference
250
+ */
251
+ declare function resolveRef<T extends ResolvableComponent>(obj: T | any, spec: OpenAPISpec, maxDepth?: number): T;
252
+ /**
253
+ * Resolve a parameter reference
254
+ * Convenience wrapper for resolveRef with parameter type
255
+ */
256
+ declare function resolveParameterRef(param: any, spec: OpenAPISpec): OpenAPIParameter | any;
257
+ /**
258
+ * Resolve a request body reference
259
+ * Convenience wrapper for resolveRef with request body type
260
+ */
261
+ declare function resolveRequestBodyRef(requestBody: any, spec: OpenAPISpec): OpenAPIRequestBody | any;
262
+ /**
263
+ * Resolve a response reference
264
+ * Convenience wrapper for resolveRef with response type
265
+ */
266
+ declare function resolveResponseRef(response: any, spec: OpenAPISpec): OpenAPIResponse | any;
267
+ /**
268
+ * Merge path-level parameters with operation-level parameters
269
+ * Operation parameters override path-level parameters with the same name and location
270
+ *
271
+ * @param pathParams - Parameters defined at the path level
272
+ * @param operationParams - Parameters defined at the operation level
273
+ * @param spec - The OpenAPI specification for resolving $refs
274
+ * @returns Merged array of resolved parameters
275
+ */
276
+ declare function mergeParameters(pathParams: any[] | undefined, operationParams: any[] | undefined, spec: OpenAPISpec): any[];
277
+
228
278
  /**
229
279
  * String utility functions for escaping and formatting
230
280
  */
@@ -248,4 +298,24 @@ declare function escapeJSDoc(str: string): string;
248
298
  */
249
299
  declare function createTypeScriptLoader(): Loader;
250
300
 
251
- export { type BaseOperationFilters, type FilterStatistics, type Generator, LRUCache, OperationFiltersSchema, type RequestResponseOptions, RequestResponseOptionsSchema, createFilterStatistics, createTypeScriptLoader, escapeJSDoc, executeBatch, formatConfigValidationError, formatFilterStatistics, getBatchExitCode, shouldIncludeOperation, stripPathPrefix, stripPrefix, toCamelCase, toPascalCase, validateFilters };
301
+ /**
302
+ * Configure custom date-time format validation
303
+ * Overrides the default z.iso.datetime() with a custom regex pattern
304
+ *
305
+ * @param pattern - Regex pattern (string or RegExp) for date-time validation
306
+ * @throws {Error} If the provided pattern is not a valid regular expression
307
+ * @example
308
+ * // String pattern (required for JSON/YAML configs)
309
+ * configureDateTimeFormat('^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}$')
310
+ *
311
+ * @example
312
+ * // RegExp literal (TypeScript configs only)
313
+ * configureDateTimeFormat(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/)
314
+ */
315
+ declare function configureDateTimeFormat(pattern?: string | RegExp): void;
316
+ /**
317
+ * Reset format map to defaults (useful for testing)
318
+ */
319
+ declare function resetFormatMap(): void;
320
+
321
+ export { type BaseOperationFilters, type FilterStatistics, type Generator, LRUCache, OperationFiltersSchema, type RequestResponseOptions, RequestResponseOptionsSchema, configureDateTimeFormat, createFilterStatistics, createTypeScriptLoader, escapeJSDoc, executeBatch, formatConfigValidationError, formatFilterStatistics, getBatchExitCode, mergeParameters, resetFormatMap, resolveParameterRef, resolveRef, resolveRequestBodyRef, resolveResponseRef, shouldIncludeOperation, stripPathPrefix, stripPrefix, toCamelCase, toPascalCase, validateFilters };
package/dist/internal.js CHANGED
@@ -33,6 +33,7 @@ __export(internal_exports, {
33
33
  LRUCache: () => LRUCache,
34
34
  OperationFiltersSchema: () => OperationFiltersSchema,
35
35
  RequestResponseOptionsSchema: () => RequestResponseOptionsSchema,
36
+ configureDateTimeFormat: () => configureDateTimeFormat,
36
37
  createFilterStatistics: () => createFilterStatistics,
37
38
  createTypeScriptLoader: () => createTypeScriptLoader,
38
39
  escapeJSDoc: () => escapeJSDoc,
@@ -40,6 +41,12 @@ __export(internal_exports, {
40
41
  formatConfigValidationError: () => formatConfigValidationError,
41
42
  formatFilterStatistics: () => formatFilterStatistics,
42
43
  getBatchExitCode: () => getBatchExitCode,
44
+ mergeParameters: () => mergeParameters,
45
+ resetFormatMap: () => resetFormatMap,
46
+ resolveParameterRef: () => resolveParameterRef,
47
+ resolveRef: () => resolveRef,
48
+ resolveRequestBodyRef: () => resolveRequestBodyRef,
49
+ resolveResponseRef: () => resolveResponseRef,
43
50
  shouldIncludeOperation: () => shouldIncludeOperation,
44
51
  stripPathPrefix: () => stripPathPrefix,
45
52
  stripPrefix: () => stripPrefix,
@@ -184,7 +191,8 @@ var import_zod = require("zod");
184
191
  var RequestResponseOptionsSchema = import_zod.z.strictObject({
185
192
  mode: import_zod.z.enum(["strict", "normal", "loose"]).optional(),
186
193
  useDescribe: import_zod.z.boolean().optional(),
187
- includeDescriptions: import_zod.z.boolean().optional()
194
+ includeDescriptions: import_zod.z.boolean().optional(),
195
+ defaultNullable: import_zod.z.boolean().optional()
188
196
  });
189
197
  var OperationFiltersSchema = import_zod.z.strictObject({
190
198
  includeTags: import_zod.z.array(import_zod.z.string()).optional(),
@@ -519,7 +527,69 @@ function stripPathPrefix(path, pattern) {
519
527
  return stripPrefix(path, pattern, "/");
520
528
  }
521
529
 
530
+ // src/utils/ref-resolver.ts
531
+ function resolveRef(obj, spec, maxDepth = 10) {
532
+ var _a, _b, _c, _d;
533
+ if (!obj || typeof obj !== "object" || maxDepth <= 0) return obj;
534
+ if (!obj.$ref) return obj;
535
+ const ref = obj.$ref;
536
+ let resolved = null;
537
+ const paramMatch = ref.match(/^#\/components\/parameters\/(.+)$/);
538
+ const requestBodyMatch = ref.match(/^#\/components\/requestBodies\/(.+)$/);
539
+ const responseMatch = ref.match(/^#\/components\/responses\/(.+)$/);
540
+ const schemaMatch = ref.match(/^#\/components\/schemas\/(.+)$/);
541
+ if (paramMatch && ((_a = spec.components) == null ? void 0 : _a.parameters)) {
542
+ const name = paramMatch[1];
543
+ resolved = spec.components.parameters[name];
544
+ } else if (requestBodyMatch && ((_b = spec.components) == null ? void 0 : _b.requestBodies)) {
545
+ const name = requestBodyMatch[1];
546
+ resolved = spec.components.requestBodies[name];
547
+ } else if (responseMatch && ((_c = spec.components) == null ? void 0 : _c.responses)) {
548
+ const name = responseMatch[1];
549
+ resolved = spec.components.responses[name];
550
+ } else if (schemaMatch && ((_d = spec.components) == null ? void 0 : _d.schemas)) {
551
+ const name = schemaMatch[1];
552
+ resolved = spec.components.schemas[name];
553
+ }
554
+ if (resolved) {
555
+ if (resolved.$ref) {
556
+ return resolveRef(resolved, spec, maxDepth - 1);
557
+ }
558
+ return resolved;
559
+ }
560
+ return obj;
561
+ }
562
+ function resolveParameterRef(param, spec) {
563
+ return resolveRef(param, spec);
564
+ }
565
+ function resolveRequestBodyRef(requestBody, spec) {
566
+ return resolveRef(requestBody, spec);
567
+ }
568
+ function resolveResponseRef(response, spec) {
569
+ return resolveRef(response, spec);
570
+ }
571
+ function mergeParameters(pathParams, operationParams, spec) {
572
+ const resolvedPathParams = (pathParams || []).map((p) => resolveParameterRef(p, spec));
573
+ const resolvedOperationParams = (operationParams || []).map((p) => resolveParameterRef(p, spec));
574
+ const merged = [...resolvedPathParams];
575
+ for (const opParam of resolvedOperationParams) {
576
+ if (!opParam || typeof opParam !== "object") continue;
577
+ const existingIndex = merged.findIndex(
578
+ (p) => p && typeof p === "object" && p.name === opParam.name && p.in === opParam.in
579
+ );
580
+ if (existingIndex >= 0) {
581
+ merged[existingIndex] = opParam;
582
+ } else {
583
+ merged.push(opParam);
584
+ }
585
+ }
586
+ return merged;
587
+ }
588
+
522
589
  // src/utils/string-utils.ts
590
+ function escapePattern(str) {
591
+ return str.replace(/\//g, "\\/");
592
+ }
523
593
  function escapeJSDoc(str) {
524
594
  return str.replace(/\*\//g, "*\\/");
525
595
  }
@@ -557,11 +627,73 @@ function createTypeScriptLoader() {
557
627
  }
558
628
  };
559
629
  }
630
+
631
+ // src/validators/string-validator.ts
632
+ var PATTERN_CACHE = new LRUCache(1e3);
633
+ var DEFAULT_FORMAT_MAP = {
634
+ uuid: "z.uuid()",
635
+ email: "z.email()",
636
+ uri: "z.url()",
637
+ url: "z.url()",
638
+ "uri-reference": 'z.string().refine((val) => !/\\s/.test(val), { message: "Must be a valid URI reference" })',
639
+ hostname: 'z.string().refine((val) => /^(?=.{1,253}$)(?:(?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)*(?!-)[A-Za-z0-9-]{1,63}(?<!-)$/.test(val), { message: "Must be a valid hostname" })',
640
+ byte: "z.base64()",
641
+ binary: "z.string()",
642
+ date: "z.iso.date()",
643
+ time: "z.iso.time()",
644
+ duration: 'z.string().refine((val) => /^P(?:(?:\\d+Y)?(?:\\d+M)?(?:\\d+D)?(?:T(?:\\d+H)?(?:\\d+M)?(?:\\d+(?:\\.\\d+)?S)?)?|\\d+W)$/.test(val) && !/^PT?$/.test(val), { message: "Must be a valid ISO 8601 duration" })',
645
+ ipv4: "z.ipv4()",
646
+ ipv6: "z.ipv6()",
647
+ emoji: "z.emoji()",
648
+ base64: "z.base64()",
649
+ base64url: "z.base64url()",
650
+ nanoid: "z.nanoid()",
651
+ cuid: "z.cuid()",
652
+ cuid2: "z.cuid2()",
653
+ ulid: "z.ulid()",
654
+ cidr: "z.cidrv4()",
655
+ // Default to v4
656
+ cidrv4: "z.cidrv4()",
657
+ cidrv6: "z.cidrv6()",
658
+ "json-pointer": 'z.string().refine((val) => val === "" || /^(\\/([^~/]|~0|~1)+)+$/.test(val), { message: "Must be a valid JSON Pointer (RFC 6901)" })',
659
+ "relative-json-pointer": 'z.string().refine((val) => /^(0|[1-9]\\d*)(#|(\\/([^~/]|~0|~1)+)*)$/.test(val), { message: "Must be a valid relative JSON Pointer" })'
660
+ };
661
+ var FORMAT_MAP = {
662
+ ...DEFAULT_FORMAT_MAP,
663
+ "date-time": "z.iso.datetime()"
664
+ };
665
+ function configureDateTimeFormat(pattern) {
666
+ if (!pattern) {
667
+ FORMAT_MAP["date-time"] = "z.iso.datetime()";
668
+ return;
669
+ }
670
+ const patternStr = pattern instanceof RegExp ? pattern.source : pattern;
671
+ if (patternStr === "") {
672
+ FORMAT_MAP["date-time"] = "z.iso.datetime()";
673
+ return;
674
+ }
675
+ try {
676
+ new RegExp(patternStr);
677
+ } catch (error) {
678
+ throw new Error(
679
+ `Invalid regular expression pattern for customDateTimeFormatRegex: ${patternStr}. ${error instanceof Error ? error.message : "Pattern is malformed"}`
680
+ );
681
+ }
682
+ const escapedPattern = escapePattern(patternStr);
683
+ FORMAT_MAP["date-time"] = `z.string().regex(/${escapedPattern}/)`;
684
+ }
685
+ function resetFormatMap() {
686
+ FORMAT_MAP = {
687
+ ...DEFAULT_FORMAT_MAP,
688
+ "date-time": "z.iso.datetime()"
689
+ };
690
+ }
560
691
  // Annotate the CommonJS export names for ESM import in node:
561
692
  0 && (module.exports = {
562
693
  LRUCache,
563
694
  OperationFiltersSchema,
564
695
  RequestResponseOptionsSchema,
696
+ configureDateTimeFormat,
565
697
  createFilterStatistics,
566
698
  createTypeScriptLoader,
567
699
  escapeJSDoc,
@@ -569,6 +701,12 @@ function createTypeScriptLoader() {
569
701
  formatConfigValidationError,
570
702
  formatFilterStatistics,
571
703
  getBatchExitCode,
704
+ mergeParameters,
705
+ resetFormatMap,
706
+ resolveParameterRef,
707
+ resolveRef,
708
+ resolveRequestBodyRef,
709
+ resolveResponseRef,
572
710
  shouldIncludeOperation,
573
711
  stripPathPrefix,
574
712
  stripPrefix,