Skip to content

Commit 45e817f

Browse files
authored
fix(ruleset-migrator): transform functions under overrides (stoplightio#2459)
1 parent deb85e2 commit 45e817f

File tree

5 files changed

+127
-1
lines changed

5 files changed

+127
-1
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const { truthy } = require('@stoplight/spectral-functions');
2+
const { oas } = require('@stoplight/spectral-rulesets');
3+
module.exports = {
4+
extends: [oas],
5+
aliases: {
6+
OperationObject: ['#PathItem[get,put,post,delete,options,head,patch,trace]'],
7+
PathItem: ['$.paths[*]'],
8+
},
9+
overrides: [
10+
{
11+
files: ['*'],
12+
rules: {
13+
'operation-description': {
14+
given: '#OperationObject',
15+
then: {
16+
field: 'summary',
17+
function: truthy,
18+
},
19+
severity: 'warn',
20+
},
21+
},
22+
},
23+
],
24+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { truthy } from '@stoplight/spectral-functions';
2+
import { oas } from '@stoplight/spectral-rulesets';
3+
export default {
4+
extends: [oas],
5+
aliases: {
6+
OperationObject: ['#PathItem[get,put,post,delete,options,head,patch,trace]'],
7+
PathItem: ['$.paths[*]'],
8+
},
9+
overrides: [
10+
{
11+
files: ['*'],
12+
rules: {
13+
'operation-description': {
14+
given: '#OperationObject',
15+
then: {
16+
field: 'summary',
17+
function: truthy,
18+
},
19+
severity: 'warn',
20+
},
21+
},
22+
},
23+
],
24+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
extends:
2+
- 'spectral:oas'
3+
aliases:
4+
OperationObject:
5+
- '#PathItem[get,put,post,delete,options,head,patch,trace]'
6+
PathItem:
7+
- $.paths[*]
8+
overrides:
9+
- files: ['*']
10+
rules:
11+
operation-description:
12+
given: '#OperationObject'
13+
then:
14+
field: 'summary'
15+
function: 'truthy'
16+
severity: warn

packages/ruleset-migrator/src/transformers/rules.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const transformer: Transformer = function (hooks) {
8585
]);
8686

8787
hooks.add([
88-
/^\/rules\/[^/]+\/then\/(?:[0-9]+\/)?function$/,
88+
/^(?:\/overrides\/\d+)?\/rules\/[^/]+\/then\/(?:\d+\/)?function$/,
8989
(value, ctx): namedTypes.Identifier | namedTypes.UnaryExpression => {
9090
assertString(value);
9191

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
====test====
2+
Respect overrides with rules-only
3+
====asset:spectral.yaml====
4+
aliases:
5+
OperationObject:
6+
- "#PathItem[get,put,post,delete,options,head,patch,trace]"
7+
PathItem:
8+
- $.paths[*]
9+
rules:
10+
operation-description:
11+
given: '#OperationObject'
12+
then:
13+
field: 'description'
14+
function: 'truthy'
15+
severity: error
16+
overrides:
17+
- files: ["v2/**/*.json"]
18+
rules:
19+
summary-description:
20+
given: '#OperationObject'
21+
then:
22+
field: 'summary'
23+
function: 'truthy'
24+
severity: warn
25+
====asset:v2/document.json====
26+
{
27+
"openapi": "3.1.0",
28+
"info": {
29+
"description": "",
30+
"title": ""
31+
},
32+
"paths": {
33+
"/": {
34+
"get": {}
35+
}
36+
}
37+
}
38+
====asset:legacy/document.json====
39+
{
40+
"openapi": "3.1.0",
41+
"info": {
42+
"description": "",
43+
"title": ""
44+
},
45+
"paths": {
46+
"/": {
47+
"get": {}
48+
}
49+
}
50+
}
51+
====command====
52+
{bin} lint **/*.json --ruleset {asset:spectral.yaml} --fail-on-unmatched-globs
53+
====stdout====
54+
55+
{asset:legacy/document.json}
56+
9:13 error operation-description "get.description" property must be truthy paths./.get
57+
58+
{asset:v2/document.json}
59+
9:13 error operation-description "get.description" property must be truthy paths./.get
60+
9:13 warning summary-description "get.summary" property must be truthy paths./.get
61+
62+
✖ 3 problems (2 errors, 1 warning, 0 infos, 0 hints)

0 commit comments

Comments
 (0)