schema-enum-types

Since 0.9.0 · fixed by --fix · In the rulesets: Recommended at error, Strict at error, All at error

This rule ensures that the values listed in the enum of a schema are allowed by the schema’s type.

Rule details

An enum value whose type is not allowed by the schema can never be sent or received: a message containing it would fail validation. Such values usually come from a missing or wrong type, or from unquoted YAML values (e.g. yes, 1.0) that are parsed as something other than strings.

The schema-enum-types rule checks every schema of an AsyncAPI document that has an enum list, and reports Enum value type is not in the schema type when one of the values is not allowed by the schema’s type:

  • strings require string, booleans require boolean, null requires null, arrays require array and objects require object,
  • integer values require integer or number,
  • other numeric values require number.

A schema that has an enum but no type is reported as well, since none of its values is allowed by an explicit type.

Examples

Good.

asyncapi: 3.0.0
info:
  title: Pet Events
  version: 1.0.0
components:
  schemas:
    PetStatus:
      type: [string, "null"]
      enum: [AVAILABLE, ADOPTED, null]
    Rating:
      type: number
      enum: [1, 2.5, 5]

Bad. The values are integers, but the schema allows only strings.

asyncapi: 3.0.0
info:
  title: Pet Events
  version: 1.0.0
components:
  schemas:
    Priority:
      type: string
      enum: [1, 2, 3]

Automatic fixes

--fix sets the type of a reported schema to the type of its enum values, provided that all values have the same type and the schema has no format. In the “Bad” example above, the type of Priority is changed to integer. Schemas mixing values of different types are not fixed.

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.asyncapi has the details.

commands:
  lint:
    asyncapi:
      rules:
        schema-enum-types: off

Related rules

Last updated on View as Markdown
Was this helpful?