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

Add JSON Schema misconfigurations rules #33

Merged
merged 4 commits into from
Apr 26, 2024
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ There are some fantastic examples of [configurable rules](https://redocly.com/do
- [Paths should not match a pattern](configurable-rules/path-excludes-pattern/)
- [API healthcheck rules](configurable-rules/api-health/)
- [String schemas length defined](configurable-rules/string-schemas-length-defined/)
- [JSON Schema misconfigurations](configurable-rules/json-schema-misconfigurations/)

### Custom plugins

Expand Down
113 changes: 113 additions & 0 deletions configurable-rules/json-schema-misconfigurations/README.md
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 configurable-rules/json-schema-misconfigurations/openapi.yaml
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 configurable-rules/json-schema-misconfigurations/redocly.yaml
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