Linter rulesOpenAPIschema-conditional-effective

schema-conditional-effective

Since 0.9.0

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.

Configuration

None.

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]

When Not to Use It

We recommend to always enable this rule.

Compatibility

This rule applies to OpenAPI 3.1 and later, where if, then and else are schema keywords. OpenAPI 3.0 documents are skipped.

Related rules