Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add enum for spec file's validation status #1363

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Loading