Description
Per @Relequestual's recommendation, I'm creating a ticket about a linter and to collect linting rules that people would want.
When authoring a JSON schema, it is desirable to have a linter to check the strictness, specificity, satisfiability, and descriptiveness of a schema. Maybe Ben envisions the JSON schema workgroup to produce a reference linter implementation?
Anyway, I'll start with a list of linting rules that would be desirable (assuming draft 07):
- Objects
- Is
additionalProperties
explicitly set? Is it set to eitherfalse
or a schema (not justtrue
)?- If
additionalProperties
is defined, ispropertyNames
also defined?
- If
- Is
required
defined? Are all fields listed inrequired
? - Is there any contradiction among
properties
,additionalProperties
, andrequired
? E.g. this schema is not satisfiable:{ "type": "object", "properties": {}, "required": "foo", "additionalProperties": false }
foo
is required, but is not defined amongproperties
, yet we also disallow additional properties. - If either
additionalProperties
is notfalse
or some fields are missing fromrequired
:- Are
minProperties
andmaxProperties
also defined?- Is
maxProperties
>= the length ofrequired
?
- Is
- Are
- Are (
$ref
or$ref
-in-allOf
) and"additionalProperties": false
present together? Usually that's a sign of the author trying to do poor man's inheritance in JSON schema. The"additonalProperties": false
will likely cause an object to not validate against the schema, counter to the user's expectations.
- Is
- Numbers and integers
- Does every number or integer have:
multipleOf
minimum
/exclusiveMinimum
andmaximum
/exclusiveMaximum
minimum
andexclusiveMinimum
should not be defined togethermaximum
andexclusiveMaximum
should not be defined together- Are the (exclusive) minimum and (exclusive) maximum satisfiable?
- if
minimum
andmaximum
are defined,minimum
should be <=maximum
; - if
minimum
andexclusiveMaximum
are defined,minimum
should be <exclusiveMaximum
; - if
exclusiveMinimum
andmaximum
are defined,exclusiveMinimum
should be <maximum
; - if
exclusiveMinimum
andexclusiveMaximum
are defined:- if
type
is"integer"
:exclusiveMaximum - exclusiveMinimum
should be > 1 - if
type
is"number"
:exclusiveMinimum
should be <exclusiveMaximum
- if
- if
- Does every number or integer have:
- Strings
- Do all strings have a
format
? - Do all strings have
minLength
andmaxLength
?- Is
minLength
<=maxLength
?
- Is
- Do all strings have a
- Arrays
- Do all arrays have
items
schemas? - Are all arrays either:
- tuple-validated - i.e.
items
as a fixed-length array of schemas; or - list-validated - i.e.
items
as a single schema
- tuple-validated - i.e.
- in the case of list validation, are
minItems
andmaxItems
defined?- Is
minItems
<=maxItems
?
- Is
- Is
uniqueItems
explicitly set? Eithertrue
orfalse
is ok. We just want it to be explicit.
- Do all arrays have
- General descriptiveness
- Does everything have a
title
? - Does everything have a
description
? - Does everything have a
$comment
? - Does everything have a
default
?- Does the
default
satisfy the schema?
- Does the
- Does everything have
examples
?- Do all
examples
satisfy the schema?
- Do all
- Does everything have a
Ideally, the linter shall be able to generate output that is machine-parsable, with the appropriate JSON pointer so that the author can choose to silence/ignore certain warnings.
I know there are more ways for a schema to become unsatisfiable. E.g. a string could have a regex pattern that can only be 3 characters long, but the author also specifies "minLength": 10
. This kind of satisfiability violation is too complex to check. I don't expect any linter to be able to check for that.