Forum Discussion

HenrikHL's avatar
HenrikHL
Frequent Contributor
3 years ago

Non-existent required fields

Hi,

I am wondering why the Validator of SwaggerHub does not report at least a Warning if I have listed a field as required without the field existing in the object?

Example:

components:
  schemas:
    container:
      type: object
      description: bla bla bla
      allOf:
        - type: object
          properties:
            containerReference:
              $ref: '#/components/schemas/equipmentReference'
        - type: object
          properties:
            ISOCode:
              $ref: '#/components/schemas/ISOEquipmentCode'
      required:
        - equipmentReference

As can be seen above the object "container" has defined equipmentReference as a required field. Problem is that this field was renamed from equipmentReference --> containerReference

It would be nice if SwaggerHub reports such an error in the Validation

  • HenrikHL's avatar
    HenrikHL
    Frequent Contributor

    As warning like this:

    Whenever a definition is not used in an API would be nice

  • Hi HenrikHL 

     

    The schema above is valid according to the JSON Schema specification. 

     

    If you wanted to be stricter (e.g. closed schema), you could set the additionalProperties keyword. 

    additionalProperties: false

    See more information at http://json-schema.org/understanding-json-schema/reference/object.html#additional-properties

     

    Note even with "additionalProperties: false", the schema is still valid. Instance validation (e.g. linting) could highlight the possible dead-end scenario. See this discussion for more info.

     

    SwaggerHub will not yet apply the linting for the scenario described in the linked discussion, but it will be merged from our OpenSource (where this has been added) in due course.

     

    Hope this helps.

    • HenrikHL's avatar
      HenrikHL
      Frequent Contributor

      Hi frankkilcommins ,

       

      Thank you for your reply and sorry for not getting back. I am aware that the schema is valid - that is not my point. If I declare a definition and not use it I get a validation error:

      I would like the same thing to happen if I mark a field required that does not exist. I would like a warning saying: "Required field does not exist in object" (or something similar)