Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove addRules and addFunctions #711

Merged
merged 4 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/getting-started/rulesets.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ rules:
function: truthy
```

Spectral has a built-in set of functions such as `truthy` or `pattern`, which you can reference in your rules.
The example above adds a single rule that checks that tags objects have a description property defined.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"The example above" sounds a little funny now as we already say "This example". Can you merge the two sentences to make it less redundant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you are right. It does sounds awkward. Will merge


Running `spectral lint` on the following object with the ruleset above will result in an error being reported, since the tag does not have a description:
Expand Down
137 changes: 0 additions & 137 deletions docs/guides/javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,143 +180,6 @@ The OpenAPI rules are opinionated. There might be some rules that you prefer to

## Advanced

### Creating a custom rule

Spectral has a built-in set of functions which you can reference in your rules. This example uses the `RuleFunction.PATTERN` to create a rule that checks that all property values are in snake case.

```javascript
const { Spectral } = require('@stoplight/spectral');

const spectral = new Spectral();

spectral.addRules({
snake_case: {
description: 'Checks for snake case pattern',

// evaluate every property
given: '$..*',

then: {
function: 'pattern',
functionOptions: {
match: '^[a-z]+[a-z0-9_]*[a-z0-9]+$',
},
},
},
});

// run!
spectral.run({name: 'helloWorld',}).then(results => {
console.log(results);
});
```

[Try it out!](https://repl.it/@ChrisMiaskowski/spectral-pattern-example)

```bash
[
{
"code": "snake_case",
"message": "Checks for snake case pattern",
"path": [
"name"
],
"severity": 1,
"range": {
"start": {
"line": 1,
"character": 10
},
"end": {
"line": 1,
"character": 22
}
}
}
]
```

### Creating a custom function

Sometimes the built-in functions don't cover your use case. This example creates a custom function, `customNotThatFunction`, and then uses it within a rule, `openapi_not_swagger`. The custom function checks that you are not using a specific string (e.g., "Swagger") and suggests what to use instead (e.g., "OpenAPI").

```javascript
const { Spectral } = require('@stoplight/spectral');

// custom function
const customNotThatFunction = (targetValue, options) => {
const { match, suggestion } = options;

if (targetValue && targetValue.match(new RegExp(match))) {
// return the single error
return [
{
message: `Use ${suggestion} instead of ${match}!`,
},
];
}
};

const spectral = new Spectral();

spectral.addFunctions({
notThat: customNotThatFunction,
});

spectral.addRules({
openapi_not_swagger: {
description: 'Checks for use of Swagger, and suggests OpenAPI.',

// check every property
given: '$..*',

then: {
// reference the function we added!
function: 'notThat',

// pass it the options it needs
functionOptions: {
match: 'Swagger',
suggestion: 'OpenAPI',
},
},
},
});

// run!
spectral.run({description: 'Swagger is pretty cool!',}).then(results => {
console.log(JSON.stringify(results, null, 4));
});
```

[Try it out!](https://repl.it/@ChrisMiaskowski/spectral-custom-function-example)

```bash
# Outputs a single result since we are using the term `Swagger` in our object
[
{
"code": "openapi_not_swagger",
"message": "Checks for use of Swagger, and suggests OpenAPI.",
"path": [
"description"
],
"severity": 1,
"range": {
"start": {
"line": 1,
"character": 17
},
"end": {
"line": 1,
"character": 42
}
}
}
]
```

For more information on creating rules, read about [rulesets](../getting-started/rulesets.md).

### Creating a custom format

Spectral supports two core formats: `oas2` and `oas3`. Using `registerFormat` you can add support for autodetecting other formats. You might want to do this for a ruleset which is run against multiple major versions of description format like RAML v0.8 and v1.0.
Expand Down
51 changes: 51 additions & 0 deletions docs/migration-guides/5.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Spectral v4 to v5 Migration Guide

Our docs have been updated, so you can always refer to them. To make the transition less painful,
this migration guide covers the most notable changes.

### I use Spectral programmatically via API...

1. addFunctions and addRules have been removed

We strongly encourage everyone to write rulesets, therefore the new preferred way to load a ruleset is via `loadRuleset`.

**Spectral v4**:

```js
const { oas3Functions, rules: oas3Rules } = require('@stoplight/spectral/dist/rulesets/oas3');

const spectral = new Spectral();
spectral.addFunctions(oas3Functions);
spectral.addFunctions(oas3Rules);
spectral.run(myOpenApiDocument)
.then(results => {
console.log('here are the results', results);
});
```

**Spectral v5**:

```js
const { Spectral } = require('@stoplight/spectral');

const spectral = new Spectral();
spectral.loadRuleset('spectral:oas')
.then(() => spectral.run(myOpenApiDocument))
.then(results => {
console.log('here are the results', results);
});
```

Alternatively, if your ruleset is stored in a plain JSON file, you can also consider using `setRuleset`, as follows

```js
const { Spectral } = require('@stoplight/spectral');
const ruleset = require('./my-ruleset.json');

const spectral = new Spectral();
spectral.setRuleset(ruleset);
spectral.run(myOpenApiDocument)
.then(results => {
console.log('here are the results', results);
});
```
21 changes: 0 additions & 21 deletions src/rulesets/oas/index.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/rulesets/oas2/index.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/rulesets/oas3/index.ts

This file was deleted.

21 changes: 1 addition & 20 deletions src/spectral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
parseWithPointers as parseYAMLWithPointers,
YamlParserResult,
} from '@stoplight/yaml';
import deprecated from 'deprecated-decorator';
import { merge, set } from 'lodash';

import { STATIC_ASSETS } from './assets';
Expand Down Expand Up @@ -132,33 +131,15 @@ export class Spectral {
return (await this.runWithResolved(target, opts)).results;
}

@deprecated('loadRuleset', '4.1')
public addFunctions(functions: FunctionCollection) {
this._addFunctions(functions);
}

public _addFunctions(functions: FunctionCollection) {
Object.assign(this.functions, functions);
}

public setFunctions(functions: FunctionCollection) {
empty(this.functions);

this._addFunctions({ ...defaultFunctions, ...functions });
}

@deprecated('loadRuleset', '4.1')
public addRules(rules: RuleCollection) {
this._addRules(rules);
Object.assign(this.functions, { ...defaultFunctions, ...functions });
}

public setRules(rules: RuleCollection) {
empty(this.rules);

this._addRules({ ...rules });
}

private _addRules(rules: RuleCollection) {
for (const name in rules) {
if (!rules.hasOwnProperty(name)) continue;
const rule = rules[name];
Expand Down