@ng-openapi/zod 0.0.2-pr-26-feature-zod-ee9b608.0 → 0.0.2-pr-32-feature-zod-9008b87.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/index.cjs +48 -132
- package/index.d.ts +1 -1
- package/index.js +48 -132
- package/package.json +2 -2
package/index.cjs
CHANGED
|
@@ -48,78 +48,6 @@ function pascalCase(str2) {
|
|
|
48
48
|
}
|
|
49
49
|
__name(pascalCase, "pascalCase");
|
|
50
50
|
|
|
51
|
-
// ../../shared/src/utils/functions/collect-used-types.ts
|
|
52
|
-
function collectUsedTypes(operations) {
|
|
53
|
-
const usedTypes = /* @__PURE__ */ new Set();
|
|
54
|
-
usedTypes.add("RequestOptions");
|
|
55
|
-
operations.forEach((operation) => {
|
|
56
|
-
operation.parameters?.forEach((param) => {
|
|
57
|
-
collectTypesFromSchema(param.schema || param, usedTypes);
|
|
58
|
-
});
|
|
59
|
-
if (operation.requestBody) {
|
|
60
|
-
collectTypesFromRequestBody(operation.requestBody, usedTypes);
|
|
61
|
-
}
|
|
62
|
-
if (operation.responses) {
|
|
63
|
-
Object.values(operation.responses).forEach((response) => {
|
|
64
|
-
collectTypesFromResponse(response, usedTypes);
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
return usedTypes;
|
|
69
|
-
}
|
|
70
|
-
__name(collectUsedTypes, "collectUsedTypes");
|
|
71
|
-
function collectTypesFromSchema(schema2, usedTypes) {
|
|
72
|
-
if (!schema2) return;
|
|
73
|
-
if (schema2.$ref) {
|
|
74
|
-
const refName = schema2.$ref.split("/").pop();
|
|
75
|
-
if (refName) {
|
|
76
|
-
usedTypes.add(pascalCase(refName));
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
if (schema2.type === "array" && schema2.items) {
|
|
80
|
-
collectTypesFromSchema(schema2.items, usedTypes);
|
|
81
|
-
}
|
|
82
|
-
if (schema2.type === "object" && schema2.properties) {
|
|
83
|
-
Object.values(schema2.properties).forEach((prop) => {
|
|
84
|
-
collectTypesFromSchema(prop, usedTypes);
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
if (schema2.allOf) {
|
|
88
|
-
schema2.allOf.forEach((subSchema) => {
|
|
89
|
-
collectTypesFromSchema(subSchema, usedTypes);
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
if (schema2.oneOf) {
|
|
93
|
-
schema2.oneOf.forEach((subSchema) => {
|
|
94
|
-
collectTypesFromSchema(subSchema, usedTypes);
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
if (schema2.anyOf) {
|
|
98
|
-
schema2.anyOf.forEach((subSchema) => {
|
|
99
|
-
collectTypesFromSchema(subSchema, usedTypes);
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
__name(collectTypesFromSchema, "collectTypesFromSchema");
|
|
104
|
-
function collectTypesFromRequestBody(requestBody, usedTypes) {
|
|
105
|
-
const content = requestBody.content || {};
|
|
106
|
-
Object.values(content).forEach((mediaType) => {
|
|
107
|
-
if (mediaType.schema) {
|
|
108
|
-
collectTypesFromSchema(mediaType.schema, usedTypes);
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
__name(collectTypesFromRequestBody, "collectTypesFromRequestBody");
|
|
113
|
-
function collectTypesFromResponse(response, usedTypes) {
|
|
114
|
-
const content = response.content || {};
|
|
115
|
-
Object.values(content).forEach((mediaType) => {
|
|
116
|
-
if (mediaType.schema) {
|
|
117
|
-
collectTypesFromSchema(mediaType.schema, usedTypes);
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
__name(collectTypesFromResponse, "collectTypesFromResponse");
|
|
122
|
-
|
|
123
51
|
// ../../shared/src/utils/functions/extract-paths.ts
|
|
124
52
|
function extractPaths(swaggerPaths = {}, methods = [
|
|
125
53
|
"get",
|
|
@@ -195,6 +123,14 @@ var PROVIDER_GENERATOR_HEADER_COMMENT = defaultHeaderComment + `* Generated prov
|
|
|
195
123
|
* Do not edit this file manually
|
|
196
124
|
*/
|
|
197
125
|
`;
|
|
126
|
+
var ZOD_PLUGIN_GENERATOR_HEADER_COMMENT = /* @__PURE__ */ __name((validatorName) => defaultHeaderComment + `* Generated Zod Schemas for ${validatorName}
|
|
127
|
+
* Do not edit this file manually
|
|
128
|
+
*/
|
|
129
|
+
`, "ZOD_PLUGIN_GENERATOR_HEADER_COMMENT");
|
|
130
|
+
var ZOD_PLUGIN_INDEX_GENERATOR_HEADER_COMMENT = defaultHeaderComment + `* Generated Zod Schemas exports
|
|
131
|
+
* Do not edit this file manually
|
|
132
|
+
*/
|
|
133
|
+
`;
|
|
198
134
|
|
|
199
135
|
// ../../../node_modules/js-yaml/dist/js-yaml.mjs
|
|
200
136
|
function isNothing(subject) {
|
|
@@ -2894,11 +2830,13 @@ var safeLoad = renamed("safeLoad", "load");
|
|
|
2894
2830
|
var safeLoadAll = renamed("safeLoadAll", "loadAll");
|
|
2895
2831
|
var safeDump = renamed("safeDump", "dump");
|
|
2896
2832
|
|
|
2897
|
-
// src/lib/
|
|
2833
|
+
// src/lib/utils/is-reference-object.ts
|
|
2898
2834
|
function isReferenceObject(obj) {
|
|
2899
2835
|
return obj && typeof obj === "object" && "$ref" in obj;
|
|
2900
2836
|
}
|
|
2901
2837
|
__name(isReferenceObject, "isReferenceObject");
|
|
2838
|
+
|
|
2839
|
+
// src/lib/zod-schema.builder.ts
|
|
2902
2840
|
var ZodSchemaBuilder = class {
|
|
2903
2841
|
static {
|
|
2904
2842
|
__name(this, "ZodSchemaBuilder");
|
|
@@ -2906,7 +2844,6 @@ var ZodSchemaBuilder = class {
|
|
|
2906
2844
|
parser;
|
|
2907
2845
|
config;
|
|
2908
2846
|
options;
|
|
2909
|
-
constsCounter = {};
|
|
2910
2847
|
constructor(parser, config, options) {
|
|
2911
2848
|
this.parser = parser;
|
|
2912
2849
|
this.config = config;
|
|
@@ -2944,7 +2881,7 @@ var ZodSchemaBuilder = class {
|
|
|
2944
2881
|
zodSchema = "z.any()";
|
|
2945
2882
|
}
|
|
2946
2883
|
if (!buildOptions.required && schema2.default !== void 0) {
|
|
2947
|
-
const defaultValue = this.generateDefaultValue(schema2.default
|
|
2884
|
+
const defaultValue = this.generateDefaultValue(schema2.default);
|
|
2948
2885
|
zodSchema = `${zodSchema}.default(${defaultValue})`;
|
|
2949
2886
|
} else if (!buildOptions.required && nullable) {
|
|
2950
2887
|
zodSchema = `${zodSchema}.nullish()`;
|
|
@@ -2970,15 +2907,17 @@ var ZodSchemaBuilder = class {
|
|
|
2970
2907
|
return buildOptions.coerce ? "z.coerce.date()" : "z.date()";
|
|
2971
2908
|
}
|
|
2972
2909
|
return buildOptions.coerce ? "z.coerce.string().date()" : "z.string().date()";
|
|
2973
|
-
case "date-time":
|
|
2910
|
+
case "date-time": {
|
|
2974
2911
|
if (this.config.options?.dateType === "Date") {
|
|
2975
2912
|
return buildOptions.coerce ? "z.coerce.date()" : "z.date()";
|
|
2976
2913
|
}
|
|
2977
2914
|
const datetimeOptions = this.options.dateTime ? `, ${JSON.stringify(this.options.dateTime)}` : "";
|
|
2978
2915
|
return buildOptions.coerce ? `z.coerce.string().datetime(${datetimeOptions})` : `z.string().datetime(${datetimeOptions})`;
|
|
2979
|
-
|
|
2916
|
+
}
|
|
2917
|
+
case "time": {
|
|
2980
2918
|
const timeOptions = this.options.time ? `, ${JSON.stringify(this.options.time)}` : "";
|
|
2981
2919
|
return buildOptions.coerce ? `z.coerce.string().time(${timeOptions})` : `z.string().time(${timeOptions})`;
|
|
2920
|
+
}
|
|
2982
2921
|
case "email":
|
|
2983
2922
|
return buildOptions.coerce ? "z.coerce.string().email()" : "z.string().email()";
|
|
2984
2923
|
case "uri":
|
|
@@ -3128,7 +3067,7 @@ ${properties.join(",\n")}
|
|
|
3128
3067
|
}
|
|
3129
3068
|
return schema2.type || "any";
|
|
3130
3069
|
}
|
|
3131
|
-
generateDefaultValue(defaultValue
|
|
3070
|
+
generateDefaultValue(defaultValue) {
|
|
3132
3071
|
if (typeof defaultValue === "string") {
|
|
3133
3072
|
return `'${this.escapeString(defaultValue)}'`;
|
|
3134
3073
|
}
|
|
@@ -3161,10 +3100,6 @@ ${properties.join(",\n")}
|
|
|
3161
3100
|
};
|
|
3162
3101
|
|
|
3163
3102
|
// src/lib/zod-schema.generator.ts
|
|
3164
|
-
function isReferenceObject2(obj) {
|
|
3165
|
-
return obj && typeof obj === "object" && "$ref" in obj;
|
|
3166
|
-
}
|
|
3167
|
-
__name(isReferenceObject2, "isReferenceObject");
|
|
3168
3103
|
var ZodSchemaGenerator = class {
|
|
3169
3104
|
static {
|
|
3170
3105
|
__name(this, "ZodSchemaGenerator");
|
|
@@ -3302,21 +3237,21 @@ var ZodSchemaGenerator = class {
|
|
|
3302
3237
|
};
|
|
3303
3238
|
}
|
|
3304
3239
|
resolveRequestBody(requestBody) {
|
|
3305
|
-
if (
|
|
3240
|
+
if (isReferenceObject(requestBody)) {
|
|
3306
3241
|
const resolved = this.parser.resolveReference(requestBody.$ref);
|
|
3307
3242
|
return resolved;
|
|
3308
3243
|
}
|
|
3309
3244
|
return requestBody;
|
|
3310
3245
|
}
|
|
3311
3246
|
resolveResponse(response) {
|
|
3312
|
-
if (
|
|
3247
|
+
if (isReferenceObject(response)) {
|
|
3313
3248
|
const resolved = this.parser.resolveReference(response.$ref);
|
|
3314
3249
|
return resolved;
|
|
3315
3250
|
}
|
|
3316
3251
|
return response;
|
|
3317
3252
|
}
|
|
3318
3253
|
resolveSchema(schema2) {
|
|
3319
|
-
if (
|
|
3254
|
+
if (isReferenceObject(schema2)) {
|
|
3320
3255
|
const resolved = this.parser.resolveReference(schema2.$ref);
|
|
3321
3256
|
return resolved;
|
|
3322
3257
|
}
|
|
@@ -3324,11 +3259,11 @@ var ZodSchemaGenerator = class {
|
|
|
3324
3259
|
}
|
|
3325
3260
|
shouldCoerce(type2) {
|
|
3326
3261
|
const typeMap = {
|
|
3327
|
-
|
|
3328
|
-
|
|
3329
|
-
|
|
3330
|
-
|
|
3331
|
-
|
|
3262
|
+
path: "param",
|
|
3263
|
+
query: "query",
|
|
3264
|
+
header: "header",
|
|
3265
|
+
body: "body",
|
|
3266
|
+
response: "response"
|
|
3332
3267
|
};
|
|
3333
3268
|
const mappedType = typeMap[type2] || type2;
|
|
3334
3269
|
if (typeof this.options.coerce === "boolean") {
|
|
@@ -3338,11 +3273,11 @@ var ZodSchemaGenerator = class {
|
|
|
3338
3273
|
}
|
|
3339
3274
|
isStrict(type2) {
|
|
3340
3275
|
const typeMap = {
|
|
3341
|
-
|
|
3342
|
-
|
|
3343
|
-
|
|
3344
|
-
|
|
3345
|
-
|
|
3276
|
+
path: "param",
|
|
3277
|
+
query: "query",
|
|
3278
|
+
header: "header",
|
|
3279
|
+
body: "body",
|
|
3280
|
+
response: "response"
|
|
3346
3281
|
};
|
|
3347
3282
|
const mappedType = typeMap[type2] || type2;
|
|
3348
3283
|
if (typeof this.options.strict === "boolean") {
|
|
@@ -3369,31 +3304,19 @@ var ZodIndexGenerator = class {
|
|
|
3369
3304
|
const sourceFile = this.project.createSourceFile(indexPath, "", {
|
|
3370
3305
|
overwrite: true
|
|
3371
3306
|
});
|
|
3372
|
-
sourceFile.insertText(0,
|
|
3307
|
+
sourceFile.insertText(0, ZOD_PLUGIN_INDEX_GENERATOR_HEADER_COMMENT);
|
|
3373
3308
|
const validatorFiles = fs.readdirSync(validatorsDir).filter((file) => file.endsWith(".validator.ts")).map((file) => file.replace(".validator.ts", ""));
|
|
3374
3309
|
validatorFiles.forEach((fileName) => {
|
|
3375
3310
|
sourceFile.addExportDeclaration({
|
|
3376
|
-
namedExports: [
|
|
3377
|
-
"*"
|
|
3378
|
-
],
|
|
3379
3311
|
moduleSpecifier: `./${fileName}.validator`
|
|
3380
3312
|
});
|
|
3381
3313
|
});
|
|
3382
3314
|
sourceFile.formatText();
|
|
3383
3315
|
sourceFile.saveSync();
|
|
3384
3316
|
}
|
|
3385
|
-
generateHeaderComment() {
|
|
3386
|
-
return `/**
|
|
3387
|
-
* Validators Index
|
|
3388
|
-
* Generated by ng-openapi
|
|
3389
|
-
* Do not edit manually
|
|
3390
|
-
*/
|
|
3391
|
-
|
|
3392
|
-
`;
|
|
3393
|
-
}
|
|
3394
3317
|
};
|
|
3395
3318
|
|
|
3396
|
-
// src/lib/
|
|
3319
|
+
// src/lib/utils/default-options.ts
|
|
3397
3320
|
var DEFAULT_OPTIONS = {
|
|
3398
3321
|
strict: false,
|
|
3399
3322
|
coerce: false,
|
|
@@ -3405,6 +3328,8 @@ var DEFAULT_OPTIONS = {
|
|
|
3405
3328
|
response: true
|
|
3406
3329
|
}
|
|
3407
3330
|
};
|
|
3331
|
+
|
|
3332
|
+
// src/lib/zod.generator.ts
|
|
3408
3333
|
var ZodGenerator = class {
|
|
3409
3334
|
static {
|
|
3410
3335
|
__name(this, "ZodGenerator");
|
|
@@ -3471,22 +3396,17 @@ var ZodGenerator = class {
|
|
|
3471
3396
|
const sourceFile = this.project.createSourceFile(filePath, "", {
|
|
3472
3397
|
overwrite: true
|
|
3473
3398
|
});
|
|
3474
|
-
sourceFile.insertText(0,
|
|
3399
|
+
sourceFile.insertText(0, ZOD_PLUGIN_GENERATOR_HEADER_COMMENT(validatorName));
|
|
3475
3400
|
this.addImports(sourceFile, operations);
|
|
3401
|
+
const _statements = [];
|
|
3476
3402
|
for (const operation of operations) {
|
|
3477
|
-
await this.generateOperationValidators(sourceFile, operation);
|
|
3403
|
+
const statements = await this.generateOperationValidators(sourceFile, operation);
|
|
3404
|
+
_statements.push(...statements);
|
|
3405
|
+
}
|
|
3406
|
+
if (_statements.length > 0) {
|
|
3407
|
+
sourceFile.fixMissingImports().organizeImports().fixUnusedIdentifiers().formatText();
|
|
3408
|
+
sourceFile.saveSync();
|
|
3478
3409
|
}
|
|
3479
|
-
sourceFile.formatText();
|
|
3480
|
-
sourceFile.saveSync();
|
|
3481
|
-
}
|
|
3482
|
-
generateHeaderComment(validatorName) {
|
|
3483
|
-
return `/**
|
|
3484
|
-
* ${validatorName} Validators
|
|
3485
|
-
* Generated by ng-openapi
|
|
3486
|
-
* Do not edit manually
|
|
3487
|
-
*/
|
|
3488
|
-
|
|
3489
|
-
`;
|
|
3490
3410
|
}
|
|
3491
3411
|
addImports(sourceFile, operations) {
|
|
3492
3412
|
sourceFile.addImportDeclaration({
|
|
@@ -3495,34 +3415,30 @@ var ZodGenerator = class {
|
|
|
3495
3415
|
],
|
|
3496
3416
|
moduleSpecifier: "zod"
|
|
3497
3417
|
});
|
|
3498
|
-
const usedTypes = collectUsedTypes(operations);
|
|
3499
|
-
if (usedTypes.size > 0) {
|
|
3500
|
-
sourceFile.addImportDeclaration({
|
|
3501
|
-
namedImports: Array.from(usedTypes).sort(),
|
|
3502
|
-
moduleSpecifier: "../models"
|
|
3503
|
-
});
|
|
3504
|
-
}
|
|
3505
3418
|
}
|
|
3506
3419
|
async generateOperationValidators(sourceFile, operation) {
|
|
3507
3420
|
const operationName = this.getOperationName(operation);
|
|
3421
|
+
const statements = [];
|
|
3508
3422
|
if (this.shouldGenerate("param") || this.shouldGenerate("query") || this.shouldGenerate("header")) {
|
|
3509
3423
|
const params = await this.generateParameterValidators(operation, operationName);
|
|
3510
3424
|
if (params.length > 0) {
|
|
3511
|
-
|
|
3425
|
+
statements.push(...params);
|
|
3512
3426
|
}
|
|
3513
3427
|
}
|
|
3514
3428
|
if (this.shouldGenerate("body") && operation.requestBody) {
|
|
3515
3429
|
const bodyValidators = await this.schemaGenerator.generateBodyValidator(operation, operationName);
|
|
3516
3430
|
if (bodyValidators.length > 0) {
|
|
3517
|
-
|
|
3431
|
+
statements.push(...bodyValidators);
|
|
3518
3432
|
}
|
|
3519
3433
|
}
|
|
3520
3434
|
if (this.shouldGenerate("response")) {
|
|
3521
3435
|
const responseValidators = await this.schemaGenerator.generateResponseValidators(operation, operationName);
|
|
3522
3436
|
if (responseValidators.length > 0) {
|
|
3523
|
-
|
|
3437
|
+
statements.push(...responseValidators);
|
|
3524
3438
|
}
|
|
3525
3439
|
}
|
|
3440
|
+
sourceFile.addStatements(statements);
|
|
3441
|
+
return statements;
|
|
3526
3442
|
}
|
|
3527
3443
|
async generateParameterValidators(operation, operationName) {
|
|
3528
3444
|
const statements = [];
|
package/index.d.ts
CHANGED
|
@@ -168,6 +168,7 @@ interface ZodPluginOptions {
|
|
|
168
168
|
precision?: -1 | 0 | 1 | 2 | 3;
|
|
169
169
|
};
|
|
170
170
|
}
|
|
171
|
+
|
|
171
172
|
declare class ZodGenerator implements IPluginGenerator {
|
|
172
173
|
private project;
|
|
173
174
|
private parser;
|
|
@@ -180,7 +181,6 @@ declare class ZodGenerator implements IPluginGenerator {
|
|
|
180
181
|
generate(outputRoot: string): Promise<void>;
|
|
181
182
|
private groupPathsByController;
|
|
182
183
|
private generateValidatorFile;
|
|
183
|
-
private generateHeaderComment;
|
|
184
184
|
private addImports;
|
|
185
185
|
private generateOperationValidators;
|
|
186
186
|
private generateParameterValidators;
|
package/index.js
CHANGED
|
@@ -14,78 +14,6 @@ function pascalCase(str2) {
|
|
|
14
14
|
}
|
|
15
15
|
__name(pascalCase, "pascalCase");
|
|
16
16
|
|
|
17
|
-
// ../../shared/src/utils/functions/collect-used-types.ts
|
|
18
|
-
function collectUsedTypes(operations) {
|
|
19
|
-
const usedTypes = /* @__PURE__ */ new Set();
|
|
20
|
-
usedTypes.add("RequestOptions");
|
|
21
|
-
operations.forEach((operation) => {
|
|
22
|
-
operation.parameters?.forEach((param) => {
|
|
23
|
-
collectTypesFromSchema(param.schema || param, usedTypes);
|
|
24
|
-
});
|
|
25
|
-
if (operation.requestBody) {
|
|
26
|
-
collectTypesFromRequestBody(operation.requestBody, usedTypes);
|
|
27
|
-
}
|
|
28
|
-
if (operation.responses) {
|
|
29
|
-
Object.values(operation.responses).forEach((response) => {
|
|
30
|
-
collectTypesFromResponse(response, usedTypes);
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
return usedTypes;
|
|
35
|
-
}
|
|
36
|
-
__name(collectUsedTypes, "collectUsedTypes");
|
|
37
|
-
function collectTypesFromSchema(schema2, usedTypes) {
|
|
38
|
-
if (!schema2) return;
|
|
39
|
-
if (schema2.$ref) {
|
|
40
|
-
const refName = schema2.$ref.split("/").pop();
|
|
41
|
-
if (refName) {
|
|
42
|
-
usedTypes.add(pascalCase(refName));
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
if (schema2.type === "array" && schema2.items) {
|
|
46
|
-
collectTypesFromSchema(schema2.items, usedTypes);
|
|
47
|
-
}
|
|
48
|
-
if (schema2.type === "object" && schema2.properties) {
|
|
49
|
-
Object.values(schema2.properties).forEach((prop) => {
|
|
50
|
-
collectTypesFromSchema(prop, usedTypes);
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
if (schema2.allOf) {
|
|
54
|
-
schema2.allOf.forEach((subSchema) => {
|
|
55
|
-
collectTypesFromSchema(subSchema, usedTypes);
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
if (schema2.oneOf) {
|
|
59
|
-
schema2.oneOf.forEach((subSchema) => {
|
|
60
|
-
collectTypesFromSchema(subSchema, usedTypes);
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
if (schema2.anyOf) {
|
|
64
|
-
schema2.anyOf.forEach((subSchema) => {
|
|
65
|
-
collectTypesFromSchema(subSchema, usedTypes);
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
__name(collectTypesFromSchema, "collectTypesFromSchema");
|
|
70
|
-
function collectTypesFromRequestBody(requestBody, usedTypes) {
|
|
71
|
-
const content = requestBody.content || {};
|
|
72
|
-
Object.values(content).forEach((mediaType) => {
|
|
73
|
-
if (mediaType.schema) {
|
|
74
|
-
collectTypesFromSchema(mediaType.schema, usedTypes);
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
__name(collectTypesFromRequestBody, "collectTypesFromRequestBody");
|
|
79
|
-
function collectTypesFromResponse(response, usedTypes) {
|
|
80
|
-
const content = response.content || {};
|
|
81
|
-
Object.values(content).forEach((mediaType) => {
|
|
82
|
-
if (mediaType.schema) {
|
|
83
|
-
collectTypesFromSchema(mediaType.schema, usedTypes);
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
__name(collectTypesFromResponse, "collectTypesFromResponse");
|
|
88
|
-
|
|
89
17
|
// ../../shared/src/utils/functions/extract-paths.ts
|
|
90
18
|
function extractPaths(swaggerPaths = {}, methods = [
|
|
91
19
|
"get",
|
|
@@ -161,6 +89,14 @@ var PROVIDER_GENERATOR_HEADER_COMMENT = defaultHeaderComment + `* Generated prov
|
|
|
161
89
|
* Do not edit this file manually
|
|
162
90
|
*/
|
|
163
91
|
`;
|
|
92
|
+
var ZOD_PLUGIN_GENERATOR_HEADER_COMMENT = /* @__PURE__ */ __name((validatorName) => defaultHeaderComment + `* Generated Zod Schemas for ${validatorName}
|
|
93
|
+
* Do not edit this file manually
|
|
94
|
+
*/
|
|
95
|
+
`, "ZOD_PLUGIN_GENERATOR_HEADER_COMMENT");
|
|
96
|
+
var ZOD_PLUGIN_INDEX_GENERATOR_HEADER_COMMENT = defaultHeaderComment + `* Generated Zod Schemas exports
|
|
97
|
+
* Do not edit this file manually
|
|
98
|
+
*/
|
|
99
|
+
`;
|
|
164
100
|
|
|
165
101
|
// ../../../node_modules/js-yaml/dist/js-yaml.mjs
|
|
166
102
|
function isNothing(subject) {
|
|
@@ -2860,11 +2796,13 @@ var safeLoad = renamed("safeLoad", "load");
|
|
|
2860
2796
|
var safeLoadAll = renamed("safeLoadAll", "loadAll");
|
|
2861
2797
|
var safeDump = renamed("safeDump", "dump");
|
|
2862
2798
|
|
|
2863
|
-
// src/lib/
|
|
2799
|
+
// src/lib/utils/is-reference-object.ts
|
|
2864
2800
|
function isReferenceObject(obj) {
|
|
2865
2801
|
return obj && typeof obj === "object" && "$ref" in obj;
|
|
2866
2802
|
}
|
|
2867
2803
|
__name(isReferenceObject, "isReferenceObject");
|
|
2804
|
+
|
|
2805
|
+
// src/lib/zod-schema.builder.ts
|
|
2868
2806
|
var ZodSchemaBuilder = class {
|
|
2869
2807
|
static {
|
|
2870
2808
|
__name(this, "ZodSchemaBuilder");
|
|
@@ -2872,7 +2810,6 @@ var ZodSchemaBuilder = class {
|
|
|
2872
2810
|
parser;
|
|
2873
2811
|
config;
|
|
2874
2812
|
options;
|
|
2875
|
-
constsCounter = {};
|
|
2876
2813
|
constructor(parser, config, options) {
|
|
2877
2814
|
this.parser = parser;
|
|
2878
2815
|
this.config = config;
|
|
@@ -2910,7 +2847,7 @@ var ZodSchemaBuilder = class {
|
|
|
2910
2847
|
zodSchema = "z.any()";
|
|
2911
2848
|
}
|
|
2912
2849
|
if (!buildOptions.required && schema2.default !== void 0) {
|
|
2913
|
-
const defaultValue = this.generateDefaultValue(schema2.default
|
|
2850
|
+
const defaultValue = this.generateDefaultValue(schema2.default);
|
|
2914
2851
|
zodSchema = `${zodSchema}.default(${defaultValue})`;
|
|
2915
2852
|
} else if (!buildOptions.required && nullable) {
|
|
2916
2853
|
zodSchema = `${zodSchema}.nullish()`;
|
|
@@ -2936,15 +2873,17 @@ var ZodSchemaBuilder = class {
|
|
|
2936
2873
|
return buildOptions.coerce ? "z.coerce.date()" : "z.date()";
|
|
2937
2874
|
}
|
|
2938
2875
|
return buildOptions.coerce ? "z.coerce.string().date()" : "z.string().date()";
|
|
2939
|
-
case "date-time":
|
|
2876
|
+
case "date-time": {
|
|
2940
2877
|
if (this.config.options?.dateType === "Date") {
|
|
2941
2878
|
return buildOptions.coerce ? "z.coerce.date()" : "z.date()";
|
|
2942
2879
|
}
|
|
2943
2880
|
const datetimeOptions = this.options.dateTime ? `, ${JSON.stringify(this.options.dateTime)}` : "";
|
|
2944
2881
|
return buildOptions.coerce ? `z.coerce.string().datetime(${datetimeOptions})` : `z.string().datetime(${datetimeOptions})`;
|
|
2945
|
-
|
|
2882
|
+
}
|
|
2883
|
+
case "time": {
|
|
2946
2884
|
const timeOptions = this.options.time ? `, ${JSON.stringify(this.options.time)}` : "";
|
|
2947
2885
|
return buildOptions.coerce ? `z.coerce.string().time(${timeOptions})` : `z.string().time(${timeOptions})`;
|
|
2886
|
+
}
|
|
2948
2887
|
case "email":
|
|
2949
2888
|
return buildOptions.coerce ? "z.coerce.string().email()" : "z.string().email()";
|
|
2950
2889
|
case "uri":
|
|
@@ -3094,7 +3033,7 @@ ${properties.join(",\n")}
|
|
|
3094
3033
|
}
|
|
3095
3034
|
return schema2.type || "any";
|
|
3096
3035
|
}
|
|
3097
|
-
generateDefaultValue(defaultValue
|
|
3036
|
+
generateDefaultValue(defaultValue) {
|
|
3098
3037
|
if (typeof defaultValue === "string") {
|
|
3099
3038
|
return `'${this.escapeString(defaultValue)}'`;
|
|
3100
3039
|
}
|
|
@@ -3127,10 +3066,6 @@ ${properties.join(",\n")}
|
|
|
3127
3066
|
};
|
|
3128
3067
|
|
|
3129
3068
|
// src/lib/zod-schema.generator.ts
|
|
3130
|
-
function isReferenceObject2(obj) {
|
|
3131
|
-
return obj && typeof obj === "object" && "$ref" in obj;
|
|
3132
|
-
}
|
|
3133
|
-
__name(isReferenceObject2, "isReferenceObject");
|
|
3134
3069
|
var ZodSchemaGenerator = class {
|
|
3135
3070
|
static {
|
|
3136
3071
|
__name(this, "ZodSchemaGenerator");
|
|
@@ -3268,21 +3203,21 @@ var ZodSchemaGenerator = class {
|
|
|
3268
3203
|
};
|
|
3269
3204
|
}
|
|
3270
3205
|
resolveRequestBody(requestBody) {
|
|
3271
|
-
if (
|
|
3206
|
+
if (isReferenceObject(requestBody)) {
|
|
3272
3207
|
const resolved = this.parser.resolveReference(requestBody.$ref);
|
|
3273
3208
|
return resolved;
|
|
3274
3209
|
}
|
|
3275
3210
|
return requestBody;
|
|
3276
3211
|
}
|
|
3277
3212
|
resolveResponse(response) {
|
|
3278
|
-
if (
|
|
3213
|
+
if (isReferenceObject(response)) {
|
|
3279
3214
|
const resolved = this.parser.resolveReference(response.$ref);
|
|
3280
3215
|
return resolved;
|
|
3281
3216
|
}
|
|
3282
3217
|
return response;
|
|
3283
3218
|
}
|
|
3284
3219
|
resolveSchema(schema2) {
|
|
3285
|
-
if (
|
|
3220
|
+
if (isReferenceObject(schema2)) {
|
|
3286
3221
|
const resolved = this.parser.resolveReference(schema2.$ref);
|
|
3287
3222
|
return resolved;
|
|
3288
3223
|
}
|
|
@@ -3290,11 +3225,11 @@ var ZodSchemaGenerator = class {
|
|
|
3290
3225
|
}
|
|
3291
3226
|
shouldCoerce(type2) {
|
|
3292
3227
|
const typeMap = {
|
|
3293
|
-
|
|
3294
|
-
|
|
3295
|
-
|
|
3296
|
-
|
|
3297
|
-
|
|
3228
|
+
path: "param",
|
|
3229
|
+
query: "query",
|
|
3230
|
+
header: "header",
|
|
3231
|
+
body: "body",
|
|
3232
|
+
response: "response"
|
|
3298
3233
|
};
|
|
3299
3234
|
const mappedType = typeMap[type2] || type2;
|
|
3300
3235
|
if (typeof this.options.coerce === "boolean") {
|
|
@@ -3304,11 +3239,11 @@ var ZodSchemaGenerator = class {
|
|
|
3304
3239
|
}
|
|
3305
3240
|
isStrict(type2) {
|
|
3306
3241
|
const typeMap = {
|
|
3307
|
-
|
|
3308
|
-
|
|
3309
|
-
|
|
3310
|
-
|
|
3311
|
-
|
|
3242
|
+
path: "param",
|
|
3243
|
+
query: "query",
|
|
3244
|
+
header: "header",
|
|
3245
|
+
body: "body",
|
|
3246
|
+
response: "response"
|
|
3312
3247
|
};
|
|
3313
3248
|
const mappedType = typeMap[type2] || type2;
|
|
3314
3249
|
if (typeof this.options.strict === "boolean") {
|
|
@@ -3335,31 +3270,19 @@ var ZodIndexGenerator = class {
|
|
|
3335
3270
|
const sourceFile = this.project.createSourceFile(indexPath, "", {
|
|
3336
3271
|
overwrite: true
|
|
3337
3272
|
});
|
|
3338
|
-
sourceFile.insertText(0,
|
|
3273
|
+
sourceFile.insertText(0, ZOD_PLUGIN_INDEX_GENERATOR_HEADER_COMMENT);
|
|
3339
3274
|
const validatorFiles = fs.readdirSync(validatorsDir).filter((file) => file.endsWith(".validator.ts")).map((file) => file.replace(".validator.ts", ""));
|
|
3340
3275
|
validatorFiles.forEach((fileName) => {
|
|
3341
3276
|
sourceFile.addExportDeclaration({
|
|
3342
|
-
namedExports: [
|
|
3343
|
-
"*"
|
|
3344
|
-
],
|
|
3345
3277
|
moduleSpecifier: `./${fileName}.validator`
|
|
3346
3278
|
});
|
|
3347
3279
|
});
|
|
3348
3280
|
sourceFile.formatText();
|
|
3349
3281
|
sourceFile.saveSync();
|
|
3350
3282
|
}
|
|
3351
|
-
generateHeaderComment() {
|
|
3352
|
-
return `/**
|
|
3353
|
-
* Validators Index
|
|
3354
|
-
* Generated by ng-openapi
|
|
3355
|
-
* Do not edit manually
|
|
3356
|
-
*/
|
|
3357
|
-
|
|
3358
|
-
`;
|
|
3359
|
-
}
|
|
3360
3283
|
};
|
|
3361
3284
|
|
|
3362
|
-
// src/lib/
|
|
3285
|
+
// src/lib/utils/default-options.ts
|
|
3363
3286
|
var DEFAULT_OPTIONS = {
|
|
3364
3287
|
strict: false,
|
|
3365
3288
|
coerce: false,
|
|
@@ -3371,6 +3294,8 @@ var DEFAULT_OPTIONS = {
|
|
|
3371
3294
|
response: true
|
|
3372
3295
|
}
|
|
3373
3296
|
};
|
|
3297
|
+
|
|
3298
|
+
// src/lib/zod.generator.ts
|
|
3374
3299
|
var ZodGenerator = class {
|
|
3375
3300
|
static {
|
|
3376
3301
|
__name(this, "ZodGenerator");
|
|
@@ -3437,22 +3362,17 @@ var ZodGenerator = class {
|
|
|
3437
3362
|
const sourceFile = this.project.createSourceFile(filePath, "", {
|
|
3438
3363
|
overwrite: true
|
|
3439
3364
|
});
|
|
3440
|
-
sourceFile.insertText(0,
|
|
3365
|
+
sourceFile.insertText(0, ZOD_PLUGIN_GENERATOR_HEADER_COMMENT(validatorName));
|
|
3441
3366
|
this.addImports(sourceFile, operations);
|
|
3367
|
+
const _statements = [];
|
|
3442
3368
|
for (const operation of operations) {
|
|
3443
|
-
await this.generateOperationValidators(sourceFile, operation);
|
|
3369
|
+
const statements = await this.generateOperationValidators(sourceFile, operation);
|
|
3370
|
+
_statements.push(...statements);
|
|
3371
|
+
}
|
|
3372
|
+
if (_statements.length > 0) {
|
|
3373
|
+
sourceFile.fixMissingImports().organizeImports().fixUnusedIdentifiers().formatText();
|
|
3374
|
+
sourceFile.saveSync();
|
|
3444
3375
|
}
|
|
3445
|
-
sourceFile.formatText();
|
|
3446
|
-
sourceFile.saveSync();
|
|
3447
|
-
}
|
|
3448
|
-
generateHeaderComment(validatorName) {
|
|
3449
|
-
return `/**
|
|
3450
|
-
* ${validatorName} Validators
|
|
3451
|
-
* Generated by ng-openapi
|
|
3452
|
-
* Do not edit manually
|
|
3453
|
-
*/
|
|
3454
|
-
|
|
3455
|
-
`;
|
|
3456
3376
|
}
|
|
3457
3377
|
addImports(sourceFile, operations) {
|
|
3458
3378
|
sourceFile.addImportDeclaration({
|
|
@@ -3461,34 +3381,30 @@ var ZodGenerator = class {
|
|
|
3461
3381
|
],
|
|
3462
3382
|
moduleSpecifier: "zod"
|
|
3463
3383
|
});
|
|
3464
|
-
const usedTypes = collectUsedTypes(operations);
|
|
3465
|
-
if (usedTypes.size > 0) {
|
|
3466
|
-
sourceFile.addImportDeclaration({
|
|
3467
|
-
namedImports: Array.from(usedTypes).sort(),
|
|
3468
|
-
moduleSpecifier: "../models"
|
|
3469
|
-
});
|
|
3470
|
-
}
|
|
3471
3384
|
}
|
|
3472
3385
|
async generateOperationValidators(sourceFile, operation) {
|
|
3473
3386
|
const operationName = this.getOperationName(operation);
|
|
3387
|
+
const statements = [];
|
|
3474
3388
|
if (this.shouldGenerate("param") || this.shouldGenerate("query") || this.shouldGenerate("header")) {
|
|
3475
3389
|
const params = await this.generateParameterValidators(operation, operationName);
|
|
3476
3390
|
if (params.length > 0) {
|
|
3477
|
-
|
|
3391
|
+
statements.push(...params);
|
|
3478
3392
|
}
|
|
3479
3393
|
}
|
|
3480
3394
|
if (this.shouldGenerate("body") && operation.requestBody) {
|
|
3481
3395
|
const bodyValidators = await this.schemaGenerator.generateBodyValidator(operation, operationName);
|
|
3482
3396
|
if (bodyValidators.length > 0) {
|
|
3483
|
-
|
|
3397
|
+
statements.push(...bodyValidators);
|
|
3484
3398
|
}
|
|
3485
3399
|
}
|
|
3486
3400
|
if (this.shouldGenerate("response")) {
|
|
3487
3401
|
const responseValidators = await this.schemaGenerator.generateResponseValidators(operation, operationName);
|
|
3488
3402
|
if (responseValidators.length > 0) {
|
|
3489
|
-
|
|
3403
|
+
statements.push(...responseValidators);
|
|
3490
3404
|
}
|
|
3491
3405
|
}
|
|
3406
|
+
sourceFile.addStatements(statements);
|
|
3407
|
+
return statements;
|
|
3492
3408
|
}
|
|
3493
3409
|
async generateParameterValidators(operation, operationName) {
|
|
3494
3410
|
const statements = [];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ng-openapi/zod",
|
|
3
|
-
"version": "0.0.2-pr-
|
|
3
|
+
"version": "0.0.2-pr-32-feature-zod-9008b87.0",
|
|
4
4
|
"description": "Zod validation plugin for ng-openapi - Generate Zod schemas from OpenAPI specifications",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"angular",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"peerDependencies": {
|
|
61
61
|
"ng-openapi": ">=0.2",
|
|
62
62
|
"ts-morph": "*",
|
|
63
|
-
"zod": ">=
|
|
63
|
+
"zod": ">=4"
|
|
64
64
|
},
|
|
65
65
|
"peerDependenciesMeta": {
|
|
66
66
|
"ng-openapi": {
|