Linter rulesSwagger 2.0schema-enum-types

schema-enum-types

Since 0.9.0 · fixed by --fix

This rule ensures that all values of an enum list match the declared type.

Rule Details

An enum value that does not match the type can never be valid, and usually indicates a mistake in either the type or the values.

The schema-enum-types rule checks every typed object of a Swagger 2.0 document that has an enum list: schemas, non-body parameters, response headers and items objects (objects with a $ref are checked where they are defined). It reports Enum value type is not in the schema type when the object has no type, or when at least one value does not match it:

  • string accepts strings,
  • integer accepts integral numbers,
  • number accepts integral and fractional numbers,
  • boolean accepts true and false,
  • array accepts arrays,
  • object accepts objects.

Swagger 2.0 has no null type, so null never matches, and no value matches the file type.

Configuration

None.

Examples

Good.

swagger: '2.0'
info:
  title: Tasks
  version: 1.0.0
paths: {}
definitions:
  Status:
    type: string
    enum: [OPEN, CLOSED]
  Ratio:
    type: number
    enum: [1, 2.5]

Bad. The values of Priority are integers, and Status has no type.

swagger: '2.0'
info:
  title: Tasks
  version: 1.0.0
paths: {}
definitions:
  Priority:
    type: string
    enum: [1, 2, 3]
  Status:
    enum: [OPEN, CLOSED]

Automatic fixes

When invoked with --fix, this rule sets the type of a reported object to the type of its enum values, provided that all values have the same type (and are not null) and the object has no format. In the “Bad” example above, Priority gets type: integer and Status gets type: string. Objects mixing values of different types are not fixed.

When Not to Use It

There is no good reason to keep enum values that do not match the type, so this rule should always be enabled.

Compatibility

This rule is compatible with Swagger 2.0 documents. OpenAPI 3.x documents are linted by the OpenAPI linter, which has a rule of the same name.

Related rules

Further reading