Skip to content

Commit

Permalink
fix(ruleset-migrator): transform functions under overrides (#2459)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored Apr 20, 2023
1 parent deb85e2 commit 45e817f
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { truthy } = require('@stoplight/spectral-functions');
const { oas } = require('@stoplight/spectral-rulesets');
module.exports = {
extends: [oas],
aliases: {
OperationObject: ['#PathItem[get,put,post,delete,options,head,patch,trace]'],
PathItem: ['$.paths[*]'],
},
overrides: [
{
files: ['*'],
rules: {
'operation-description': {
given: '#OperationObject',
then: {
field: 'summary',
function: truthy,
},
severity: 'warn',
},
},
},
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { truthy } from '@stoplight/spectral-functions';
import { oas } from '@stoplight/spectral-rulesets';
export default {
extends: [oas],
aliases: {
OperationObject: ['#PathItem[get,put,post,delete,options,head,patch,trace]'],
PathItem: ['$.paths[*]'],
},
overrides: [
{
files: ['*'],
rules: {
'operation-description': {
given: '#OperationObject',
then: {
field: 'summary',
function: truthy,
},
severity: 'warn',
},
},
},
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
extends:
- 'spectral:oas'
aliases:
OperationObject:
- '#PathItem[get,put,post,delete,options,head,patch,trace]'
PathItem:
- $.paths[*]
overrides:
- files: ['*']
rules:
operation-description:
given: '#OperationObject'
then:
field: 'summary'
function: 'truthy'
severity: warn
2 changes: 1 addition & 1 deletion packages/ruleset-migrator/src/transformers/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const transformer: Transformer = function (hooks) {
]);

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

Expand Down
62 changes: 62 additions & 0 deletions test-harness/scenarios/overrides/new-rule-legacy-ruleset.scenario
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
====test====
Respect overrides with rules-only
====asset:spectral.yaml====
aliases:
OperationObject:
- "#PathItem[get,put,post,delete,options,head,patch,trace]"
PathItem:
- $.paths[*]
rules:
operation-description:
given: '#OperationObject'
then:
field: 'description'
function: 'truthy'
severity: error
overrides:
- files: ["v2/**/*.json"]
rules:
summary-description:
given: '#OperationObject'
then:
field: 'summary'
function: 'truthy'
severity: warn
====asset:v2/document.json====
{
"openapi": "3.1.0",
"info": {
"description": "",
"title": ""
},
"paths": {
"/": {
"get": {}
}
}
}
====asset:legacy/document.json====
{
"openapi": "3.1.0",
"info": {
"description": "",
"title": ""
},
"paths": {
"/": {
"get": {}
}
}
}
====command====
{bin} lint **/*.json --ruleset {asset:spectral.yaml} --fail-on-unmatched-globs
====stdout====

{asset:legacy/document.json}
9:13 error operation-description "get.description" property must be truthy paths./.get

{asset:v2/document.json}
9:13 error operation-description "get.description" property must be truthy paths./.get
9:13 warning summary-description "get.summary" property must be truthy paths./.get

✖ 3 problems (2 errors, 1 warning, 0 infos, 0 hints)

0 comments on commit 45e817f

Please sign in to comment.