-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBaseRuleset.ts
More file actions
78 lines (71 loc) · 2.69 KB
/
BaseRuleset.ts
File metadata and controls
78 lines (71 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// SPDX-FileCopyrightText: 2025 Digg - Agency for Digital Government
//
// SPDX-License-Identifier: EUPL-1.2
import { RulesetInterface } from '../ruleinterface/RuleInterface.js';
import { CustomProperties } from '../ruleinterface/CustomProperties.js';
import { CustomFormatType } from './util/CustomOasVersion.js';
import { DiagnosticSeverity } from '@stoplight/types';
import Format from '@stoplight/spectral-formats';
import { RuleExecutionContext } from '../util/RuleExecutionContext.js';
export abstract class BaseRuleset implements RulesetInterface {
static customProperties: CustomProperties = { område: undefined!, id: '' };
static getCustomProperties(): CustomProperties {
return BaseRuleset.customProperties;
}
given: string | string[] = '';
message: string = '';
then: any = {};
description: string = '';
severity: DiagnosticSeverity = DiagnosticSeverity.Error;
formats: any = [];
constructor(context: RuleExecutionContext) {
this.#context = context;
}
#context: RuleExecutionContext;
protected trackRuleExecutionHandler(
targetVal: string,
_opts: string,
paths: string[],
serverity: DiagnosticSeverity,
subclassInfo: any,
moduleName: any,
subclassProperties: CustomProperties,
) {
this.#context.logRuleExecution(moduleName, subclassInfo, subclassProperties, this.severity.toString(), true, targetVal);
return [];
}
async initializeFormats(formats: CustomFormatType[] = []) {
this.formats = await this.loadFormats(formats);
}
async loadFormats(formats: CustomFormatType[]): Promise<any[]> {
try {
// If no formats specified, default to loading all available formats
// Code run if there is support for ES module dynamic import
return formats.length > 0
? formats.map((format) => this.mapCustomFormatToPkgFormat(format, Format)) // Map provided formats
: [Format.oas2, Format.oas3]; // Default to oas2 and oas3
} catch (e) {
// Fallback for CommonJS (Node environments(framework) not using ES modules)
const pkg = require('@stoplight/spectral-formats');
return formats.length > 0
? formats.map((format) => this.mapCustomFormatToPkgFormat(format, pkg))
: [pkg.oas2, pkg.oas3];
}
}
// Helper function to map the CustomFormatType to the actual formats
mapCustomFormatToPkgFormat(format: CustomFormatType, pkg: any): any {
// Map the formats based on the provided CustomFormatType
switch (format) {
case 'OAS2':
return pkg.oas2;
case 'OAS3':
return pkg.oas3;
case 'OAS3_0':
return pkg.oas3_0;
case 'OAS3_1':
return pkg.oas3_1;
default:
throw new Error(`Unknown format: ${format}`);
}
}
}