From 819b0e7956a0ffa3564af2a0fcfa3525b3cd5527 Mon Sep 17 00:00:00 2001 From: Pedro Ramos Date: Fri, 12 Apr 2024 08:43:15 +0200 Subject: [PATCH] refactor: add enum for spec file's validation status (#1363) Co-authored-by: souvik --- src/commands/validate.ts | 4 ++-- src/parser.ts | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/commands/validate.ts b/src/commands/validate.ts index d0eb1c211ad..e3503db2a95 100644 --- a/src/commands/validate.ts +++ b/src/commands/validate.ts @@ -1,6 +1,6 @@ import { Flags } from '@oclif/core'; import Command from '../base'; -import { validate, validationFlags } from '../parser'; +import { validate, validationFlags, ValidationStatus } from '../parser'; import { load } from '../models/SpecificationFile'; import { specWatcher } from '../globals'; import { watchFlag } from '../flags'; @@ -31,7 +31,7 @@ export default class Validate extends Command { const result = await validate(this, this.specFile, flags); this.metricsMetadata.validation_result = result; - if (result === 'invalid') { + if (result === ValidationStatus.INVALID) { process.exitCode = 1; } } diff --git a/src/parser.ts b/src/parser.ts index 8e35adc56f6..0f5f3bece81 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -12,7 +12,7 @@ import type { Diagnostic } from '@asyncapi/parser/cjs'; import type Command from './base'; import type { Specification } from './models/SpecificationFile'; -export type SeveritytKind = 'error' | 'warn' | 'info' | 'hint'; +export type SeverityKind = 'error' | 'warn' | 'info' | 'hint'; export { convertToOldAPI }; @@ -33,6 +33,11 @@ export interface ValidationFlagsOptions { logDiagnostics?: boolean; } +export enum ValidationStatus { + INVALID = 'invalid', + VALID = 'valid', +} + export function validationFlags({ logDiagnostics = true }: ValidationFlagsOptions = {}) { return { 'log-diagnostics': Flags.boolean({ @@ -45,7 +50,7 @@ export function validationFlags({ logDiagnostics = true }: ValidationFlagsOption options: Object.values(OutputFormat), default: OutputFormat.STYLISH, }), - 'fail-severity': Flags.enum({ + 'fail-severity': Flags.enum({ description: 'diagnostics of this level or above will trigger a failure exit code', options: ['error', 'warn', 'info', 'hint'], default: 'error', @@ -56,7 +61,7 @@ export function validationFlags({ logDiagnostics = true }: ValidationFlagsOption interface ValidateOptions { 'log-diagnostics'?: boolean; 'diagnostics-format'?: `${OutputFormat}`; - 'fail-severity'?: SeveritytKind; + 'fail-severity'?: SeverityKind; } export async function validate(command: Command, specFile: Specification, options: ValidateOptions = {}) { @@ -82,7 +87,7 @@ function logDiagnostics(diagnostics: Diagnostic[], command: Command, specFile: S command.logToStderr(`\n${sourceString} and/or referenced documents have governance issues.`); command.logToStderr(formatOutput(diagnostics, diagnosticsFormat, failSeverity)); } - return 'invalid'; + return ValidationStatus.INVALID; } if (logDiagnostics) { @@ -93,10 +98,10 @@ function logDiagnostics(diagnostics: Diagnostic[], command: Command, specFile: S command.log(`\n${sourceString} is valid! ${sourceString} and referenced documents don't have governance issues.`); } - return 'valid'; + return ValidationStatus.VALID; } -export function formatOutput(diagnostics: Diagnostic[], format: `${OutputFormat}`, failSeverity: SeveritytKind) { +export function formatOutput(diagnostics: Diagnostic[], format: `${OutputFormat}`, failSeverity: SeverityKind) { const options = { failSeverity: getDiagnosticSeverity(failSeverity) }; switch (format) { case 'stylish': return stylish(diagnostics, options); @@ -110,7 +115,7 @@ export function formatOutput(diagnostics: Diagnostic[], format: `${OutputFormat} } } -function hasFailSeverity(diagnostics: Diagnostic[], failSeverity: SeveritytKind) { +function hasFailSeverity(diagnostics: Diagnostic[], failSeverity: SeverityKind) { const diagnosticSeverity = getDiagnosticSeverity(failSeverity); return diagnostics.some(diagnostic => diagnostic.severity <= diagnosticSeverity); }