@cerios/openapi-to-zod 0.6.0 → 1.1.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.
@@ -0,0 +1,345 @@
1
+ /**
2
+ * Common options shared by both request and response contexts
3
+ */
4
+ interface CommonSchemaOptions {
5
+ /**
6
+ * Object validation mode
7
+ * - 'strict': Uses z.strictObject() - no additional properties allowed
8
+ * - 'normal': Uses z.object() - additional properties allowed
9
+ * - 'loose': Uses z.looseObject() - explicitly allows additional properties
10
+ */
11
+ mode?: "strict" | "normal" | "loose";
12
+ /**
13
+ * Whether to add .describe() calls for better error messages
14
+ * @default false
15
+ */
16
+ useDescribe?: boolean;
17
+ /**
18
+ * Whether to include descriptions as JSDoc comments
19
+ */
20
+ includeDescriptions?: boolean;
21
+ }
22
+ /**
23
+ * Request-specific options that can override root-level options
24
+ */
25
+ interface RequestOptions extends CommonSchemaOptions {
26
+ }
27
+ /**
28
+ * Response-specific options that can override root-level options
29
+ */
30
+ interface ResponseOptions extends CommonSchemaOptions {
31
+ }
32
+ interface OpenApiGeneratorOptions {
33
+ /**
34
+ * Object validation mode
35
+ * - 'strict': Uses z.strictObject() - no additional properties allowed
36
+ * - 'normal': Uses z.object() - additional properties allowed
37
+ * - 'loose': Uses z.looseObject() - explicitly allows additional properties
38
+ */
39
+ mode?: "strict" | "normal" | "loose";
40
+ /**
41
+ * Input OpenAPI YAML file path
42
+ */
43
+ input: string;
44
+ /**
45
+ * Output TypeScript file path
46
+ * Optional when using string generation methods (generateString)
47
+ * Required when calling generate() to write to a file
48
+ */
49
+ output?: string;
50
+ /**
51
+ * Whether to include descriptions as JSDoc comments
52
+ */
53
+ includeDescriptions?: boolean;
54
+ /**
55
+ * Whether to add .describe() calls for better error messages
56
+ * @default false
57
+ */
58
+ useDescribe?: boolean;
59
+ /**
60
+ * Schema filtering mode
61
+ * - 'all': Generate all schemas (default)
62
+ * - 'request': Only include schemas suitable for requests (excludes readOnly)
63
+ * - 'response': Only include schemas suitable for responses (excludes writeOnly)
64
+ */
65
+ schemaType?: "all" | "request" | "response";
66
+ /**
67
+ * Prefix to add to all generated schema names
68
+ * @example "api" -> "apiUserSchema"
69
+ */
70
+ prefix?: string;
71
+ /**
72
+ * Suffix to add before "Schema" in generated names
73
+ * @example "dto" -> "userDtoSchema"
74
+ */
75
+ suffix?: string;
76
+ /**
77
+ * Strip a common prefix from all schema names before processing
78
+ * Useful when OpenAPI spec has redundant schema prefixes that you want to ignore
79
+ *
80
+ * Supports both literal strings and regex patterns:
81
+ * - Literal string: "Company.Models." (must match exactly)
82
+ * - Regex pattern: "^[A-Z][a-z]+\\." (auto-detected or use RegExp for TypeScript configs)
83
+ *
84
+ * Regex auto-detection checks for: ^, $, \\d, \\w, \\s, .*, .+, [], ()
85
+ *
86
+ * This affects:
87
+ * - Schema name generation (shorter, cleaner names)
88
+ * - Type name generation
89
+ * - References to schemas
90
+ *
91
+ * Applied before prefix/suffix options.
92
+ *
93
+ * @example
94
+ * // Spec has: "Company.Models.User", "Company.Models.Post"
95
+ * // stripSchemaPrefix: "Company.Models."
96
+ * // Results in: "User", "Post"
97
+ * // Schema names: userSchema, postSchema
98
+ *
99
+ * @example
100
+ * // Strip any namespace prefix
101
+ * // stripSchemaPrefix: "^[A-Za-z]+\\."
102
+ * // Matches: "Namespace.User", "App.User", etc.
103
+ *
104
+ * @default undefined (no stripping)
105
+ */
106
+ stripSchemaPrefix?: string | RegExp;
107
+ /**
108
+ * Whether to include generation statistics in output file
109
+ * @default true
110
+ */
111
+ showStats?: boolean;
112
+ /**
113
+ * Request-specific options that override root-level options
114
+ * Applied when schemas are used in request contexts
115
+ */
116
+ request?: RequestOptions;
117
+ /**
118
+ * Response-specific options that override root-level options
119
+ * Applied when schemas are used in response contexts
120
+ */
121
+ response?: ResponseOptions;
122
+ /**
123
+ * Filter which operations to include/exclude from generation
124
+ * Useful for generating separate schemas for different API subsets
125
+ *
126
+ * Filtering logic:
127
+ * 1. If no filters specified, all operations are included
128
+ * 2. Empty arrays are treated as "no constraint" (not as "exclude all")
129
+ * 3. Include filters are applied first (allowlist)
130
+ * 4. Exclude filters are applied second (blocklist)
131
+ * 5. Exclude rules always win over include rules
132
+ *
133
+ * Supports glob patterns for paths and operationIds (e.g., "/api/v1/**", "get*")
134
+ *
135
+ * @example
136
+ * // Only generate schemas for user-related endpoints
137
+ * operationFilters: {
138
+ * includeTags: ["users"]
139
+ * }
140
+ *
141
+ * @example
142
+ * // Generate only GET endpoints, excluding deprecated ones
143
+ * operationFilters: {
144
+ * includeMethods: ["get"],
145
+ * excludeDeprecated: true
146
+ * }
147
+ *
148
+ * @example
149
+ * // Generate only v1 API endpoints
150
+ * operationFilters: {
151
+ * includePaths: ["/api/v1/**"]
152
+ * }
153
+ */
154
+ operationFilters?: OperationFilters;
155
+ /**
156
+ * Header parameters to ignore during schema generation
157
+ * Supports glob patterns for flexible matching
158
+ * Case-insensitive matching (HTTP header semantics)
159
+ *
160
+ * @internal Used by Playwright generator
161
+ */
162
+ ignoreHeaders?: string[];
163
+ /**
164
+ * Cache size for pattern regex compilation
165
+ * Higher values improve performance for large specifications with many string patterns
166
+ * @default 1000
167
+ */
168
+ cacheSize?: number;
169
+ /**
170
+ * Batch size for parallel execution
171
+ * Controls how many specifications are processed concurrently in parallel mode
172
+ * Higher values increase memory usage but may improve throughput
173
+ * @default 10
174
+ */
175
+ batchSize?: number;
176
+ }
177
+ /**
178
+ * Operation filtering options
179
+ * Controls which operations from the OpenAPI specification are included in generation
180
+ */
181
+ interface OperationFilters {
182
+ /**
183
+ * Include only operations with these tags
184
+ * If specified, only operations with at least one matching tag are included
185
+ * Empty array = no constraint
186
+ */
187
+ includeTags?: string[];
188
+ /**
189
+ * Exclude operations with these tags
190
+ * Operations with any matching tag are excluded
191
+ * Empty array = no constraint
192
+ */
193
+ excludeTags?: string[];
194
+ /**
195
+ * Include only operations matching these path patterns
196
+ * Supports glob patterns (e.g., "/users/**", "/api/v1/*")
197
+ * Empty array = no constraint
198
+ */
199
+ includePaths?: string[];
200
+ /**
201
+ * Exclude operations matching these path patterns
202
+ * Supports glob patterns (e.g., "/internal/**", "/admin/*")
203
+ * Empty array = no constraint
204
+ */
205
+ excludePaths?: string[];
206
+ /**
207
+ * Include only these HTTP methods
208
+ * Valid values: "get", "post", "put", "patch", "delete", "head", "options"
209
+ * Empty array = no constraint
210
+ */
211
+ includeMethods?: string[];
212
+ /**
213
+ * Exclude these HTTP methods
214
+ * Valid values: "get", "post", "put", "patch", "delete", "head", "options"
215
+ * Empty array = no constraint
216
+ */
217
+ excludeMethods?: string[];
218
+ /**
219
+ * Include only operations matching these operationId patterns
220
+ * Supports glob patterns (e.g., "getUser*", "*Admin")
221
+ * Empty array = no constraint
222
+ */
223
+ includeOperationIds?: string[];
224
+ /**
225
+ * Exclude operations matching these operationId patterns
226
+ * Supports glob patterns (e.g., "deleteUser*", "*Internal")
227
+ * Empty array = no constraint
228
+ */
229
+ excludeOperationIds?: string[];
230
+ /**
231
+ * Whether to exclude deprecated operations
232
+ * @default false
233
+ */
234
+ excludeDeprecated?: boolean;
235
+ }
236
+ interface OpenAPISchema {
237
+ type?: string | string[];
238
+ format?: string;
239
+ enum?: (string | number)[];
240
+ const?: string | number | boolean | null;
241
+ properties?: Record<string, OpenAPISchema>;
242
+ required?: string[];
243
+ items?: OpenAPISchema;
244
+ prefixItems?: OpenAPISchema[];
245
+ allOf?: OpenAPISchema[];
246
+ oneOf?: OpenAPISchema[];
247
+ anyOf?: OpenAPISchema[];
248
+ $ref?: string;
249
+ nullable?: boolean;
250
+ minLength?: number;
251
+ maxLength?: number;
252
+ minimum?: number;
253
+ maximum?: number;
254
+ exclusiveMinimum?: boolean | number;
255
+ exclusiveMaximum?: boolean | number;
256
+ multipleOf?: number;
257
+ pattern?: string;
258
+ description?: string;
259
+ title?: string;
260
+ example?: any;
261
+ examples?: any[];
262
+ additionalProperties?: boolean | OpenAPISchema;
263
+ minProperties?: number;
264
+ maxProperties?: number;
265
+ minItems?: number;
266
+ maxItems?: number;
267
+ uniqueItems?: boolean;
268
+ contains?: OpenAPISchema;
269
+ minContains?: number;
270
+ maxContains?: number;
271
+ discriminator?: {
272
+ propertyName: string;
273
+ mapping?: Record<string, string>;
274
+ };
275
+ readOnly?: boolean;
276
+ writeOnly?: boolean;
277
+ deprecated?: boolean;
278
+ dependentRequired?: Record<string, string[]>;
279
+ dependencies?: Record<string, string[] | OpenAPISchema>;
280
+ patternProperties?: Record<string, OpenAPISchema>;
281
+ propertyNames?: OpenAPISchema;
282
+ contentMediaType?: string;
283
+ contentEncoding?: string;
284
+ not?: OpenAPISchema;
285
+ if?: OpenAPISchema;
286
+ then?: OpenAPISchema;
287
+ else?: OpenAPISchema;
288
+ unevaluatedProperties?: boolean | OpenAPISchema;
289
+ unevaluatedItems?: boolean | OpenAPISchema;
290
+ }
291
+ interface OpenAPISpec {
292
+ components?: {
293
+ schemas?: Record<string, OpenAPISchema>;
294
+ };
295
+ paths?: Record<string, any>;
296
+ }
297
+ /**
298
+ * Execution mode for batch processing
299
+ * - 'parallel': Process all specifications concurrently (default, faster)
300
+ * - 'sequential': Process specifications one at a time (safer for resource constraints)
301
+ */
302
+ type ExecutionMode = "parallel" | "sequential";
303
+ /**
304
+ * Root configuration file structure
305
+ */
306
+ interface ConfigFile {
307
+ /**
308
+ * Global default options applied to all specifications
309
+ * Can be overridden by individual specification configurations
310
+ */
311
+ defaults?: Partial<Omit<OpenApiGeneratorOptions, "input" | "output">>;
312
+ /**
313
+ * Array of OpenAPI specifications to process
314
+ * Each specification must have input and output paths
315
+ */
316
+ specs: OpenApiGeneratorOptions[];
317
+ /**
318
+ * Execution mode for batch processing
319
+ * @default "parallel"
320
+ */
321
+ executionMode?: ExecutionMode;
322
+ }
323
+ /**
324
+ * Helper function for type-safe config file creation
325
+ * Provides IDE autocomplete and type checking for config files
326
+ *
327
+ * @example
328
+ * ```typescript
329
+ * import { defineConfig } from '@cerios/openapi-to-zod';
330
+ *
331
+ * export default defineConfig({
332
+ * defaults: {
333
+ * mode: 'strict',
334
+ * includeDescriptions: true
335
+ * },
336
+ * specs: [
337
+ * { input: 'api-v1.yaml', output: 'schemas/v1.ts' },
338
+ * { input: 'api-v2.yaml', output: 'schemas/v2.ts', mode: 'normal' }
339
+ * ]
340
+ * });
341
+ * ```
342
+ */
343
+ declare function defineConfig(config: ConfigFile): ConfigFile;
344
+
345
+ export { type CommonSchemaOptions as C, type ExecutionMode as E, type OpenApiGeneratorOptions as O, type RequestOptions as R, type ConfigFile as a, type OpenAPISchema as b, type OpenAPISpec as c, type OperationFilters as d, type ResponseOptions as e, defineConfig as f };
package/package.json CHANGED
@@ -1,78 +1,102 @@
1
- {
2
- "name": "@cerios/openapi-to-zod",
3
- "version": "0.6.0",
4
- "author": "Ronald Veth - Cerios",
5
- "description": "Generate Zod schemas from OpenAPI specifications. A TypeScript code generator that converts OpenAPI/Swagger YAML definitions into type-safe Zod validation schemas.",
6
- "license": "MIT",
7
- "keywords": ["zod", "openapi", "validation", "typescript", "schema", "generator", "api", "yaml", "codegen", "cerios"],
8
- "bin": {
9
- "openapi-to-zod": "./dist/cli.js"
10
- },
11
- "main": "./dist/index.js",
12
- "module": "./dist/index.mjs",
13
- "types": "./dist/index.d.ts",
14
- "files": ["dist"],
15
- "type": "commonjs",
16
- "sideEffects": false,
17
- "exports": {
18
- ".": {
19
- "import": {
20
- "types": "./dist/index.d.mts",
21
- "default": "./dist/index.mjs"
22
- },
23
- "require": {
24
- "types": "./dist/index.d.ts",
25
- "default": "./dist/index.js"
26
- }
27
- },
28
- "./package.json": "./package.json"
29
- },
30
- "bugs": {
31
- "url": "https://github.com/CeriosTesting/openapi-to-zod/issues"
32
- },
33
- "homepage": "https://github.com/CeriosTesting/openapi-to-zod#readme",
34
- "repository": {
35
- "type": "git",
36
- "url": "git+https://github.com/CeriosTesting/openapi-to-zod.git"
37
- },
38
- "scripts": {
39
- "biome:migrate": "biome migrate --write",
40
- "build": "tsup",
41
- "changeset": "npx changeset",
42
- "check": "biome check --write",
43
- "check-exports": "attw --pack .",
44
- "compile": "tsc --noEmit",
45
- "dev": "tsup --watch",
46
- "lint": "biome lint",
47
- "prepare": "husky",
48
- "test": "vitest run",
49
- "test:generate": "node dist/cli.js -i src/phx-case-management-api.openapi.yaml -o output/schemas.ts",
50
- "update-all-packages": "npx npm-check-updates -u && npm i"
51
- },
52
- "dependencies": {
53
- "commander": "^12.1.0",
54
- "cosmiconfig": "^9.0.0",
55
- "esbuild": "^0.27.1",
56
- "minimatch": "^10.0.1",
57
- "yaml": "^2.6.1"
58
- },
59
- "devDependencies": {
60
- "@arethetypeswrong/cli": "^0.18.2",
61
- "@biomejs/biome": "2.3.6",
62
- "@changesets/cli": "^2.29.7",
63
- "@types/node": "^24.10.1",
64
- "@types/prompts": "^2.4.9",
65
- "husky": "^9.1.7",
66
- "lint-staged": "^16.2.6",
67
- "npm-check-updates": "^19.1.2",
68
- "prompts": "^2.4.2",
69
- "tsup": "^8.5.1",
70
- "tsx": "^4.21.0",
71
- "typescript": "^5.9.3",
72
- "vitest": "^4.0.15",
73
- "zod": "^4.1.13"
74
- },
75
- "peerDependencies": {
76
- "zod": "^4.0.0"
77
- }
78
- }
1
+ {
2
+ "name": "@cerios/openapi-to-zod",
3
+ "version": "1.1.0",
4
+ "author": "Ronald Veth - Cerios",
5
+ "description": "Generate Zod schemas from OpenAPI specifications. A TypeScript code generator that converts OpenAPI/Swagger YAML definitions into type-safe Zod validation schemas.",
6
+ "license": "MIT",
7
+ "keywords": [
8
+ "zod",
9
+ "openapi",
10
+ "validation",
11
+ "typescript",
12
+ "schema",
13
+ "generator",
14
+ "api",
15
+ "yaml",
16
+ "codegen",
17
+ "cerios"
18
+ ],
19
+ "bin": {
20
+ "openapi-to-zod": "./dist/cli.js"
21
+ },
22
+ "main": "./dist/index.js",
23
+ "module": "./dist/index.mjs",
24
+ "types": "./dist/index.d.ts",
25
+ "files": [
26
+ "dist"
27
+ ],
28
+ "type": "commonjs",
29
+ "sideEffects": false,
30
+ "exports": {
31
+ ".": {
32
+ "import": {
33
+ "types": "./dist/index.d.mts",
34
+ "default": "./dist/index.mjs"
35
+ },
36
+ "require": {
37
+ "types": "./dist/index.d.ts",
38
+ "default": "./dist/index.js"
39
+ }
40
+ },
41
+ "./internal": {
42
+ "import": {
43
+ "types": "./dist/internal.d.mts",
44
+ "default": "./dist/internal.mjs"
45
+ },
46
+ "require": {
47
+ "types": "./dist/internal.d.ts",
48
+ "default": "./dist/internal.js"
49
+ }
50
+ },
51
+ "./package.json": "./package.json"
52
+ },
53
+ "bugs": {
54
+ "url": "https://github.com/CeriosTesting/openapi-to-zod/issues"
55
+ },
56
+ "homepage": "https://github.com/CeriosTesting/openapi-to-zod#readme",
57
+ "repository": {
58
+ "type": "git",
59
+ "url": "git+https://github.com/CeriosTesting/openapi-to-zod.git"
60
+ },
61
+ "scripts": {
62
+ "biome:migrate": "biome migrate --write",
63
+ "build": "tsup",
64
+ "changeset": "npx changeset",
65
+ "check": "biome check --write",
66
+ "check-exports": "attw --pack .",
67
+ "compile": "tsc --noEmit",
68
+ "dev": "tsup --watch",
69
+ "lint": "biome lint",
70
+ "pack": "npm pack --pack-destination=../..",
71
+ "prepare": "husky",
72
+ "test": "vitest run",
73
+ "test:generate": "node dist/cli.js -i src/phx-case-management-api.openapi.yaml -o output/schemas.ts",
74
+ "update-all-packages": "npx npm-check-updates -u && npm i"
75
+ },
76
+ "dependencies": {
77
+ "commander": "^14.0.2",
78
+ "cosmiconfig": "^9.0.0",
79
+ "esbuild": "^0.27.1",
80
+ "minimatch": "^10.1.1",
81
+ "yaml": "^2.8.2"
82
+ },
83
+ "devDependencies": {
84
+ "@arethetypeswrong/cli": "^0.18.2",
85
+ "@biomejs/biome": "2.3.9",
86
+ "@changesets/cli": "^2.29.8",
87
+ "@types/node": "^25.0.2",
88
+ "@types/prompts": "^2.4.9",
89
+ "husky": "^9.1.7",
90
+ "lint-staged": "^16.2.7",
91
+ "npm-check-updates": "^19.2.0",
92
+ "prompts": "^2.4.2",
93
+ "tsup": "^8.5.1",
94
+ "tsx": "^4.21.0",
95
+ "typescript": "^5.9.3",
96
+ "vitest": "^4.0.15",
97
+ "zod": "^4.2.0"
98
+ },
99
+ "peerDependencies": {
100
+ "zod": "^4.0.0"
101
+ }
102
+ }