From 0b3ab054144219d89b422fe0152ecb9ed489b58c Mon Sep 17 00:00:00 2001 From: Hikaru Yoshino Date: Sun, 18 Aug 2024 18:21:44 +0900 Subject: [PATCH] fix: don't allow conversion if there are errors in the results of the validation --- packages/core/src/utils/logger.ts | 8 ++++---- packages/core/src/utils/validator.ts | 23 ++++++++++++++++++++--- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/packages/core/src/utils/logger.ts b/packages/core/src/utils/logger.ts index 20b0e7545..6181a64d9 100644 --- a/packages/core/src/utils/logger.ts +++ b/packages/core/src/utils/logger.ts @@ -48,27 +48,27 @@ export const createSuccessMessage = (backend?: string) => export const ibmOpenapiValidatorWarnings = ( warnings: { - path: string; + path: string[]; message: string; }[], ) => { log(chalk.yellow('(!) Warnings')); warnings.forEach((i) => - log(chalk.yellow(`Message : ${i.message}\nPath : ${i.path}`)), + log(chalk.yellow(`Message : ${i.message}\nPath : ${i.path.join(', ')}`)), ); }; export const ibmOpenapiValidatorErrors = ( errors: { - path: string; + path: string[]; message: string; }[], ) => { log(chalk.red('(!) Errors')); errors.forEach((i) => - log(chalk.red(`Message : ${i.message}\nPath : ${i.path}`)), + log(chalk.red(`Message : ${i.message}\nPath : ${i.path.join(', ')}`)), ); }; diff --git a/packages/core/src/utils/validator.ts b/packages/core/src/utils/validator.ts index 7eb4387c3..02575a805 100644 --- a/packages/core/src/utils/validator.ts +++ b/packages/core/src/utils/validator.ts @@ -15,13 +15,30 @@ const { Spectral } = require('@stoplight/spectral-core'); export const ibmOpenapiValidator = async (specs: OpenAPIObject) => { const spectral = new Spectral(); spectral.setRuleset(ibmOpenapiRuleset); - const { errors, warnings } = await spectral.run(specs); + const results = await spectral.run(specs); - if (warnings && warnings.length) { + const errors: { message: string; path: string[] }[] = []; + const warnings: { message: string; path: string[] }[] = []; + + for (const { severity, message, path } of results) { + const entry = { message, path }; + // 0: error, 1: "warning", see: https://github.com/IBM/openapi-validator/blob/a93e18a156108b6b946727d0b24bbcc69095b72e/packages/validator/src/spectral/spectral-validator.js#L222 + switch (severity) { + case 0: + errors.push(entry); + break; + case 1: + warnings.push(entry); + break; + } + } + + if (warnings.length) { ibmOpenapiValidatorWarnings(warnings); } - if (errors && errors.length) { + if (errors.length) { ibmOpenapiValidatorErrors(errors); + process.exit(1); } };