redocly-cli
redocly-cli copied to clipboard
Detect properties with no type defined
Describe the solution you'd like
given a specification , it would like to ensure that all properties has a given type assidned (after resolution of $ref , and may be combination of allof etc)
to me most of the time if the properties is not defined is not on purpose but because it has been forgoten . I know that the schema is valid but still looks at least a warning would increase the API quality
Describe alternatives you've considered
create a dedicated rules could be an alternative
here name has no type and not detected as such
"Pet": {
"type": "object",
"required" : ["id", "name"],
"properties": {
"id": {
"type": "string"
},
"name" : {
"description" : "this is the name of the pet"
}
}
Hi! I wonder if something really simple like the following can help you:
rules:
rule/require-type-in-schemas:
subject:
type: Schema
assertions:
requireAny:
- type
- allOf
- anyOf
- oneOf
However, in general case, I'd recommend writing a custom rule for that.
This rule will work for you. That way you don't need to define every possible JSON Schema keyword
rules:
rule/require-type-in-schema:
severity: warn
message: '`type` must be defined'
subject:
type: Schema
property: type
assertions:
defined: true
@jeremyfiel thanks, it's a good one. However, it will fail to validate cases like this:
components:
schemas:
Foo:
allOf:
- type: object
- properties:
bar:
type: string
It will require defining types at two more levels, which is excessive:
components:
schemas:
Foo:
type: object # <- redundant
allOf:
- type: object
- properties:
bar:
type: string
type: object # <- redundant
Thanks @jeremyfield , i think this kind rules can be part of the default rules set of redocly , as most of the time not providing a type is a mistake .
but @tatomyr just input more complex use cases , my issue was on "simple" model , but that does not fly as soon as you enter into the Json schema allOf and co , complexity 😥
@jeremyfiel thanks, it's a good one. However, it will fail to validate cases like this:
components: schemas: Foo: allOf: - type: object - properties: bar: type: stringIt will require defining types at two more levels, which is excessive:
components: schemas: Foo: type: object # <- redundant allOf: - type: object - properties: bar: type: string type: object # <- redundant
I think that's the position of JSON Schema spec too.. declaring type in every schema is redundant, that's why it's not a required field to begin with. If someone wants that behavior, they will have a lot of duplication in their schemas.