@cerios/openapi-to-zod 1.4.0 → 1.5.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/LICENSE +8 -0
- package/README.md +546 -395
- package/dist/cli.js +806 -1293
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +839 -1343
- package/dist/cli.mjs.map +1 -1
- package/dist/index.d.mts +468 -59
- package/dist/index.d.ts +468 -59
- package/dist/index.js +644 -881
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +650 -856
- package/dist/index.mjs.map +1 -1
- package/package.json +89 -104
- package/dist/internal.d.mts +0 -470
- package/dist/internal.d.ts +0 -470
- package/dist/internal.js +0 -2025
- package/dist/internal.js.map +0 -1
- package/dist/internal.mjs +0 -1972
- package/dist/internal.mjs.map +0 -1
- package/dist/types-DZ4Bw-D5.d.mts +0 -505
- package/dist/types-DZ4Bw-D5.d.ts +0 -505
package/dist/index.d.mts
CHANGED
|
@@ -1,56 +1,416 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
1
|
+
import { BaseGeneratorOptions, ExecutionMode, OpenAPISchema, OpenAPISpec, NamingOptions, LRUCache } from '@cerios/openapi-core';
|
|
2
|
+
export { CircularReferenceError, CliOptionsError, ConfigValidationError, ExecutionMode, FileOperationError, GeneratorError, OpenAPIParameter, OpenAPIRequestBody, OpenAPIResponse, OpenAPISchema, OpenAPISpec, OperationFilters, SchemaGenerationError, SpecValidationError } from '@cerios/openapi-core';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* Re-export core types from @cerios/openapi-core
|
|
6
6
|
*/
|
|
7
|
+
|
|
7
8
|
/**
|
|
8
|
-
*
|
|
9
|
+
* Common options shared by both request and response contexts
|
|
9
10
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
interface CommonSchemaOptions {
|
|
12
|
+
/**
|
|
13
|
+
* Object validation mode
|
|
14
|
+
* - 'strict': Uses z.strictObject() - no additional properties allowed
|
|
15
|
+
* - 'normal': Uses z.object() - additional properties allowed
|
|
16
|
+
* - 'loose': Uses z.looseObject() - explicitly allows additional properties
|
|
17
|
+
*/
|
|
18
|
+
mode?: "strict" | "normal" | "loose";
|
|
19
|
+
/**
|
|
20
|
+
* Whether to add .describe() calls for better error messages
|
|
21
|
+
* @default false
|
|
22
|
+
*/
|
|
23
|
+
useDescribe?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Whether to include descriptions as JSDoc comments
|
|
26
|
+
*/
|
|
27
|
+
includeDescriptions?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Default nullable behavior when not explicitly specified in the schema
|
|
30
|
+
*
|
|
31
|
+
* When true: Properties without explicit nullable annotation are treated as nullable.
|
|
32
|
+
* This follows the industry de facto standard for OpenAPI 3.0.x where tooling convergence
|
|
33
|
+
* made "nullable by default" the safest assumption.
|
|
34
|
+
*
|
|
35
|
+
* When false (default): Properties are only nullable when explicitly marked with `nullable: true`
|
|
36
|
+
* (OpenAPI 3.0) or `type: ["string", "null"]` (OpenAPI 3.1).
|
|
37
|
+
*
|
|
38
|
+
* @default false
|
|
39
|
+
*/
|
|
40
|
+
defaultNullable?: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Behavior for empty object schemas (objects with no properties defined)
|
|
43
|
+
*
|
|
44
|
+
* - 'strict': Uses z.strictObject({}) - no additional properties allowed
|
|
45
|
+
* - 'loose': Uses z.looseObject({}) - explicitly allows additional properties (Zod v4)
|
|
46
|
+
* - 'record': Uses z.record(z.string(), z.unknown()) - treat as arbitrary key-value map
|
|
47
|
+
*
|
|
48
|
+
* Note: This option controls nested/property-level empty objects.
|
|
49
|
+
* The top-level `mode` option controls how schema definitions are wrapped.
|
|
50
|
+
*
|
|
51
|
+
* @default 'loose'
|
|
52
|
+
*/
|
|
53
|
+
emptyObjectBehavior?: "strict" | "loose" | "record";
|
|
14
54
|
}
|
|
15
55
|
/**
|
|
16
|
-
*
|
|
56
|
+
* Request-specific options that can override root-level options
|
|
17
57
|
*/
|
|
18
|
-
|
|
19
|
-
constructor(message: string, context?: Record<string, unknown>);
|
|
58
|
+
interface RequestOptions extends CommonSchemaOptions {
|
|
20
59
|
}
|
|
21
60
|
/**
|
|
22
|
-
*
|
|
61
|
+
* Response-specific options that can override root-level options
|
|
23
62
|
*/
|
|
24
|
-
|
|
25
|
-
readonly filePath: string;
|
|
26
|
-
constructor(message: string, filePath: string, context?: Record<string, unknown>);
|
|
63
|
+
interface ResponseOptions extends CommonSchemaOptions {
|
|
27
64
|
}
|
|
28
65
|
/**
|
|
29
|
-
*
|
|
66
|
+
* Options for Zod schema generation from OpenAPI specifications
|
|
67
|
+
*
|
|
68
|
+
* Extends BaseGeneratorOptions from @cerios/openapi-core with Zod-specific options.
|
|
30
69
|
*/
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
70
|
+
interface OpenApiGeneratorOptions extends BaseGeneratorOptions {
|
|
71
|
+
/**
|
|
72
|
+
* Object validation mode
|
|
73
|
+
* - 'strict': Uses z.strictObject() - no additional properties allowed
|
|
74
|
+
* - 'normal': Uses z.object() - additional properties allowed
|
|
75
|
+
* - 'loose': Uses z.looseObject() - explicitly allows additional properties
|
|
76
|
+
*/
|
|
77
|
+
mode?: "strict" | "normal" | "loose";
|
|
78
|
+
/**
|
|
79
|
+
* Whether to add .describe() calls for better error messages
|
|
80
|
+
* @default false
|
|
81
|
+
*/
|
|
82
|
+
useDescribe?: boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Behavior for empty object schemas (objects with no properties defined)
|
|
85
|
+
*
|
|
86
|
+
* - 'strict': Uses z.strictObject({}) - no additional properties allowed
|
|
87
|
+
* - 'loose': Uses z.looseObject({}) - explicitly allows additional properties (Zod v4)
|
|
88
|
+
* - 'record': Uses z.record(z.string(), z.unknown()) - treat as arbitrary key-value map
|
|
89
|
+
*
|
|
90
|
+
* Note: This option controls nested/property-level empty objects.
|
|
91
|
+
* The top-level `mode` option controls how schema definitions are wrapped.
|
|
92
|
+
*
|
|
93
|
+
* @default 'loose'
|
|
94
|
+
*/
|
|
95
|
+
emptyObjectBehavior?: "strict" | "loose" | "record";
|
|
96
|
+
/**
|
|
97
|
+
* Schema filtering mode
|
|
98
|
+
* - 'all': Generate all schemas (default)
|
|
99
|
+
* - 'request': Only include schemas suitable for requests (excludes readOnly)
|
|
100
|
+
* - 'response': Only include schemas suitable for responses (excludes writeOnly)
|
|
101
|
+
*/
|
|
102
|
+
schemaType?: "all" | "request" | "response";
|
|
103
|
+
/**
|
|
104
|
+
* Fallback parsing method for unknown or missing content types
|
|
105
|
+
*
|
|
106
|
+
* When a content type is not recognized, this determines how the response is parsed:
|
|
107
|
+
* - "text": Use response.text() - safest, always succeeds (default)
|
|
108
|
+
* - "json": Use response.json() - may throw if response isn't valid JSON
|
|
109
|
+
* - "body": Use response.body() - returns raw Buffer
|
|
110
|
+
*
|
|
111
|
+
* A warning will be logged during generation when an unknown content type is encountered.
|
|
112
|
+
*
|
|
113
|
+
* @default "text"
|
|
114
|
+
*/
|
|
115
|
+
fallbackContentTypeParsing?: "text" | "json" | "body";
|
|
116
|
+
/**
|
|
117
|
+
* Request-specific options that override root-level options
|
|
118
|
+
* Applied when schemas are used in request contexts
|
|
119
|
+
*/
|
|
120
|
+
request?: RequestOptions;
|
|
121
|
+
/**
|
|
122
|
+
* Response-specific options that override root-level options
|
|
123
|
+
* Applied when schemas are used in response contexts
|
|
124
|
+
*/
|
|
125
|
+
response?: ResponseOptions;
|
|
126
|
+
/**
|
|
127
|
+
* Header parameters to ignore during schema generation
|
|
128
|
+
* Supports glob patterns for flexible matching
|
|
129
|
+
* Case-insensitive matching (HTTP header semantics)
|
|
130
|
+
*
|
|
131
|
+
* @internal Used by Playwright generator
|
|
132
|
+
*/
|
|
133
|
+
ignoreHeaders?: string[];
|
|
134
|
+
/**
|
|
135
|
+
* Cache size for pattern regex compilation
|
|
136
|
+
* Higher values improve performance for large specifications with many string patterns
|
|
137
|
+
* @default 1000
|
|
138
|
+
*/
|
|
139
|
+
cacheSize?: number;
|
|
140
|
+
/**
|
|
141
|
+
* Custom regex pattern for date-time format validation
|
|
142
|
+
* Overrides the default z.iso.datetime() which requires ISO 8601 format with timezone suffix (Z)
|
|
143
|
+
*
|
|
144
|
+
* **Config File Formats:**
|
|
145
|
+
* - JSON/YAML configs: Must use string pattern (requires double-escaping: `\\d`)
|
|
146
|
+
* - TypeScript configs: Can use either string pattern OR RegExp literal (`/\d/`)
|
|
147
|
+
*
|
|
148
|
+
* **Common Patterns:**
|
|
149
|
+
* ```typescript
|
|
150
|
+
* // No timezone suffix (e.g., "2026-01-07T14:30:00")
|
|
151
|
+
* customDateTimeFormatRegex: '^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}$'
|
|
152
|
+
* // OR in TypeScript config:
|
|
153
|
+
* customDateTimeFormatRegex: /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/
|
|
154
|
+
*
|
|
155
|
+
* // With milliseconds, no Z (e.g., "2026-01-07T14:30:00.123")
|
|
156
|
+
* customDateTimeFormatRegex: '^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}$'
|
|
157
|
+
*
|
|
158
|
+
* // Optional Z suffix (e.g., "2026-01-07T14:30:00" or "2026-01-07T14:30:00Z")
|
|
159
|
+
* customDateTimeFormatRegex: '^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z?$'
|
|
160
|
+
* ```
|
|
161
|
+
*
|
|
162
|
+
* @default "z.iso.datetime()" (requires Z suffix per ISO 8601)
|
|
163
|
+
*/
|
|
164
|
+
customDateTimeFormatRegex?: string | RegExp;
|
|
165
|
+
/**
|
|
166
|
+
* Output path for Zod schemas when using separate type/schema files.
|
|
167
|
+
*
|
|
168
|
+
* When specified:
|
|
169
|
+
* - TypeScript types are generated to `outputTypes` (using @cerios/openapi-to-typescript)
|
|
170
|
+
* - Zod schemas are generated to `outputZodSchemas` with explicit type annotations
|
|
171
|
+
*
|
|
172
|
+
* This approach solves "Type instantiation is excessively deep" errors that occur
|
|
173
|
+
* with very large or deeply nested schemas when using `z.infer<typeof schema>`.
|
|
174
|
+
*
|
|
175
|
+
* Instead of generating:
|
|
176
|
+
* ```typescript
|
|
177
|
+
* export const userSchema = z.object({ ... });
|
|
178
|
+
* export type User = z.infer<typeof userSchema>; // Can cause TS errors
|
|
179
|
+
* ```
|
|
180
|
+
*
|
|
181
|
+
* Generates two files:
|
|
182
|
+
* ```typescript
|
|
183
|
+
* // types.ts
|
|
184
|
+
* export type User = { ... };
|
|
185
|
+
*
|
|
186
|
+
* // schemas.ts
|
|
187
|
+
* import type { User } from './types';
|
|
188
|
+
* export const userSchema: z.ZodType<User> = z.object({ ... });
|
|
189
|
+
* ```
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* ```typescript
|
|
193
|
+
* {
|
|
194
|
+
* input: 'openapi.yaml',
|
|
195
|
+
* outputTypes: 'src/generated/types.ts',
|
|
196
|
+
* outputZodSchemas: 'src/generated/schemas.ts'
|
|
197
|
+
* }
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
outputZodSchemas?: string;
|
|
201
|
+
/**
|
|
202
|
+
* Format for generating enums in TypeScript types (when using outputZodSchemas)
|
|
203
|
+
* - 'union': Generate union of string literals
|
|
204
|
+
* - 'const-object': Generate const object with derived type (default)
|
|
205
|
+
*
|
|
206
|
+
* This option is passed to @cerios/openapi-to-typescript when generating types.
|
|
207
|
+
*
|
|
208
|
+
* @default 'const-object'
|
|
209
|
+
*/
|
|
210
|
+
enumFormat?: "union" | "const-object";
|
|
211
|
+
/**
|
|
212
|
+
* Complexity threshold for switching from type annotation (`:`) to double assertion (`as unknown as`)
|
|
213
|
+
* in generated z.ZodType<T> declarations (only applies when outputZodSchemas is used).
|
|
214
|
+
*
|
|
215
|
+
* - When not set or 0: Always use annotation syntax `: z.ZodType<T>`
|
|
216
|
+
* - When set to a positive number: Use double assertion `as unknown as z.ZodType<T>` for schemas
|
|
217
|
+
* with complexity >= threshold, annotation syntax for simpler schemas
|
|
218
|
+
*
|
|
219
|
+
* Type annotation (`:`) provides full TypeScript type checking but can cause
|
|
220
|
+
* "Type instantiation is excessively deep" errors on very large/complex schemas.
|
|
221
|
+
* Double assertion via `unknown` completely bypasses TypeScript's structural checking,
|
|
222
|
+
* ensuring compilation even for extremely large schemas.
|
|
223
|
+
*
|
|
224
|
+
* Complexity is calculated as: properties + (nested levels * 10) + (array/union members * 2)
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* // Always use annotation (default, safest)
|
|
228
|
+
* typeAssertionThreshold: 0
|
|
229
|
+
*
|
|
230
|
+
* // Use double assertion for complex schemas (when experiencing TS depth errors)
|
|
231
|
+
* typeAssertionThreshold: 100
|
|
232
|
+
*
|
|
233
|
+
* @default 0 (always use annotation)
|
|
234
|
+
*/
|
|
235
|
+
typeAssertionThreshold?: number;
|
|
34
236
|
}
|
|
35
237
|
/**
|
|
36
|
-
*
|
|
238
|
+
* Root configuration file structure
|
|
37
239
|
*/
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
240
|
+
interface ConfigFile {
|
|
241
|
+
/**
|
|
242
|
+
* Global default options applied to all specifications
|
|
243
|
+
* Can be overridden by individual specification configurations
|
|
244
|
+
*/
|
|
245
|
+
defaults?: Partial<Omit<OpenApiGeneratorOptions, "input" | "outputTypes" | "outputZodSchemas">>;
|
|
246
|
+
/**
|
|
247
|
+
* Array of OpenAPI specifications to process
|
|
248
|
+
* Each specification must provide `input` and at least one of:
|
|
249
|
+
* - `outputTypes` (preferred) - generates combined types+schemas OR just types (when outputZodSchemas is used)
|
|
250
|
+
* - `output` (deprecated alias for outputTypes)
|
|
251
|
+
* - `outputZodSchemas` (optional) - when specified, schemas go here with z.ZodType<TypeAlias> syntax
|
|
252
|
+
*/
|
|
253
|
+
specs: (Omit<OpenApiGeneratorOptions, "outputTypes"> & {
|
|
254
|
+
outputTypes?: string;
|
|
255
|
+
/**
|
|
256
|
+
* @deprecated Use `outputTypes` instead.
|
|
257
|
+
*/
|
|
258
|
+
output?: string;
|
|
259
|
+
})[];
|
|
260
|
+
/**
|
|
261
|
+
* Execution mode for batch processing
|
|
262
|
+
* @default "parallel"
|
|
263
|
+
*/
|
|
264
|
+
executionMode?: ExecutionMode;
|
|
41
265
|
}
|
|
42
266
|
/**
|
|
43
|
-
*
|
|
267
|
+
* Helper function for type-safe config file creation
|
|
268
|
+
* Provides IDE autocomplete and type checking for config files
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* ```typescript
|
|
272
|
+
* import { defineConfig } from '@cerios/openapi-to-zod';
|
|
273
|
+
*
|
|
274
|
+
* export default defineConfig({
|
|
275
|
+
* defaults: {
|
|
276
|
+
* mode: 'strict',
|
|
277
|
+
* includeDescriptions: true
|
|
278
|
+
* },
|
|
279
|
+
* specs: [
|
|
280
|
+
* { input: 'api-v1.yaml', outputTypes: 'schemas/v1.ts' },
|
|
281
|
+
* { input: 'api-v2.yaml', outputTypes: 'schemas/v2.ts', mode: 'normal' }
|
|
282
|
+
* ]
|
|
283
|
+
* });
|
|
284
|
+
* ```
|
|
44
285
|
*/
|
|
45
|
-
declare
|
|
46
|
-
|
|
47
|
-
|
|
286
|
+
declare function defineConfig(config: ConfigFile): ConfigFile;
|
|
287
|
+
|
|
288
|
+
type ObjectMode = "strict" | "normal" | "loose";
|
|
289
|
+
|
|
290
|
+
interface PropertyGeneratorContext {
|
|
291
|
+
spec: OpenAPISpec;
|
|
292
|
+
schemaDependencies: Map<string, Set<string>>;
|
|
293
|
+
schemaType: "all" | "request" | "response";
|
|
294
|
+
mode: ObjectMode;
|
|
295
|
+
includeDescriptions: boolean;
|
|
296
|
+
useDescribe: boolean;
|
|
297
|
+
namingOptions: NamingOptions;
|
|
298
|
+
stripSchemaPrefix?: string | string[];
|
|
299
|
+
/**
|
|
300
|
+
* Default nullable behavior when not explicitly specified
|
|
301
|
+
* @default false
|
|
302
|
+
*/
|
|
303
|
+
defaultNullable: boolean;
|
|
304
|
+
/**
|
|
305
|
+
* Behavior for empty object schemas (objects with no properties defined)
|
|
306
|
+
* @default 'loose'
|
|
307
|
+
*/
|
|
308
|
+
emptyObjectBehavior: "strict" | "loose" | "record";
|
|
309
|
+
/**
|
|
310
|
+
* Zod validation string for date-time format fields
|
|
311
|
+
* @default "z.iso.datetime()"
|
|
312
|
+
*/
|
|
313
|
+
dateTimeValidation: string;
|
|
314
|
+
/**
|
|
315
|
+
* Instance-level cache for escaped regex patterns (parallel-safe)
|
|
316
|
+
*/
|
|
317
|
+
patternCache: LRUCache<string, string>;
|
|
318
|
+
/**
|
|
319
|
+
* Whether types are generated in a separate file (imported) vs inline (z.infer)
|
|
320
|
+
* When true, z.lazy uses z.ZodType<TypeName> for proper type inference
|
|
321
|
+
* When false, z.lazy uses z.ZodTypeAny to avoid circular type references
|
|
322
|
+
* @default false
|
|
323
|
+
*/
|
|
324
|
+
separateTypesFile: boolean;
|
|
48
325
|
}
|
|
49
326
|
/**
|
|
50
|
-
*
|
|
327
|
+
* Property schema generator with memoization for performance
|
|
51
328
|
*/
|
|
52
|
-
declare class
|
|
53
|
-
|
|
329
|
+
declare class PropertyGenerator {
|
|
330
|
+
private context;
|
|
331
|
+
private filteredPropsCache;
|
|
332
|
+
private schemaCache;
|
|
333
|
+
private allOfConflicts;
|
|
334
|
+
private circularDependencies;
|
|
335
|
+
static readonly INCLUSION_RULES: {
|
|
336
|
+
readonly request: (schema: OpenAPISchema) => boolean;
|
|
337
|
+
readonly response: (schema: OpenAPISchema) => boolean;
|
|
338
|
+
readonly all: () => boolean;
|
|
339
|
+
};
|
|
340
|
+
constructor(context: PropertyGeneratorContext);
|
|
341
|
+
/**
|
|
342
|
+
* Set the schemas that are involved in circular dependency chains.
|
|
343
|
+
* These schemas will use z.lazy() for forward references.
|
|
344
|
+
*/
|
|
345
|
+
setCircularDependencies(deps: Set<string>): void;
|
|
346
|
+
/**
|
|
347
|
+
* Get allOf conflicts detected during the last schema generation
|
|
348
|
+
* @returns Array of conflict description strings
|
|
349
|
+
*/
|
|
350
|
+
getAllOfConflicts(): string[];
|
|
351
|
+
/**
|
|
352
|
+
* Clear tracked allOf conflicts (call before generating a new schema)
|
|
353
|
+
*/
|
|
354
|
+
clearAllOfConflicts(): void;
|
|
355
|
+
/**
|
|
356
|
+
* Check if a property should be included based on schemaType and readOnly/writeOnly flags
|
|
357
|
+
*/
|
|
358
|
+
shouldIncludeProperty(schema: OpenAPISchema): boolean;
|
|
359
|
+
/**
|
|
360
|
+
* Recursively filter any schema type (helper for composition schemas)
|
|
361
|
+
*/
|
|
362
|
+
private filterSchemaRecursive;
|
|
363
|
+
/**
|
|
364
|
+
* Recursively filter properties in nested objects based on readOnly/writeOnly
|
|
365
|
+
* Performance optimized with memoization
|
|
366
|
+
*/
|
|
367
|
+
private filterNestedProperties;
|
|
368
|
+
/**
|
|
369
|
+
* Resolve discriminator mapping to actual schema references
|
|
370
|
+
*/
|
|
371
|
+
private resolveDiscriminatorMapping;
|
|
372
|
+
/**
|
|
373
|
+
* Resolve a $ref string to the actual schema
|
|
374
|
+
*/
|
|
375
|
+
private resolveSchemaRef;
|
|
376
|
+
/**
|
|
377
|
+
* Resolve a schema name through any aliases to get the actual schema name
|
|
378
|
+
* If the schema is an alias (allOf with single $ref), return the target name
|
|
379
|
+
*/
|
|
380
|
+
private resolveSchemaAlias;
|
|
381
|
+
/**
|
|
382
|
+
* Check if this is a circular dependency through aliases
|
|
383
|
+
*/
|
|
384
|
+
private isCircularThroughAlias;
|
|
385
|
+
/**
|
|
386
|
+
* Generate union for multiple types (OpenAPI 3.1)
|
|
387
|
+
*/
|
|
388
|
+
private generateMultiTypeUnion;
|
|
389
|
+
/**
|
|
390
|
+
* Apply unevaluatedProperties validation to a schema
|
|
391
|
+
*/
|
|
392
|
+
private applyUnevaluatedProperties;
|
|
393
|
+
/**
|
|
394
|
+
* Generate Zod schema for a property
|
|
395
|
+
* @param schema - The OpenAPI schema to generate
|
|
396
|
+
* @param currentSchema - The name of the current schema being processed (for circular ref detection)
|
|
397
|
+
* @param isTopLevel - Whether this is a top-level schema definition
|
|
398
|
+
* @param suppressDefaultNullable - When true, don't apply defaultNullable (used when outer schema has explicit nullable: false)
|
|
399
|
+
*/
|
|
400
|
+
generatePropertySchema(schema: OpenAPISchema, currentSchema?: string, isTopLevel?: boolean, suppressDefaultNullable?: boolean): string;
|
|
401
|
+
/**
|
|
402
|
+
* Generate inline object shape for use with .extend()
|
|
403
|
+
* Returns just the shape object literal: { prop1: z.string(), prop2: z.number() }
|
|
404
|
+
*
|
|
405
|
+
* This method is specifically for allOf compositions where we need to pass
|
|
406
|
+
* the shape directly to .extend() instead of using z.object({...}).shape.
|
|
407
|
+
* This avoids the .nullable().shape bug when inline objects have nullable: true.
|
|
408
|
+
*
|
|
409
|
+
* According to Zod docs (https://zod.dev/api?id=extend):
|
|
410
|
+
* - .extend() accepts an object of shape definitions
|
|
411
|
+
* - e.g., baseSchema.extend({ prop: z.string() })
|
|
412
|
+
*/
|
|
413
|
+
generateInlineObjectShape(schema: OpenAPISchema, currentSchema?: string): string;
|
|
54
414
|
}
|
|
55
415
|
|
|
56
416
|
declare class OpenApiGenerator {
|
|
@@ -69,9 +429,16 @@ declare class OpenApiGenerator {
|
|
|
69
429
|
private patternCache;
|
|
70
430
|
/** Instance-level date-time validation string for parallel-safe execution */
|
|
71
431
|
private dateTimeValidation;
|
|
432
|
+
/** Track total allOf conflicts detected across all schemas */
|
|
433
|
+
private allOfConflictCount;
|
|
434
|
+
/** Track schemas involved in circular dependency chains */
|
|
435
|
+
private circularDependencies;
|
|
436
|
+
/** Separate schemas mode - when outputZodSchemas is specified */
|
|
437
|
+
private separateSchemasMode;
|
|
72
438
|
constructor(options: OpenApiGeneratorOptions);
|
|
73
439
|
/**
|
|
74
440
|
* Generate schemas as a string (without writing to file)
|
|
441
|
+
* When separateSchemasMode is active, generates Zod schemas with explicit type annotations
|
|
75
442
|
* @returns The generated TypeScript code as a string
|
|
76
443
|
*/
|
|
77
444
|
generateString(): string;
|
|
@@ -80,39 +447,59 @@ declare class OpenApiGenerator {
|
|
|
80
447
|
*/
|
|
81
448
|
private ensureDirectoryExists;
|
|
82
449
|
/**
|
|
83
|
-
* Generate the complete output file
|
|
450
|
+
* Generate the complete output file(s)
|
|
451
|
+
* When separateSchemasMode is active, generates both types and schemas files
|
|
84
452
|
*/
|
|
85
453
|
generate(): void;
|
|
86
454
|
/**
|
|
87
|
-
*
|
|
88
|
-
*
|
|
455
|
+
* Generate Zod schemas with explicit type annotations (for outputZodSchemas mode)
|
|
456
|
+
* Generates schemas like: `export const userSchema: z.ZodType<User> = z.object({...})`
|
|
457
|
+
* @returns The generated Zod schemas TypeScript code
|
|
89
458
|
*/
|
|
90
|
-
private
|
|
459
|
+
private generateSeparateSchemasString;
|
|
460
|
+
/**
|
|
461
|
+
* Generate TypeScript types as a string (for outputZodSchemas mode)
|
|
462
|
+
* Uses @cerios/openapi-to-typescript internally
|
|
463
|
+
* @returns The generated TypeScript types code
|
|
464
|
+
*/
|
|
465
|
+
generateTypesString(): string;
|
|
91
466
|
/**
|
|
92
|
-
*
|
|
93
|
-
*
|
|
467
|
+
* Add explicit type annotation to a schema declaration
|
|
468
|
+
* Transforms: `export const userSchema = z.object({...})`
|
|
469
|
+
* To: `export const userSchema: z.ZodType<User> = z.object({...})` (annotation)
|
|
470
|
+
* Or: `export const userSchema = z.object({...}) as unknown as z.ZodType<User>` (double assertion)
|
|
471
|
+
*
|
|
472
|
+
* Uses double assertion via `unknown` when typeAssertionThreshold is set and schema complexity
|
|
473
|
+
* meets or exceeds the threshold. This completely bypasses TypeScript's structural checking
|
|
474
|
+
* to avoid "Type instantiation is excessively deep" errors on very large schemas.
|
|
475
|
+
*
|
|
476
|
+
* Also removes any `export type X = z.infer<...>` lines since types
|
|
477
|
+
* are imported from the separate types file.
|
|
94
478
|
*/
|
|
95
|
-
private
|
|
479
|
+
private addExplicitTypeAnnotation;
|
|
96
480
|
/**
|
|
97
|
-
*
|
|
481
|
+
* Type guard to check if a value is a Record<string, unknown>
|
|
98
482
|
*/
|
|
99
|
-
private
|
|
483
|
+
private isRecordObject;
|
|
100
484
|
/**
|
|
101
|
-
*
|
|
485
|
+
* Calculate the complexity of a schema for threshold comparison
|
|
486
|
+
* Complexity formula: properties + (nested levels * 10) + (array/union members * 2)
|
|
102
487
|
*/
|
|
103
|
-
private
|
|
488
|
+
private calculateSchemaComplexity;
|
|
104
489
|
/**
|
|
105
|
-
*
|
|
490
|
+
* Calculate relative import path from schema file to types file
|
|
106
491
|
*/
|
|
107
|
-
private
|
|
492
|
+
private calculateRelativeImportPath;
|
|
108
493
|
/**
|
|
109
|
-
*
|
|
494
|
+
* Resolve options for a specific context (request or response)
|
|
495
|
+
* Nested options silently override root-level options
|
|
110
496
|
*/
|
|
111
|
-
private
|
|
497
|
+
private resolveOptionsForContext;
|
|
112
498
|
/**
|
|
113
|
-
*
|
|
499
|
+
* Initialize schema usage map using core utilities with operation filtering
|
|
500
|
+
* This is a wrapper around core's analyzeSchemaUsage that adds operation filtering
|
|
114
501
|
*/
|
|
115
|
-
private
|
|
502
|
+
private initializeSchemaUsage;
|
|
116
503
|
/**
|
|
117
504
|
* Validate the OpenAPI specification
|
|
118
505
|
*/
|
|
@@ -129,17 +516,6 @@ declare class OpenApiGenerator {
|
|
|
129
516
|
* Generate query parameter schemas for each operation
|
|
130
517
|
*/
|
|
131
518
|
private generateQueryParameterSchemas;
|
|
132
|
-
/**
|
|
133
|
-
* Generate a PascalCase method name from HTTP method and path
|
|
134
|
-
* Used as fallback when operationId is not available
|
|
135
|
-
* @internal
|
|
136
|
-
*/
|
|
137
|
-
private generateMethodNameFromPath;
|
|
138
|
-
/**
|
|
139
|
-
* Capitalizes a path segment, handling special characters like dashes, underscores, and dots
|
|
140
|
-
* @internal
|
|
141
|
-
*/
|
|
142
|
-
private capitalizeSegment;
|
|
143
519
|
/**
|
|
144
520
|
* Check if a header should be ignored based on filter patterns
|
|
145
521
|
* @internal
|
|
@@ -163,6 +539,39 @@ declare class OpenApiGenerator {
|
|
|
163
539
|
* Generate statistics about the generated schemas
|
|
164
540
|
*/
|
|
165
541
|
private generateStats;
|
|
542
|
+
/**
|
|
543
|
+
* Pre-analyze schemas to detect circular dependencies before code generation.
|
|
544
|
+
* This allows the property generator to use z.lazy() for forward references.
|
|
545
|
+
*/
|
|
546
|
+
private analyzeCircularDependencies;
|
|
547
|
+
/**
|
|
548
|
+
* Generate JSDoc warning for allOf conflicts
|
|
549
|
+
* @param conflicts Array of conflict description strings
|
|
550
|
+
* @returns JSDoc formatted warning string
|
|
551
|
+
*/
|
|
552
|
+
private generateConflictJSDoc;
|
|
166
553
|
}
|
|
167
554
|
|
|
168
|
-
|
|
555
|
+
/**
|
|
556
|
+
* Build the Zod validation string for date-time format
|
|
557
|
+
* Pure function that returns the validation string without side effects
|
|
558
|
+
*
|
|
559
|
+
* @param pattern - Optional regex pattern (string or RegExp) for date-time validation
|
|
560
|
+
* @returns Zod validation string (either "z.iso.datetime()" or custom regex)
|
|
561
|
+
* @throws {Error} If the provided pattern is not a valid regular expression
|
|
562
|
+
*
|
|
563
|
+
* @example
|
|
564
|
+
* // Default (no pattern)
|
|
565
|
+
* buildDateTimeValidation() // Returns "z.iso.datetime()"
|
|
566
|
+
*
|
|
567
|
+
* @example
|
|
568
|
+
* // String pattern (for JSON/YAML configs)
|
|
569
|
+
* buildDateTimeValidation('^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}$')
|
|
570
|
+
*
|
|
571
|
+
* @example
|
|
572
|
+
* // RegExp literal (TypeScript configs)
|
|
573
|
+
* buildDateTimeValidation(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/)
|
|
574
|
+
*/
|
|
575
|
+
declare function buildDateTimeValidation(pattern?: string | RegExp): string;
|
|
576
|
+
|
|
577
|
+
export { type CommonSchemaOptions, type ConfigFile, OpenApiGenerator, type OpenApiGeneratorOptions, PropertyGenerator, type PropertyGeneratorContext, type RequestOptions, type ResponseOptions, buildDateTimeValidation, defineConfig };
|