Object schema without defined properties

Use properties to describe objects with a fixed set of fields.

Description

Define each fixed field and its type in properties. List the fields that must be present in required.

Potential impact

By default, an object can still contain fields outside that list.

Remediation

Use additionalProperties: false to reject unknown fields, or an additionalProperties schema to constrain the values of a map with dynamic keys. Test an object missing a required field and one containing an extra field.

Examples

Both examples require petType. The second defines the types of code, message and petType and rejects other fields.

Before

json
{
  "components": {
    "schemas": {
      "GeneralError": {
        "type": "object",
        "required": [
          "petType"
        ]
      }
    }
  }
}

After

json
{
  "components": {
    "schemas": {
      "GeneralError": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "code": {
            "type": "integer",
            "format": "int32"
          },
          "message": {
            "type": "string"
          },
          "petType": {
            "type": "string"
          }
        },
        "required": [
          "petType"
        ]
      }
    }
  }
}

References