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 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:
stringaccepts strings,integeraccepts integral numbers,numberaccepts integral and fractional numbers,booleanacceptstrueandfalse,arrayaccepts arrays,objectaccepts objects.
Swagger 2.0 has no null type, so null never matches, and no value matches
the file type.
The OpenAPI linter has a rule of the same name, schema-enum-types, for OpenAPI 3.x documents.
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
--fix 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.
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-types: off