Skip to content
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
7 changes: 5 additions & 2 deletions src/cli-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Issue } from './util/Issue.js';
import { parseApiSpecInput,detectSpecFormatPreference, ParseResult} from './util/validateUtil.js';
import { SpecParseError } from './util/RapLPSpecParseError.js';
import * as path from 'node:path';
import { RuleExecutionContext } from './util/RuleExecutionContext.js';

declare var AggregateError: {
prototype: AggregateError;
Expand Down Expand Up @@ -47,6 +48,8 @@ export async function execCLI<T extends CliArgs>(argv: T) {
const logErrorFilePath = argv.logError as string | undefined;
const logDiagnosticFilePath = argv.logDiagnostic as string | undefined;
const strict = argv.strict as boolean ?? false;
const context = new RuleExecutionContext();


// Schemevalidation and Spectral Document creation ----------
let apiSpecDocument: SpectralDocument;
Expand Down Expand Up @@ -125,7 +128,7 @@ export async function execCLI<T extends CliArgs>(argv: T) {

try {
// Import and create rule instances in RAP-LP
const enabledRulesAndCategorys = await importAndCreateRuleInstances(ruleCategories);
const enabledRulesAndCategorys = await importAndCreateRuleInstances(context,ruleCategories);
// Load API specification into a Document object
const parser: IParser<any> = (parseResult.format === 'json' ? Parsers.Json : Parsers.Yaml) as unknown as IParser<any>;
apiSpecDocument = new SpectralDocument(parseResult.raw, parser, apiSpecFileName);
Expand All @@ -138,7 +141,7 @@ export async function execCLI<T extends CliArgs>(argv: T) {
customSpectral.setRuleset(enabledRulesAndCategorys.rules);
const result = await customSpectral.run(apiSpecDocument);

const customDiagnostic = new RapLPDiagnostic();
const customDiagnostic = new RapLPDiagnostic(context);
customDiagnostic.processRuleExecutionInformation(result, enabledRulesAndCategorys.instanceCategoryMap);
const diagnosticReports: DiagnosticReport[] = customDiagnostic.processDiagnosticInformation();
if (argv.dex != null) {
Expand Down
6 changes: 4 additions & 2 deletions src/routes/urlValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import { UrlContentDto } from '../model/UrlContentDto.js';
import { importAndCreateRuleInstances } from '../util/ruleUtil.js';
import { ERROR_TYPE, RapLPBaseApiError } from '../util/RapLPBaseApiErrorHandling.js';
import { loadUrlValidationConfiguration } from '../util/urlValidationConfig.js';
import { RuleExecutionContext } from '../util/RuleExecutionContext.js';

export const registerUrlValidationRoutes = (app: Express, urlValidationConfigFile?: string) => {
const config = loadUrlValidationConfiguration(urlValidationConfigFile);

// Route for validating openapi yaml from url.
app.post('/api/v1/validation/url', async (req, res, next) => {
try {
const context = new RuleExecutionContext();
const dto: UrlContentDto = req.body;

if (config?.urlMatchRegex && !dto.url.match(config.urlMatchRegex)) {
Expand All @@ -35,9 +37,9 @@ export const registerUrlValidationRoutes = (app: Express, urlValidationConfigFil

const apiSpecDocument = new Document(yamlContentString, Parsers.Yaml, '');

const rules = await importAndCreateRuleInstances(dto.categories);
const rules = await importAndCreateRuleInstances(context, dto.categories);

const result = await processApiSpec(rules, apiSpecDocument);
const result = await processApiSpec(context,rules, apiSpecDocument);
res.send(result);
} catch (e) {
next(e);
Expand Down
16 changes: 9 additions & 7 deletions src/routes/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ import { ApiInfo } from '../model/ApiInfo.js';
import { validationRules } from '../model/validationRules.js';
import { ExcelReportProcessor } from '../util/excelReportProcessor.js';
import { DiagnosticReport, RapLPDiagnostic } from '../util/RapLPDiagnostic.js';
import { Issue } from '../util/Issue.js';
import * as IssueHelper from '../util/RapLPIssueHelpers.js';
import { parseApiSpecInput,detectSpecFormatPreference, ParseResult} from '../util/validateUtil.js';
import { SpecParseError } from '../util/RapLPSpecParseError.js';
import { ProblemDetailsDTO } from '../model/ProblemDetailsDto.js';
import { SpecValidationRequestDto } from '../model/SpecValidationRequestDto.js';
import { ERROR_TYPE, RapLPBaseApiError } from '../util/RapLPBaseApiErrorHandling.js';
import type { IParser } from '@stoplight/spectral-parsers';
import { stringify } from 'node:querystring';
import { RuleExecutionContext } from '../util/RuleExecutionContext.js';



export const registerValidationRoutes = (app: Express) => {
// Route for raw content upload.
app.post('/api/v1/validation/validate', async (req, res, next) => {
try {
const context = new RuleExecutionContext();
const yamlContent: YamlContentDto = req.body;

let yamlContentString: string;
Expand All @@ -37,9 +37,9 @@ export const registerValidationRoutes = (app: Express) => {

const apiSpecDocument = new Document(yamlContentString, Parsers.Yaml, '');

const rules = await importAndCreateRuleInstances(yamlContent.categories);
const rules = await importAndCreateRuleInstances(context, yamlContent.categories);

const result = await processApiSpec(rules, apiSpecDocument);
const result = await processApiSpec(context,rules, apiSpecDocument);
res.send(result);
} catch (e) {
next(e);
Expand All @@ -59,6 +59,7 @@ export const registerValidationRoutes = (app: Express) => {
app.post('/api/v1/validation/generate-report', async (req, res, next): Promise<any> => {
try {
const data = req.body;
const context = new RuleExecutionContext();

if (!data || !data.result || !Array.isArray(data.result)) {
return res.status(400).json({ error: 'Invalid data format. Expected an object with a "result" array.' });
Expand All @@ -70,7 +71,7 @@ export const registerValidationRoutes = (app: Express) => {
const ruleCategories = data.categories && data.categories.length > 0 ? data.categories : undefined;

const enabledRulesAndCategorys = await importAndCreateRuleInstances(ruleCategories);
const customDiagnostic = new RapLPDiagnostic();
const customDiagnostic = new RapLPDiagnostic(context);
customDiagnostic.processRuleExecutionInformation(data.result, enabledRulesAndCategorys.instanceCategoryMap);
const diagnosticReports: DiagnosticReport[] = customDiagnostic.processDiagnosticInformation();

Expand All @@ -93,6 +94,7 @@ export const registerValidationRoutes = (app: Express) => {
*/
app.post('/api/v1/validation/validatespec', async (req, res, next) => {
try {
const context = new RuleExecutionContext();
const body: SpecValidationRequestDto = req.body;

//0.5 Check input
Expand Down Expand Up @@ -146,8 +148,8 @@ export const registerValidationRoutes = (app: Express) => {
// 5. No strict-errors → run raplp ruleengine
const parser: IParser<any> = (parseResult.format === 'json' ? Parsers.Json : Parsers.Yaml) as unknown as IParser<any>;
const apiSpecDocument = new Document(parseResult.raw, parser, '');
const rules = await importAndCreateRuleInstances(categories);
const result = await processApiSpec(rules, apiSpecDocument);
const rules = await importAndCreateRuleInstances(context, categories);
const result = await processApiSpec(context, rules, apiSpecDocument);

const hasRuleViolations = result.result.some(
d =>d.allvarlighetsgrad === 'ERROR' || d.allvarlighetsgrad === 'WARNING'
Expand Down
21 changes: 11 additions & 10 deletions src/rulesets/AmeRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { parsePropertyNames } from './rulesetUtil.js';
import { CustomProperties } from '../ruleinterface/CustomProperties.js';
import { BaseRuleset } from './BaseRuleset.js';
import { isValidApplicationJson } from './util/AmeRulesUtil.js';
import { RuleExecutionContext } from '../util/RuleExecutionContext.js';

const moduleName: string = 'AmeRules.js';

Expand Down Expand Up @@ -55,8 +56,8 @@ export class Ame07 extends BaseRuleset {
},
},
];
constructor() {
super();
constructor(context: RuleExecutionContext) {
super(context);
super.initializeFormats(['OAS3']);
}
severity = DiagnosticSeverity.Warning;
Expand Down Expand Up @@ -91,8 +92,8 @@ export class Ame04 extends BaseRuleset {
},
},
];
constructor() {
super();
constructor(context: RuleExecutionContext) {
super(context);
super.initializeFormats(['OAS3']);
}
severity = DiagnosticSeverity.Warning;
Expand Down Expand Up @@ -137,8 +138,8 @@ export class Ame01 extends BaseRuleset {
},
},
];
constructor() {
super();
constructor(context: RuleExecutionContext) {
super(context);
super.initializeFormats(['OAS3']);
}
severity = DiagnosticSeverity.Warning;
Expand Down Expand Up @@ -185,8 +186,8 @@ export class Ame02 extends BaseRuleset {
},
},
];
constructor() {
super();
constructor(context: RuleExecutionContext) {
super(context);
super.initializeFormats(['OAS3']);
}
severity = DiagnosticSeverity.Warning;
Expand Down Expand Up @@ -315,8 +316,8 @@ export class Ame05 extends BaseRuleset {
}
return Array.from(invalidEntries);
}
constructor() {
super();
constructor(context: RuleExecutionContext) {
super(context);
super.initializeFormats(['OAS3']);
}
severity = DiagnosticSeverity.Error;
Expand Down
11 changes: 6 additions & 5 deletions src/rulesets/ArqRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { DiagnosticSeverity } from '@stoplight/types';
import { CustomProperties } from '../ruleinterface/CustomProperties.js';
import { BaseRuleset } from './BaseRuleset.js';
import { isValidApplicationJson } from './rulesetUtil.js';
import { RuleExecutionContext } from '../util/RuleExecutionContext.js';

const moduleName: string = 'ArqRules.js';

Expand Down Expand Up @@ -149,8 +150,8 @@ export class Arq01 extends BaseRuleset {
},
},
];
constructor() {
super();
constructor(context: RuleExecutionContext) {
super(context);
super.initializeFormats(['OAS3']);
}
severity = DiagnosticSeverity.Warning;
Expand Down Expand Up @@ -215,9 +216,9 @@ export class Arq03 extends BaseRuleset {
},
},
];
constructor() {
super();
super.initializeFormats(['OAS2', 'OAS3']);
constructor(context: RuleExecutionContext) {
super(context);
super.initializeFormats(['OAS3']);
}
severity = DiagnosticSeverity.Warning;
}
Expand Down
15 changes: 11 additions & 4 deletions src/rulesets/BaseRuleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { RulesetInterface } from '../ruleinterface/RuleInterface.js';
import { CustomProperties } from '../ruleinterface/CustomProperties.js';
import { CustomFormatType } from './util/CustomOasVersion.js';
import { DiagnosticSeverity } from '@stoplight/types';
import { logRuleExecution } from '../util/RuleExecutionStatusModule.js';
import Format from '@stoplight/spectral-formats';
import { RuleExecutionContext } from '../util/RuleExecutionContext.js';

export class BaseRuleset implements RulesetInterface {
export abstract class BaseRuleset implements RulesetInterface {
static customProperties: CustomProperties = { område: undefined!, id: '' };
static getCustomProperties(): CustomProperties {
return BaseRuleset.customProperties;
Expand All @@ -22,7 +22,14 @@ export class BaseRuleset implements RulesetInterface {

formats: any = [];

trackRuleExecutionHandler(
constructor(context: RuleExecutionContext) {
this.#context = context;

}
#context: RuleExecutionContext;


protected trackRuleExecutionHandler(
targetVal: string,
_opts: string,
paths: string[],
Expand All @@ -31,7 +38,7 @@ export class BaseRuleset implements RulesetInterface {
moduleName: any,
subclassProperties: CustomProperties,
) {
logRuleExecution(moduleName, subclassInfo, subclassProperties, this.severity.toString(), true, targetVal);
this.#context.logRuleExecution(moduleName, subclassInfo, subclassProperties, this.severity.toString(), true, targetVal);
return [];
}
async initializeFormats(formats: CustomFormatType[] = []) {
Expand Down
Loading