|
1 | 1 | import { OpenAPIV3 } from "openapi-types"; |
2 | | -import { logInfo } from "src/helpers/cli.helper"; |
| 2 | +import { chk } from "src/helpers/chalk.helper"; |
3 | 3 | import { DEFAULT_GENERATE_OPTIONS } from "./const/options.const"; |
| 4 | +import { VALIDATION_ERROR_TYPE_TITLE } from "./const/validation.const"; |
4 | 5 | import { getDataFromOpenAPIDoc } from "./core/getDataFromOpenAPIDoc"; |
5 | 6 | import { GenerateType } from "./types/generate"; |
6 | 7 | import { GenerateOptions } from "./types/options"; |
| 8 | +import { ValidationErrorType } from "./types/validation"; |
7 | 9 | import { getFileName } from "./utils/file.utils"; |
8 | 10 | import { getTagFileName } from "./utils/generate/generate.utils"; |
| 11 | +import { groupByType } from "./utils/validation.utils"; |
9 | 12 |
|
10 | | -export function checkOpenAPIDoc({ |
11 | | - openApiDoc, |
12 | | - options: cliOptions, |
13 | | -}: { |
14 | | - openApiDoc: OpenAPIV3.Document; |
15 | | - options?: Partial<GenerateOptions>; |
16 | | -}) { |
| 13 | +export function checkOpenAPIDoc(openApiDoc: OpenAPIV3.Document, cliOptions?: Partial<GenerateOptions>) { |
17 | 14 | const options = { ...DEFAULT_GENERATE_OPTIONS, ...cliOptions } as GenerateOptions; |
18 | 15 |
|
19 | | - const { resolver, data, validationErrorMessages } = getDataFromOpenAPIDoc({ openApiDoc, options }); |
| 16 | + const { resolver, data } = getDataFromOpenAPIDoc(openApiDoc, options); |
20 | 17 |
|
21 | | - const errorMessages = [...validationErrorMessages, ...resolver.validationErrorMessages]; |
22 | | - if (errorMessages.length > 0) { |
23 | | - logInfo(`Issues:\n${errorMessages.map((message) => `- ${message}`).join("\n")}`); |
| 18 | + if (resolver.validationErrors.length > 0) { |
| 19 | + const groupedErrors = groupByType(resolver.validationErrors); |
| 20 | + Object.entries(groupedErrors).forEach(([type, errorMessages]) => { |
| 21 | + console.log( |
| 22 | + `${chk.red(`${VALIDATION_ERROR_TYPE_TITLE[type as ValidationErrorType]}:`)}\n${errorMessages.map((message) => `- ${message}`).join("\n")}\n`, |
| 23 | + ); |
| 24 | + }); |
24 | 25 | } else { |
25 | 26 | const outputs = [...data.keys()].reduce((acc, tag) => { |
26 | | - const excludedTagIndex = options.excludeTags?.findIndex( |
27 | | - (excludeTag) => excludeTag.toLocaleLowerCase() === tag.toLocaleLowerCase(), |
28 | | - ); |
29 | | - const isExcludedTag = excludedTagIndex !== -1; |
30 | | - if (isExcludedTag) { |
31 | | - return acc; |
32 | | - } |
33 | | - |
34 | | - return [ |
35 | | - ...acc, |
36 | | - ...[GenerateType.Models, GenerateType.Endpoints, GenerateType.Queries].map((type) => |
37 | | - getFileName({ output: options.output, fileName: getTagFileName({ tag, type, options }) }), |
38 | | - ), |
39 | | - ]; |
| 27 | + const excludedTag = options.excludeTags.find((excludeTag) => excludeTag.toLowerCase() === tag.toLowerCase()); |
| 28 | + return excludedTag ? acc : [...acc, ...getTagOutputFileNames(tag, options)]; |
40 | 29 | }, [] as string[]); |
41 | | - logInfo(`Output:\n${outputs.map((message) => `- ${message}`).join("\n")}`); |
| 30 | + console.log(`${chk.green("Outputs:")}\n${outputs.map((output) => `- ${output}`).join("\n")}\n`); |
42 | 31 | } |
43 | 32 |
|
44 | | - return errorMessages; |
| 33 | + return resolver.validationErrors; |
| 34 | +} |
| 35 | + |
| 36 | +function getTagOutputFileNames(tag: string, options: GenerateOptions) { |
| 37 | + return [GenerateType.Models, GenerateType.Endpoints, GenerateType.Queries].map((type) => |
| 38 | + getFileName({ output: options.output, fileName: getTagFileName({ tag, type, options }) }), |
| 39 | + ); |
45 | 40 | } |
0 commit comments