-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JSON Schema misconfigurations rules (#33)
* Add JSON Schema misconfigurations rules * adjust rules so problems point to the key instead of the value * fix rules and readme * add newline Co-authored-by: Andrew Tatomyr <[email protected]> --------- Co-authored-by: Andrew Tatomyr <[email protected]>
- Loading branch information
1 parent
2581a52
commit 6a7f075
Showing
4 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
configurable-rules/json-schema-misconfigurations/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# JSON Schema misconfigurations | ||
|
||
Authors: | ||
- [`adamaltman`](https://github.com/adamaltman) Adam Altman (Redocly) | ||
|
||
## What this does and why | ||
|
||
This catches common misconfigurations of JSON Schema: | ||
- disallows `minimum` or `maximum` values for a string (these are more logical for a number) | ||
- disallows `items` on an object (instead of an array) | ||
- disallows `properties` on an array (instead of items) | ||
|
||
## Code | ||
|
||
The first rule checks that a string isn't using the `minimum` and `maximum` keywords. | ||
```yaml | ||
rule/json-schema-string-misconfiguration: | ||
subject: | ||
type: Schema | ||
where: | ||
- subject: | ||
type: Schema | ||
property: type | ||
assertions: | ||
const: string | ||
assertions: | ||
disallowed: | ||
- minimum | ||
- maximum | ||
``` | ||
The second rule checks that an array isn't using the `properties` keyword. | ||
```yaml | ||
rule/json-schema-array-misconfiguration: | ||
subject: | ||
type: Schema | ||
where: | ||
- subject: | ||
type: Schema | ||
property: type | ||
assertions: | ||
const: array | ||
assertions: | ||
disallowed: | ||
- properties | ||
``` | ||
|
||
The third rule checks that an object isn't using the `items` keyword. | ||
```yaml | ||
rule/json-schema-object-misconfiguration: | ||
subject: | ||
type: Schema | ||
where: | ||
- subject: | ||
type: Schema | ||
property: type | ||
assertions: | ||
const: object | ||
assertions: | ||
disallowed: | ||
- items | ||
``` | ||
|
||
## Examples | ||
|
||
The following OpenAPI has schemas prefixed with either `Good` or `Bad` to show the configurable rules catch the likely bad uses of keywords. | ||
|
||
```yaml | ||
openapi: 3.1.0 | ||
info: | ||
title: Unintended schema misconfigurations | ||
version: 1.0.0 | ||
paths: {} | ||
components: | ||
schemas: | ||
BadString: | ||
type: string | ||
minimum: 5 | ||
maximum: 10 | ||
GoodNumber: | ||
type: number | ||
minimum: 5 | ||
maximum: 10 | ||
GoodString: | ||
type: string | ||
minLength: 5 | ||
maxLength: 10 | ||
BadObject: | ||
type: object | ||
items: | ||
type: string | ||
GoodObject: | ||
type: object | ||
properties: | ||
foo: | ||
$ref: "#/components/schemas/GoodString" | ||
BadArray: | ||
type: array | ||
properties: | ||
foo: | ||
$ref: "#/components/schemas/GoodString" | ||
``` | ||
|
||
|
||
## References | ||
|
||
Inspired by a question in the "APIs You Won't Hate" Slack community (special thanks to Can Vural and Phil Sturgeon). |
39 changes: 39 additions & 0 deletions
39
configurable-rules/json-schema-misconfigurations/openapi.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
openapi: 3.1.0 | ||
info: | ||
title: Unintended schema misconfigurations | ||
version: 1.0.0 | ||
paths: {} | ||
components: | ||
schemas: | ||
|
||
BadString: | ||
type: string | ||
minimum: 5 | ||
maximum: 10 | ||
|
||
GoodNumber: | ||
type: number | ||
minimum: 5 | ||
maximum: 10 | ||
|
||
GoodString: | ||
type: string | ||
minLength: 5 | ||
maxLength: 10 | ||
|
||
BadObject: | ||
type: object | ||
items: | ||
type: string | ||
|
||
GoodObject: | ||
type: object | ||
properties: | ||
foo: | ||
$ref: "#/components/schemas/GoodString" | ||
|
||
BadArray: | ||
type: array | ||
properties: | ||
foo: | ||
$ref: "#/components/schemas/GoodString" |
38 changes: 38 additions & 0 deletions
38
configurable-rules/json-schema-misconfigurations/redocly.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
rules: | ||
rule/json-schema-string-misconfiguration: | ||
subject: | ||
type: Schema | ||
where: | ||
- subject: | ||
type: Schema | ||
property: type | ||
assertions: | ||
const: string | ||
assertions: | ||
disallowed: | ||
- minimum | ||
- maximum | ||
rule/json-schema-array-misconfiguration: | ||
subject: | ||
type: Schema | ||
where: | ||
- subject: | ||
type: Schema | ||
property: type | ||
assertions: | ||
const: array | ||
assertions: | ||
disallowed: | ||
- properties | ||
rule/json-schema-object-misconfiguration: | ||
subject: | ||
type: Schema | ||
where: | ||
- subject: | ||
type: Schema | ||
property: type | ||
assertions: | ||
const: object | ||
assertions: | ||
disallowed: | ||
- items |