Skip to content

Initial Spectral ruleset #18

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

Merged
merged 3 commits into from
Mar 2, 2023
Merged
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
7 changes: 7 additions & 0 deletions spectral/.spectral.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extends:
- ./rules/schema-at-root.spectral.yml
- ./rules/schema-resource-root.spectral.yml
- ./rules/id-at-root.spectral.yml
- ./rules/incompatible-type-validations.spectral.yml
- ./rules/if-then-else.spectral.yml
- ./rules/unknown-format.spectral.yml
26 changes: 26 additions & 0 deletions spectral/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[Spectral](https://stoplight.io/open-source/spectral) is a linting tool for JSON and YAML.

It's most used to help developers make informed decisions when composing OpenAPI and AsyncAPI documents.

This folder supplies a ruleset that can do the same for JSON Schema in general.

Spectral works by defining a set of rules. The rules defining herein work independently of each other and typically focus on a single problem or area.

For more information on how Spectral rules work, you can read their [full documentation](https://docs.stoplight.io/docs/spectral/).

## Using the ruleset

Currently the rules are only published as part of this repository, however we are exploring other publication options.

To use these rules, first clone this repository.

You can run the CLI linter (downloadable separately) from this folder (_./spectral_). Check the CLI documentation for more options and configuration.

### In Visual Studio Code

If you're a VSCode user, Spectral offers an extension. There are a couple gotchas with using it, though.

- It expects the .spectral.yml file to be at the root of the workspace, so you may need to copy these files into the root folder of wherever your JSON/YAML files are and open _that folder_ (not a parent) in VSCode.
- You'll need to configure the extension to ignore **/*.spectral.yml files so that it doesn't try to validate the rule files themselves.

Once that's set up, you'll get problems reported in the Problems pane.
67 changes: 67 additions & 0 deletions spectral/rules/const-independence.spectral.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
rules:
const-should-stand-alone:
description: Schemas with const should not have additional validation
severity: error
given: $..[?(@.const)]
then:
- function: undefined
field: type
- function: undefined
field: minimum
- function: undefined
field: maximum
- function: undefined
field: exclusiveMinimum
- function: undefined
field: exclusiveMaximum
- function: undefined
field: multipleOf
- function: undefined
field: minLength
- function: undefined
field: maxLength
- function: undefined
field: pattern
- function: undefined
field: minItems
- function: undefined
field: maxItems
- function: undefined
field: items
- function: undefined
field: uniqueItems
- function: undefined
field: additionalItems
- function: undefined
field: unevaluatedItems
- function: undefined
field: contains
- function: undefined
field: minContains
- function: undefined
field: maxContains
- function: undefined
field: properties
- function: undefined
field: additionalProperties
- function: undefined
field: unevaluatedProperties
- function: undefined
field: propertyNames
- function: undefined
field: patternProperties
- function: undefined
field: minProperties
- function: undefined
field: maxProperties
- function: undefined
field: required
- function: undefined
field: dependentRequired
- function: undefined
field: dependentSchema
- function: undefined
field: dependencies
- function: undefined
field: enum

66 changes: 66 additions & 0 deletions spectral/rules/enum-independence.spectral.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
rules:
enum-should-stand-alone:
description: Schemas with enum should not have additional validation
severity: error
given: $..[?(@.enum)]
then:
- function: undefined
field: type
- function: undefined
field: minimum
- function: undefined
field: maximum
- function: undefined
field: exclusiveMinimum
- function: undefined
field: exclusiveMaximum
- function: undefined
field: multipleOf
- function: undefined
field: minLength
- function: undefined
field: maxLength
- function: undefined
field: pattern
- function: undefined
field: minItems
- function: undefined
field: maxItems
- function: undefined
field: items
- function: undefined
field: uniqueItems
- function: undefined
field: additionalItems
- function: undefined
field: unevaluatedItems
- function: undefined
field: contains
- function: undefined
field: minContains
- function: undefined
field: maxContains
- function: undefined
field: properties
- function: undefined
field: additionalProperties
- function: undefined
field: unevaluatedProperties
- function: undefined
field: propertyNames
- function: undefined
field: patternProperties
- function: undefined
field: minProperties
- function: undefined
field: maxProperties
- function: undefined
field: required
- function: undefined
field: dependentRequired
- function: undefined
field: dependentSchema
- function: undefined
field: dependencies
- function: undefined
field: const
8 changes: 8 additions & 0 deletions spectral/rules/id-at-root.spectral.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
rules:
require-id-root:
description: Root must include $id.
severity: error
given: $
then:
function: truthy
field: $id
41 changes: 41 additions & 0 deletions spectral/rules/if-then-else.spectral.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
rules:
if-requires-then-or-else:
description: 'The `if` keyword will be ignored unless an adjacent `then` and/or `else` keyword is present.'
severity: error
given:
- $
- $..[?(@.if)]
then:
- function: schema
functionOptions:
schema:
if:
not:
anyOf:
- required:
- then
- required:
- else
then:
properties:
if: false
then-and-else-require-if:
description: 'The `then` and `else` keywords will be ignored unless an adjacent `if` keyword is present.'
severity: error
given:
- $
- $..[?(@.then)]
- $..[?(@.else)]
then:
- function: schema
functionOptions:
schema:
if:
not:
required:
- if
then:
properties:
then: false
else: false

26 changes: 26 additions & 0 deletions spectral/rules/incompatible-type-additionalItems.spectral.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
rules:
additionalItems-only-applies-to-objects:
description: The additionalItems keyword is incompatible with the specified type.
severity: error
given:
- $
- $..[?(@.additionalItems!=null)]
then:
- function: schema
functionOptions:
schema:
if:
properties:
type:
enum:
- object
- string
- number
- integer
- boolean
- 'null'
required:
- type
then:
properties:
additionalItems: false
26 changes: 26 additions & 0 deletions spectral/rules/incompatible-type-additionalProperties.spectral.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
rules:
additionalProperties-only-applies-to-objects:
description: The additionalProperties keyword is incompatible with the specified type.
severity: error
given:
- $
- $..[?(@.additionalProperties!=null)]
then:
- function: schema
functionOptions:
schema:
if:
properties:
type:
enum:
- array
- string
- number
- integer
- boolean
- 'null'
required:
- type
then:
properties:
additionalProperties: false
26 changes: 26 additions & 0 deletions spectral/rules/incompatible-type-contains.spectral.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
rules:
contains-only-applies-to-objects:
description: The contains keyword is incompatible with the specified type.
severity: error
given:
- $
- $..[?(@.contains!=null)]
then:
- function: schema
functionOptions:
schema:
if:
properties:
type:
enum:
- object
- string
- number
- integer
- boolean
- 'null'
required:
- type
then:
properties:
contains: false
26 changes: 26 additions & 0 deletions spectral/rules/incompatible-type-dependencies.spectral.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
rules:
dependencies-only-applies-to-objects:
description: The dependencies keyword is incompatible with the specified type.
severity: error
given:
- $
- $..[?(@.dependencies!=null)]
then:
- function: schema
functionOptions:
schema:
if:
properties:
type:
enum:
- array
- string
- number
- integer
- boolean
- 'null'
required:
- type
then:
properties:
dependencies: false
26 changes: 26 additions & 0 deletions spectral/rules/incompatible-type-dependentRequired.spectral.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
rules:
dependentRequired-only-applies-to-objects:
description: The dependentRequired keyword is incompatible with the specified type.
severity: error
given:
- $
- $..[?(@.dependentRequired!=null)]
then:
- function: schema
functionOptions:
schema:
if:
properties:
type:
enum:
- array
- string
- number
- integer
- boolean
- 'null'
required:
- type
then:
properties:
dependentRequired: false
26 changes: 26 additions & 0 deletions spectral/rules/incompatible-type-dependentSchema.spectral.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
rules:
dependentSchema-only-applies-to-objects:
description: The dependentSchema keyword is incompatible with the specified type.
severity: error
given:
- $
- $..[?(@.dependentSchema!=null)]
then:
- function: schema
functionOptions:
schema:
if:
properties:
type:
enum:
- array
- string
- number
- integer
- boolean
- 'null'
required:
- type
then:
properties:
dependentSchema: false
Loading