Skip to content

Linter #1079

Open
Open
Linter#1079
@ksze

Description

@ksze

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 either false or a schema (not just true)?
      • If additionalProperties is defined, is propertyNames also defined?
    • Is required defined? Are all fields listed in required?
    • Is there any contradiction among properties, additionalProperties, and required? E.g. this schema is not satisfiable:
      {
        "type": "object",
        "properties": {},
        "required": "foo",
        "additionalProperties": false
      }
      
      foo is required, but is not defined among properties, yet we also disallow additional properties.
    • If either additionalProperties is not false or some fields are missing from required:
      • Are minProperties and maxProperties also defined?
        • Is maxProperties >= the length of required?
    • 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.
  • Numbers and integers
    • Does every number or integer have:
      • multipleOf
      • minimum/exclusiveMinimum and maximum/exclusiveMaximum
        • minimum and exclusiveMinimum should not be defined together
        • maximum and exclusiveMaximum should not be defined together
        • Are the (exclusive) minimum and (exclusive) maximum satisfiable?
          • if minimum and maximum are defined, minimum should be <= maximum;
          • if minimum and exclusiveMaximum are defined, minimum should be < exclusiveMaximum;
          • if exclusiveMinimum and maximum are defined, exclusiveMinimum should be < maximum;
          • if exclusiveMinimum and exclusiveMaximum are defined:
            • if type is "integer": exclusiveMaximum - exclusiveMinimum should be > 1
            • if type is "number": exclusiveMinimum should be < exclusiveMaximum
  • Strings
    • Do all strings have a format?
    • Do all strings have minLength and maxLength?
      • Is minLength <= maxLength?
  • 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
    • in the case of list validation, are minItems and maxItems defined?
      • Is minItems <= maxItems?
    • Is uniqueItems explicitly set? Either true or false is ok. We just want it to be explicit.
  • 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 everything have examples?
      • Do all examples satisfy the schema?

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions