redocly-cli icon indicating copy to clipboard operation
redocly-cli copied to clipboard

Detect properties with no type defined

Open LasneF opened this issue 1 year ago • 5 comments

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"
                    }
            }
            

noType.json

LasneF avatar Jul 08 '24 16:07 LasneF

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.

tatomyr avatar Jul 09 '24 10:07 tatomyr

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 avatar Jul 09 '24 16:07 jeremyfiel

@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

tatomyr avatar Jul 09 '24 16:07 tatomyr

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 😥

LasneF avatar Jul 09 '24 16:07 LasneF

@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

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.

jeremyfiel avatar Jul 09 '24 16:07 jeremyfiel