-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAmeRules.ts
More file actions
325 lines (313 loc) · 10.7 KB
/
AmeRules.ts
File metadata and controls
325 lines (313 loc) · 10.7 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// SPDX-FileCopyrightText: 2025 Digg - Agency for Digital Government
//
// SPDX-License-Identifier: EUPL-1.2
import { undefined as undefinedFunc, pattern } from '@stoplight/spectral-functions';
import { DiagnosticSeverity } from '@stoplight/types';
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';
enum CasingType {
snake = 'snake',
camel = 'camel',
}
interface State {
casingCamelState: boolean;
casingSnakeState: boolean;
propertyName: string;
schemaKey: string;
casingTypeName: CasingType;
}
interface StateExecutionLog {
[key: string]: State[];
}
export class Ame07 extends BaseRuleset {
static customProperties: CustomProperties = {
område: 'API Message',
id: 'AME.07',
};
description = 'Fältnamn BÖR använda tecken som är alfanumeriska.';
message = 'Fältnamn BÖR använda tecken som är alfanumeriska.';
given = '$.components.schemas..properties[*]~';
then = [
{
function: pattern,
functionOptions: {
match: '^[a-zA-Z0-9_]+$',
},
},
{
function: (targetVal: string, _opts: string, paths: string[]) => {
this.trackRuleExecutionHandler(
JSON.stringify(targetVal, null, 2),
_opts,
paths,
this.severity,
this.constructor.name,
moduleName,
Ame07.customProperties,
);
},
},
];
constructor(context: RuleExecutionContext) {
super(context);
super.initializeFormats(['OAS3']);
}
severity = DiagnosticSeverity.Warning;
}
export class Ame04 extends BaseRuleset {
static customProperties: CustomProperties = {
område: 'API Message',
id: 'AME.04',
};
description = 'För fältnamn i request och response body BÖR camelCase eller snake_case notation användas.';
message = 'För fältnamn i request och response body BÖR camelCase eller snake_case notation användas.';
given = '$.components.schemas..properties[*]~';
then = [
{
function: pattern,
functionOptions: {
match: '^(?:[a-z]+(?:_[a-z]+)*|[a-z]+(?:[A-Z][a-z]*)*)$',
},
},
{
function: (targetVal: string, _opts: string, paths: string[]) => {
this.trackRuleExecutionHandler(
JSON.stringify(targetVal, null, 2),
_opts,
paths,
this.severity,
this.constructor.name,
moduleName,
Ame04.customProperties,
);
},
},
];
constructor(context: RuleExecutionContext) {
super(context);
super.initializeFormats(['OAS3']);
}
severity = DiagnosticSeverity.Warning;
}
export class Ame01 extends BaseRuleset {
static errorMessage = 'Datamodellen för en representation BÖR beskrivas med JSON enligt senaste versionen, RFC 8259.';
static customProperties: CustomProperties = {
område: 'API Message',
id: 'AME.01',
};
description = 'Denna regel validerar att request och response är application/json.';
message = Ame01.errorMessage;
given = ['$.paths[*][*].responses[?(@property < 400)].content', '$.paths.*.*.requestBody.content'];
then = [
{
function: (targetVal: any, _opts: string, paths: string[]) => {
const valid: boolean = Object.keys(targetVal ?? {}).some((property) => isValidApplicationJson(property));
if (!valid) {
return [
{
message: this.message,
severity: this.severity,
},
];
} else {
return [];
}
},
},
{
function: (targetVal: string, _opts: string, paths: string[]) => {
this.trackRuleExecutionHandler(
JSON.stringify(targetVal, null, 2),
_opts,
paths,
this.severity,
this.constructor.name,
moduleName,
Ame01.customProperties,
);
},
},
];
constructor(context: RuleExecutionContext) {
super(context);
super.initializeFormats(['OAS3']);
}
severity = DiagnosticSeverity.Warning;
}
export class Ame02 extends BaseRuleset {
static errorMessage =
"Det BÖR förutsättas att alla request headers som standard använder 'Accept' med värde 'application/json'";
static customProperties: CustomProperties = {
område: 'API Message',
id: 'AME.02',
};
description = 'Denna regel validerar att response är application/json.';
message = Ame02.errorMessage;
given = ['$.paths.*.*.requestBody.content'];
then = [
{
function: (targetVal: any, _opts: string, paths: string[]) => {
const valid: boolean = Object.keys(targetVal ?? {}).some((property) => isValidApplicationJson(property));
if (!valid) {
return [
{
message: this.message,
severity: this.severity,
},
];
} else {
return [];
}
},
},
{
function: (targetVal: string, _opts: string, paths: string[]) => {
this.trackRuleExecutionHandler(
JSON.stringify(targetVal, null, 2),
_opts,
paths,
this.severity,
this.constructor.name,
moduleName,
Ame02.customProperties,
);
},
},
];
constructor(context: RuleExecutionContext) {
super(context);
super.initializeFormats(['OAS3']);
}
severity = DiagnosticSeverity.Warning;
}
export class Ame05 extends BaseRuleset {
static customProperties: CustomProperties = {
område: 'API Message',
id: 'AME.05',
};
description = 'Inom ett API SKALL namnsättningen vara konsekvent, dvs blanda inte camelCase och snake_case.';
message = 'Inom ett API SKALL namnsättningen vara konsekvent, dvs blanda inte camelCase och snake_case.';
given = '$.components.schemas';
then = [
{
function: (targetVal: string, _opts: string, paths) => {
const result: any = [];
const stateExecutionLogDictionary: StateExecutionLog = {};
const data = JSON.parse(JSON.stringify(targetVal, null, 2)); // Need to use both stringfy and parse in order to parse to JSON correctly here...hm ?
for (const key in data) {
// Loop throough each Schema object found in given expression
const propertyNames: string[] = parsePropertyNames(key, data); // Pick out properties of object
propertyNames.forEach((name) => {
/**
* RegExp for scenarios
*/
const snakeCasePattern: RegExp = /^[a-z]+(?:_[a-z]+)*$/;
const camelCasePattern: RegExp = /^([a-z]+)(?:([A-Z]{1})([a-z]+))+$/;
const lowercasePattern: RegExp = /^[a-z]+$/;
const stateKey = `${'casingCamelState'}:${'casingSnakeState'}:${Math.floor(Math.random() * 1000)}`;
if (!stateExecutionLogDictionary[stateKey]) {
// Init dictionary
stateExecutionLogDictionary[stateKey] = [];
}
if (!lowercasePattern.test(name)) {
// When lowercasepattern we cant test camel/snake pattern for sure
if (snakeCasePattern.test(name)) {
//Match for snakeCase / store info about it
stateExecutionLogDictionary[stateKey].push({
casingCamelState: false,
casingSnakeState: true,
propertyName: name,
schemaKey: key,
casingTypeName: CasingType.snake,
});
} else if (camelCasePattern.test(name)) {
//Match for camelcase / store info about it
stateExecutionLogDictionary[stateKey].push({
casingCamelState: true,
casingSnakeState: false,
propertyName: name,
schemaKey: key,
casingTypeName: CasingType.camel,
});
}
}
});
}
//Find possible vialoations
const possibleViolations: string[] = this.findPropertyViolations(stateExecutionLogDictionary);
// Notify the user if both snake case and camel case patterns are present or [] if ok
for (const key of possibleViolations) {
const properties = stateExecutionLogDictionary[key];
// Push items with casingCamelState or casingSnakeState set to true to the result array
properties.forEach((property) => {
result.push({
path: [...paths.path, property.schemaKey, 'properties', property.propertyName],
message: this.message,
severity: this.severity,
});
});
}
return result;
},
},
{
function: (targetVal: string, _opts: string, paths: string[]) => {
this.trackRuleExecutionHandler(
JSON.stringify(targetVal, null, 2),
_opts,
paths,
this.severity,
this.constructor.name,
moduleName,
Ame05.customProperties,
);
},
},
];
/**
* Search a dictionary for possbile vialoations.
* Rule is you cant mix casing types[snake/camel] within the same schemaObject
* Dictionary contains the look of interface State, declared in module
* @param log
* @returns array of keys in the dictionary that violates the conditon
*/
private findPropertyViolations(log: StateExecutionLog): string[] {
const violations: string[] = [];
const invalidEntries = new Set<string>();
// Iterate over each entry in the dictionary
const schemaKeys = Object.keys(log);
for (let i = 0; i < schemaKeys.length - 1; i++) {
const schemaKey1 = schemaKeys[i];
const states1 = log[schemaKey1];
for (let j = i + 1; j < schemaKeys.length; j++) {
const schemaKey2 = schemaKeys[j];
const states2 = log[schemaKey2];
// Check if there are states with the same schemaKey value and mix of casing types
for (const state1 of states1) {
for (const state2 of states2) {
if (
state1.schemaKey === state2.schemaKey &&
((state1.casingCamelState && state2.casingSnakeState) ||
(state1.casingSnakeState && state2.casingCamelState))
) {
invalidEntries.add(schemaKey1);
invalidEntries.add(schemaKey2);
break;
}
}
}
}
}
return Array.from(invalidEntries);
}
constructor(context: RuleExecutionContext) {
super(context);
super.initializeFormats(['OAS3']);
}
severity = DiagnosticSeverity.Error;
}
export default { Ame01, Ame02, Ame05, Ame04, Ame07 };