Skip to content

Commit

Permalink
refactor: add enum for spec file's validation status (asyncapi#1363)
Browse files Browse the repository at this point in the history
Co-authored-by: souvik <souvikde.ns@gmail.com>
  • Loading branch information
peter-rr and Souvikns authored Apr 12, 2024
1 parent 95f15bd commit 819b0e7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/commands/validate.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
}
}
Expand Down
19 changes: 12 additions & 7 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand All @@ -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({
Expand All @@ -45,7 +50,7 @@ export function validationFlags({ logDiagnostics = true }: ValidationFlagsOption
options: Object.values(OutputFormat),
default: OutputFormat.STYLISH,
}),
'fail-severity': Flags.enum<SeveritytKind>({
'fail-severity': Flags.enum<SeverityKind>({
description: 'diagnostics of this level or above will trigger a failure exit code',
options: ['error', 'warn', 'info', 'hint'],
default: 'error',
Expand All @@ -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 = {}) {
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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);
}

0 comments on commit 819b0e7

Please sign in to comment.