schema-conditional-effective

Since 0.9.0 · In the rulesets: Recommended at warning, Strict at error, All at error

This rule checks that if, then and else in schemas have an effect.

Rule details

JSON Schema applies then to values matching if and else to the other values. Several combinations silently do nothing, usually because of a typo or a keyword placed at the wrong level:

  • then or else without if is ignored.
  • if without then and else has no effect.
  • else never applies when if accepts every value, e.g. if: {} or an if with only a description; then never applies when if is false.
  • then or else accepting every value (e.g. {}) has no effect.

References are followed, so if: {$ref: ...} is checked against the referenced schema. The rule applies to OpenAPI 3.1 and later, where if, then and else are schema keywords; OpenAPI 3.0 documents are skipped.

Examples

Good. Card payments require a card number, the others an IBAN.

components:
  schemas:
    Payment:
      type: object
      if:
        properties:
          kind:
            const: card
      then:
        required: [cardNumber]
      else:
        required: [iban]

Bad. then is ignored because the schema has no if.

components:
  schemas:
    Payment:
      type: object
      then:
        required: [cardNumber]

Turn it off or change its severity

In apigenie.yaml, off turns the rule off and warning or error sets its severity; a rule with options takes the severity and the options as a pair, [warning, options]. lint.rules has the details.

commands:
  lint:
    rules:
      schema-conditional-effective: off

Related rules

Was this helpful?