Skip to content

Commit

Permalink
Adding a enum for validation
Browse files Browse the repository at this point in the history
  • Loading branch information
shawakash committed Nov 4, 2023
1 parent f7c00c6 commit 9bd0d29
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/commands/validate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Flags } from '@oclif/core';

import Command from '../base';
import { validate, validationFlags } from '../parser';
import { ValidationStatus, validate, validationFlags } from '../parser';
import { load } from '../models/SpecificationFile';
import { specWatcher } from '../globals';
import { watchFlag } from '../flags';
Expand All @@ -23,14 +23,14 @@ export default class Validate extends Command {
const { args, flags } = await this.parse(Validate); //NOSONAR
const filePath = args['spec-file'];
const watchMode = flags.watch;

const specFile = await load(filePath);
if (watchMode) {
specWatcher({ spec: specFile, handler: this, handlerName: 'validate' });
}

await validate(this, specFile, flags);
if(await validate(this, specFile, flags) == 'invalid') {
if(await validate(this, specFile, flags) == ValidationStatus.Invalid) {
this.exit(1);
}
this.exit(0);
Expand Down
11 changes: 8 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 Down Expand Up @@ -74,15 +79,15 @@ function logDiagnostics(diagnostics: Diagnostic[], command: Command, specFile: S
const logDiagnostics = options['log-diagnostics'];
const failSeverity = options['fail-severity'] ?? 'error';
const diagnosticsFormat = options['diagnostics-format'] ?? 'stylish';

const sourceString = specFile.toSourceString();
if (diagnostics.length) {
if (hasFailSeverity(diagnostics, failSeverity)) {
if (logDiagnostics) {
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,7 +98,7 @@ 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) {
Expand Down

0 comments on commit 9bd0d29

Please sign in to comment.