Skip to content

Commit

Permalink
Add custom linting rule example to the README
Browse files Browse the repository at this point in the history
This shows use of the generic JSON traversal method.
  • Loading branch information
ssilverman committed Jun 22, 2020
1 parent 1394f6d commit 6039a66
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ See: [Keep a Changelog](https://keepachangelog.com)
* A linter check for `$id` values that have an empty fragment, for Draft 2019-09
and later.
* Added "minimum" keyword > "maximum" keyword checks to the linter.
* Added a custom linting rule example to the README.

### Changed
* The EMAIL regex in Format now disallows local parts starting with a dot,
Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ See: [JSON Schema](https://json-schema.org)
8. [Building and running](#building-and-running)
1. [Program execution with Maven](#program-execution-with-maven)
9. [The linter](#the-linter)
1. [Doing your own linting](#doing-your-own-linting)
10. [Future plans](#future-plans)
11. [References](#references)
12. [An ending thought](#an-ending-thought)
Expand Down Expand Up @@ -384,6 +385,32 @@ schema. It does currently check for the following things:
15. Draft-07 or later, or unspecified, schemas:
1. `then` without `if`.
2. `else` without `if`.

### Doing your own linting

The `JSON` class has a `traverse` method that does a preorder tree traversal.
It's what the linter uses internally. It's possible to use this to write your
own linting rules.

The following example snippet tests for the existence of any "anyOf"
schema keywords:

```java
JsonElement schema;
// ...load the schema...
JSON.traverse(schema, (e, parent, path, state) -> {
if (path.isEmpty()) {
return;
}
// Ignore if the parent is "properties" because then it's not a keyword
if (path.size() >= 2 && path.get(path.size() - 2).equals("properties")) {
return;
}
if (path.get(path.size() - 1).equals("anyOf")) {
System.out.println(path + ": anyOf keyword present");
}
});
```
## Future plans
Expand Down

0 comments on commit 6039a66

Please sign in to comment.