schema-enum-no-duplicates

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

This rule ensures that enum lists do not contain duplicate values.

Rule details

A duplicate enum value is redundant and usually a copy-paste mistake. It may also break code generators, which turn enum values into constants that must be unique.

The schema-enum-no-duplicates rule checks the enum list of every typed object of a Swagger 2.0 document: schemas, non-body parameters, response headers and items objects (objects with a $ref are checked where they are defined). It reports Duplicate enum value once for every value that is equal to a value listed before it.

Values are compared structurally: arrays are equal when their elements are equal in order, and objects are equal when they have the same keys with equal values, regardless of the order of the keys.

The OpenAPI linter has a rule of the same name, schema-enum-no-duplicates, for OpenAPI 3.x documents.

Examples

Good.

swagger: '2.0'
info:
  title: Users
  version: 1.0.0
paths: {}
definitions:
  UserStatus:
    type: string
    enum: [ACTIVE, INACTIVE]

Bad. ACTIVE is listed twice, and the two points differ only in the order of their keys.

swagger: '2.0'
info:
  title: Users
  version: 1.0.0
paths: {}
definitions:
  UserStatus:
    type: string
    enum: [ACTIVE, INACTIVE, ACTIVE]
  Corner:
    type: object
    enum:
      - { x: 1, y: 2 }
      - { y: 2, x: 1 }

Automatic fixes

--fix removes every duplicate value, keeping the first occurrence of each value.

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

commands:
  lint:
    swagger:
      rules:
        schema-enum-no-duplicates: off

Related rules

Further reading

Was this helpful?