Skip to content

Commit 28c0f60

Browse files
authored
feat: add rule DOK.09
Signed-off-by: Mats Johansson <extern.mats.johansson@digg.se>
1 parent e60c1a2 commit 28c0f60

File tree

6 files changed

+138
-0
lines changed

6 files changed

+138
-0
lines changed

GUIDELINES.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Detta dokument specificerar reglerna som verktyget tillämpar.
3535
- [ID: DOK.06](#id-dok06)
3636
- [ID: DOK.07](#id-dok07)
3737
- [ID: DOK.08](#id-dok08)
38+
- [ID: DOK.09](#id-dok09)
3839
- [ID: DOK.15](#id-dok15)
3940
- [ID: DOK.17](#id-dok17)
4041
- [ID: DOK.19](#id-dok19)
@@ -240,6 +241,29 @@ _Ett av objekten räcker för att uppfylla regeln._
240241

241242
---
242243

244+
### ID: DOK.09
245+
246+
**Krav:** Kända problem och begränsningar SKALL finnas tydlig beskrivna i dokumentationen.
247+
248+
**Typ:** SKALL
249+
250+
**JSON Path Plus-uttryck:**
251+
252+
```
253+
$
254+
```
255+
256+
**Förklaring:**
257+
Regeln förutsätter att det finns en förekomst av något av objekten `info.description` och `externalDocs.description`, och att de innehåller någon av strängarna "limit", "begränsning" eller "problem". Alternativt så räcker det om det finns en förekomst av `info.x-limitations`.
258+
259+
**Exempel:**
260+
261+
![Exempelbild på ovan nämnda objekts placering i en OpenAPI Description](images/dok9.png)
262+
263+
_Ett av objekten räcker för att uppfylla regeln, men "limit", "begränsning" eller "problem" behöver nämnas i `description`-fälten för att de ska räknas._
264+
265+
---
266+
243267
### ID: DOK.15
244268

245269
**Krav:** I dokumentationen av API:et SKALL exempel på API:ets fråga (en:request) och svar (en:reply) finnas i sin helhet.

REUSE.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ path = [
3535
"images/dok6-2.png",
3636
"images/dok7.png",
3737
"images/dok8.png",
38+
"images/dok9.png",
3839
"images/dot1-2.png",
3940
"images/dot1.png",
4041
"images/dot4.png",

images/dok9.png

21 KB
Loading

rulesets/DokRules.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,64 @@ export class Dok08 extends BaseRuleset {
286286
}
287287
severity = DiagnosticSeverity.Error;
288288
}
289+
export class Dok09 extends BaseRuleset {
290+
static customProperties: CustomProperties = {
291+
område: 'Dokumentation',
292+
id: 'DOK.09',
293+
};
294+
given = '$';
295+
message = 'Kända problem och begränsningar SKALL finnas tydlig beskrivna i dokumentationen.';
296+
then = [
297+
{
298+
function: (targetVal: any, _opts: string, paths: string[]) => {
299+
const includesLimitTerm = (description: string): boolean => {
300+
if (description) {
301+
const limitTerms = ['limit', 'begränsning', 'problem'];
302+
return limitTerms.some((term) => description.includes(term));
303+
} else {
304+
return false;
305+
}
306+
};
307+
308+
const externalDocs = targetVal?.externalDocs;
309+
const info = targetVal?.info;
310+
if (
311+
info?.['x-limitations'] ||
312+
includesLimitTerm(info?.description?.toLowerCase()) ||
313+
includesLimitTerm(externalDocs?.description?.toLowerCase())
314+
) {
315+
return [];
316+
}
317+
318+
return [
319+
{
320+
message: this.message,
321+
severity: this.severity,
322+
paths: paths,
323+
},
324+
];
325+
},
326+
},
327+
{
328+
function: (targetVal: string, _opts: string, paths: string[]) => {
329+
this.trackRuleExecutionHandler(
330+
JSON.stringify(targetVal, null, 2),
331+
_opts,
332+
paths,
333+
this.severity,
334+
this.constructor.name,
335+
moduleName,
336+
Dok09.customProperties,
337+
);
338+
},
339+
},
340+
];
341+
constructor() {
342+
super();
343+
super.initializeFormats(['OAS2', 'OAS3']);
344+
}
345+
severity = DiagnosticSeverity.Error;
346+
}
289347
export class Dok19 extends BaseRuleset {
290348
static customProperties: CustomProperties = {
291349
område: 'Dokumentation',

tests/dok.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,60 @@ testRule('Dok08', [
317317
],
318318
},
319319
]);
320+
testRule('Dok09', [
321+
{
322+
name: 'giltigt testfall - info.description innehåller keyword',
323+
document: {
324+
openapi: '3.1.0',
325+
info: { version: '1.0', description: 'något om limits' },
326+
},
327+
errors: [],
328+
},
329+
{
330+
name: 'giltigt testfall - externalDocs.description innehåller keyword',
331+
document: {
332+
openapi: '3.1.0',
333+
info: { version: '1.0' },
334+
externalDocs: { description: 'något om begränsningar' },
335+
},
336+
errors: [],
337+
},
338+
{
339+
name: 'giltigt testfall - info.x-limitations är definierad',
340+
document: {
341+
openapi: '3.1.0',
342+
info: { version: '1.0', 'x-limitations': 'behöver bara existera utan keywords' },
343+
},
344+
errors: [],
345+
},
346+
{
347+
name: 'ogiltigt testfall - samtliga fält saknas',
348+
document: {
349+
openapi: '3.1.0',
350+
info: { version: '1.0' },
351+
},
352+
errors: [
353+
{
354+
message: 'Kända problem och begränsningar SKALL finnas tydlig beskrivna i dokumentationen.',
355+
severity: DiagnosticSeverity.Error,
356+
},
357+
],
358+
},
359+
{
360+
name: 'ogiltigt testfall - fält finns men saknar samtliga keywords',
361+
document: {
362+
openapi: '3.1.0',
363+
info: { version: '1.0', description: 'test' },
364+
externalDocs: { description: 'test' },
365+
},
366+
errors: [
367+
{
368+
message: 'Kända problem och begränsningar SKALL finnas tydlig beskrivna i dokumentationen.',
369+
severity: DiagnosticSeverity.Error,
370+
},
371+
],
372+
},
373+
]);
320374
testRule('Dok17', [
321375
{
322376
name: 'giltigt testfall',

tests/util/rulesetTest.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const ruleTypes = [
6060
DokRules.Dok06,
6161
DokRules.Dok07,
6262
DokRules.Dok08,
63+
DokRules.Dok09,
6364
FelRules.Fel01,
6465
FelRules.Fel02,
6566
FnsRules.Fns08,

0 commit comments

Comments
 (0)