プロパティが定義されていないオブジェクトスキーマ

フィールド構成が決まっているオブジェクトは、propertiesで各フィールドを定義します。

説明

固定のフィールドは properties に名前と型を定義し、必須のフィールドを required に列挙します。

想定される影響

既定では、定義したフィールド以外もオブジェクトに含められます。

対処方法

未定義のフィールドを拒否するには additionalProperties: false を指定します。キーが動的なマップでは additionalProperties に値のスキーマを指定してください。必須フィールドを省いた場合とフィールドを追加した場合の両方を検証します。

例

どちらも petType を必須にしています。2 つ目は code、message、petType の型を定め、それ以外のフィールドを拒否します。

変更前

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

変更後

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

参考資料