Skip to content

Commit

Permalink
feat(core): support overrides in rulesets (#1684)
Browse files Browse the repository at this point in the history
Co-authored-by: William Hilton <wmhilton@gmail.com>
  • Loading branch information
P0lip and billiegoose authored Jun 28, 2021
1 parent f95cac4 commit 153d685
Show file tree
Hide file tree
Showing 30 changed files with 1,293 additions and 103 deletions.
8 changes: 5 additions & 3 deletions packages/cli/src/services/linter/utils/getRuleset.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Optional } from '@stoplight/types';
import { Ruleset } from '@stoplight/spectral-core';
import { Ruleset, RulesetDefinition } from '@stoplight/spectral-core';
import * as fs from 'fs';
import * as path from '@stoplight/path';
import { isAbsolute } from '@stoplight/path';
import * as process from 'process';
import { RulesetDefinition } from '@stoplight/spectral-core';

async function getDefaultRulesetFile(): Promise<Optional<string>> {
const cwd = process.cwd();
Expand All @@ -30,5 +29,8 @@ export async function getRuleset(rulesetFile: Optional<string>): Promise<Ruleset

const ruleset = (await import(rulesetFile)) as { default: RulesetDefinition } | RulesetDefinition;

return new Ruleset('default' in ruleset ? ruleset.default : ruleset, { severity: 'recommended' });
return new Ruleset('default' in ruleset ? ruleset.default : ruleset, {
severity: 'recommended',
source: rulesetFile,
});
}
2 changes: 2 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"json-schema": "^0.3.0",
"jsonpath-plus": "^5.0.7",
"lodash": "~4.17.21",
"minimatch": "^3.0.4",
"nanoid": "2.1.11",
"nimma": "0.0.0",
"treeify": "^1.1.0",
Expand All @@ -49,6 +50,7 @@
"@stoplight/spectral-parsers": "^0.0.0",
"@stoplight/yaml": "^4.2.2",
"@types/json-schema": "^7.0.7",
"@types/minimatch": "^3.0.4",
"@types/treeify": "^1.0.0",
"esm": "^3.2.25",
"nock": "^13.1.0",
Expand Down
57 changes: 57 additions & 0 deletions packages/core/src/meta/ruleset.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,60 @@
}
},
"additionalProperties": false
},
"overrides": {
"type": "array",
"minItems": 1,
"items": {
"allOf": [
{
"type": "object",
"properties": {
"files": {
"type": "array",
"minItems": 1,
"items": {
"type": "string"
},
"errorMessage": "must be an non-empty array of glob patterns"
}
},
"required": ["files"],
"errorMessage": {
"type": "must be a override, i.e. { \"files\": [\"v2/**/*.json\"], \"rules\": {} }"
}
},
{
"type": "object",
"properties": {
"formats": {
"$ref": "#/properties/formats"
},
"extends": {
"$ref": "#/properties/extends"
},
"rules": {
"$ref": "#/properties/rules"
},
"parserOptions": {
"$ref": "#/properties/parserOptions"
}
},
"anyOf": [
{
"required": ["extends"]
},
{
"required": ["rules"]
}
]
}
]
},
"errorMessage": {
"minItems": "must not be empty",
"type": "must be an array"
}
}
},
"anyOf": [
Expand All @@ -71,6 +125,9 @@
},
{
"required": ["rules"]
},
{
"required": ["overrides"]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { pattern } from '@stoplight/spectral-functions';
import { DiagnosticSeverity } from '@stoplight/types';

export default {
rules: {
'description-matches-stoplight': {
message: 'Description must contain Stoplight',
given: '$.info',
recommended: true,
severity: DiagnosticSeverity.Error,
then: {
field: 'description',
function: pattern,
functionOptions: {
match: 'Stoplight',
},
},
},
'title-matches-stoplight': {
message: 'Title must contain Stoplight',
given: '$.info',
then: {
field: 'title',
function: pattern,
functionOptions: {
match: 'Stoplight',
},
},
},
'contact-name-matches-stoplight': {
message: 'Contact name must contain Stoplight',
given: '$.info.contact',
recommended: false,
then: {
field: 'name',
function: pattern,
functionOptions: {
match: 'Stoplight',
},
},
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { RulesetDefinition } from '@stoplight/spectral-core';

import _base from '../_base';

export { ruleset as default };

const ruleset: RulesetDefinition = {
overrides: [
{
files: ['**/*.json'],
extends: [[_base, 'all']],
},
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { RulesetDefinition } from '@stoplight/spectral-core';

import _base from '../_base';

export { ruleset as default };

const ruleset: RulesetDefinition = {
extends: [[_base, 'off']],
overrides: [
{
files: ['**/*.json'],
extends: [[_base, 'all']],
},
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { RulesetDefinition } from '@stoplight/spectral-core';

import _base from '../_base';

export { ruleset as default };

const ruleset: RulesetDefinition = {
overrides: [
{
files: ['**/*.json'],
extends: [[_base, 'all']],
},
{
files: ['v2/**/*.json'],
extends: [[_base, 'off']],
},
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { jsonSchemaDraft4, jsonSchemaDraft7 } from '@stoplight/spectral-formats';
import { schema } from '@stoplight/spectral-functions';
import { RulesetDefinition } from '@stoplight/spectral-core';

import _base from './_base';

export { ruleset as default };

const ruleset: RulesetDefinition = {
rules: {
..._base.rules,
},
overrides: [
{
files: ['schemas/**/*.draft7.json'],
formats: [jsonSchemaDraft7],
rules: {
// if you stumbled upon this file and want to use this rule in your own ruleset - do NOT.
// it doesn't cover many cases, and it's only purpose is to serve us as a test artefact
'valid-number-validation': {
given: ['$..exclusiveMinimum', '$..exclusiveMaximum'],
then: {
function: schema,
functionOptions: {
type: 'number',
},
},
},
},
},
{
files: ['schemas/**/*.draft4.json'],
formats: [jsonSchemaDraft4],
rules: {
// if you stumbled upon this file and want to use this rule in your own ruleset - do NOT.
// it doesn't cover many cases, and it's only purpose is to serve us as a test artefact
'valid-number-validation': {
given: ['$..exclusiveMinimum', '$..exclusiveMaximum'],
then: {
function: schema,
functionOptions: {
type: 'boolean',
},
},
},
},
},
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import _base from './_base';
import { RulesetDefinition } from '../../../types';

export { ruleset as default };

const ruleset: RulesetDefinition = {
rules: {
..._base.rules,
},
overrides: [
{
files: ['legacy/**/*.json'],
rules: {
'description-matches-stoplight': 'off',
'title-matches-stoplight': 'warn',
'contact-name-matches-stoplight': true,
},
},
{
files: ['v2/**/*.json'],
rules: {
'description-matches-stoplight': 'error',
'title-matches-stoplight': 'hint',
},
},
{
files: ['**/*.json'],
rules: {
'description-matches-stoplight': 'info',
'title-matches-stoplight': 'off',
},
},
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import _base from './_base';
import { RulesetDefinition } from '../../../types';

export { ruleset as default };

const ruleset: RulesetDefinition = {
extends: _base,
overrides: [
{
files: ['legacy/**/*.json'],
rules: {
'new-definition': {
given: '$',
then: {
function() {},
},
},
},
},
{
files: ['v2/**/*.json'],
rules: {
'new-definition': 'off', // errors
},
},
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { pattern } from '@stoplight/spectral-functions';
import { RulesetDefinition } from '@stoplight/spectral-core';

import _base from './_base';

export { ruleset as default };

const ruleset: RulesetDefinition = {
extends: _base,
overrides: [
{
files: ['legacy/**/*.json'],
rules: {
'value-matches-stoplight': {
message: 'Value must contain Stoplight',
given: '$..value',
severity: DiagnosticSeverity.Error,
then: {
field: 'description',
function: pattern,
functionOptions: {
match: 'Stoplight',
},
},
},
},
},
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import _base from './_base';

export default {
overrides: [
{
files: ['**/*.json'],
extends: [_base],
},
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import _base from './_base';
import { RulesetDefinition } from '../../../types';

export { ruleset as default };

const ruleset: RulesetDefinition = {
rules: {
..._base.rules,
},
overrides: [
{
files: ['legacy/**/*.json'],
rules: {
'description-matches-stoplight': 'off',
'title-matches-stoplight': 'warn',
},
},
{
files: ['v2/**/*.json'],
rules: {
'description-matches-stoplight': 'error',
'title-matches-stoplight': 'hint',
},
},
],
};
Loading

0 comments on commit 153d685

Please sign in to comment.