schema-format-type-mismatch
Since 0.9.0 · fixed by --fix · In the rulesets: Recommended at error, Strict at error, All at error
This rule ensures that the format of a schema is consistent with its
type.
Rule details
Formats apply to values of specific types. A schema such as
type: integer, format: email is contradictory: validators ignore the format,
and code generators produce unexpected types.
The schema-format-type-mismatch rule checks every schema of an AsyncAPI
document whose format is in the
OpenAPI format registry or in
openapi.custom_formats of the configuration file, and reports
Schema type and format do not match when none of the schema’s types is a JSON
type of the format:
- the integer formats (
int8touint64,int32,int64,double-int,sf-integer) needintegerornumber, andint64anduint64also allowstring; float,doubleandsf-decimalneednumber;decimalanddecimal128allowstringandnumber,unixtimealsointeger;- the others (
date,date-time,uuid,email,http-date, …) needstring.
A schema with one of these formats but without a type is reported as well.
Other formats are left to
schema-format-unknown.
The Formats page lists every format.
Configuration
The configuration block is an object with a single optional boolean property,
fixTypeFromFormat, which controls whether reported issues can be fixed
automatically (see “Automatic fixes”). The default value is true.
Configuration example. The following configuration reports mismatches, but does not fix them.
version: "1.0"
commands:
lint:
asyncapi:
rules:
schema-format-type-mismatch:
- error
- fixTypeFromFormat: false
Examples
Good.
asyncapi: 3.0.0
info:
title: User Events
version: 1.0.0
components:
schemas:
UserSignedUp:
type: object
properties:
email:
type: string
format: email
signed_up_at:
type: string
format: date-time
Bad.
asyncapi: 3.0.0
info:
title: User Events
version: 1.0.0
components:
schemas:
UserSignedUp:
type: object
properties:
email:
type: integer
format: email
signed_up_at:
format: date-time
Automatic fixes
When fixTypeFromFormat is true and the linter is invoked with the --fix
option, this rule sets the type of every reported schema to the type the
format applies to. In the “Bad” example above, both properties get
type: string.
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-format-type-mismatch: off