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

docs: config file #270

Merged
merged 2 commits into from
Jun 26, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Other options include:

> Note: The Spectral CLI supports both YAML and JSON.

Currently, the CLI supports validation of OpenAPI documents and lints them based on our default ruleset. It does not support custom rulesets at this time. Although if you want to build and run custom rulesets outside of the CLI, see [Customization](#Customization).
Currently, Spectral CLI CLI supports validation of OpenAPI documents and lints them based on our default ruleset, or you can provide [your own rulesets](docs/rulesets.md).

## Concepts

Expand All @@ -105,13 +105,13 @@ There are three key concepts in Spectral: **Rulesets**, **Rules** and **Function

Think of a set of **rules** and **functions** as a flexible and customizable style guide for your JSON objects.

## Programmatic usage
## Config

Spectral is written in TypeScript (JavaScript) and can be used directly for when you need to use Spectral programatically. Take a look at our ["JavaScript API documentation"](docs/js-api.md).
Spectral CLI supports [config files](docs/config.md), to avoid typing out CLI options and arguments every single time.

## Rulesets
## Programmatic usage

You can find all about [rulesets here](docs/rulesets.md).
Spectral is written in TypeScript (JavaScript) and can be used directly for when you need to use Spectral programmatically. Take a look at our ["JavaScript API documentation"](docs/js-api.md).

## FAQs

Expand Down
62 changes: 62 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
Copy link
Contributor

Choose a reason for hiding this comment

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

❤️

title: Spectral Config
tags:
- Documentation
- Config
---

Spectral CLI supports config files, to avoid typing out CLI options and arguments every single time.

By default Spectral CLI will look for `spectral.yml` in the current working directory, but it could be any JSON or YAML file if you specify it with the `--config` option.

## Usage

```bash
spectral lint openapi.yaml --config=spectral.yaml
```

## Example Config

```yaml
lint:
format: json
ruleset:
- http://apis.example.com/governance/core.yml
- http://apis.example.com/governance/rest.yml
skipRule:
- that-one-rule
- broken-rule
output: tmp/lint-results.json
verbose: true
```

Right now Spectral only has the one command: `lint`, but other commands in the
future will have their own config namespace too.

Here are all the available options, with their default values:

```yaml
lint:
encoding: utf8 # text encoding to use
format: stylish # stylish, json
output: # filename for output instead of stdout
maxResults: # only show the first X erros
verbose: false # see more output for debugging
ruleset: # array of local files or URLs
quiet: false # see only results
skipRule: [] # array of rule names as strings
```

The TypeScript interface here looks like this:

```typescript
export interface ILintConfig {
encoding: string;
format: ConfigFormat;
maxResults?: number;
output?: string;
ruleset?: string[];
skipRule?: string[];
verbose: boolean;
}
```
30 changes: 15 additions & 15 deletions docs/rulesets.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
# Spectral Rulesets
---
title: Spectral Rulesets
tags:
- Documentation
- Rulesets
---

Rulesets are a container for collections of rules. These rules are essentially calling functions Spectral, and by taking parameters you can make these functions do whatever you want.

First the rule filters your object (JSON/YAML file) down to a set of target values, and then list what function arguments should be passed in.

## Usage

```bash
spectral lint foo.yaml --ruleset=path/to/acme-company-ruleset.yaml --ruleset=http://example.com/acme-common-ruleset.yaml
```

## Example ruleset file
## Example Ruleset

We currently support ruleset files in both `yaml` and `json` formats.

Expand All @@ -28,8 +37,6 @@ Rules are highly configurable. There are only few required parameters but the op

*TODO: generate this table automatically from the TS file.*

### Rule

<table>
<thead>
<tr>
Expand Down Expand Up @@ -140,14 +147,14 @@ Rules are highly configurable. There are only few required parameters but the op
</tbody>
</table>

### Ruleset validation
## Ruleset Validation

We use JSON Schema & AJV to validate your rulesets file and help you spot issues early.

**Example output**

```bash
spectral lint some-oas.yaml --ruleset acme-company.json
$ spectral lint some-oas.yaml --ruleset acme-company.json
tbarn marked this conversation as resolved.
Show resolved Hide resolved

Reading ruleset

Expand All @@ -158,12 +165,5 @@ Reading ruleset
/rules/rule-with-invalid-enum/type should be equal to one of the allowed values
```

**Note for developers**
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed this because I didn't understand what it was talking about. I think we only offer YAML rulesets right now anyhow, and we removed our TS to YAML scrupt, but what is a TS to JSON Schema script? @chris-miaskowski do you know about this?


Supporting YAML and JSON file validation doesn't come free.
We need to maintain schema files that mirror IRule and IRuleset types (see `/src/meta/*.schema.json`).
Ideally, we would have a script that converts TS type to JSON Schema and keeps the meta files up to date. As of now we have a helper that partially automates the work.

Execute `yarn schema.update` to recreate the `/src/meta/rule.schema.json`.
It will take `IRule` type from `types.ts` file and automatically update the JSON Schema file we use to validate yaml/json ruleset files.
**Warning**: make sure to update *generic* types. Current tools fails to recognize it properly and e.g. treats `string` as `object`.
These errors should look just like errors you get from Spectral when an API description is invalid,
so use them to fix your rules in the same way.