0

Having some issues trying to get to AWS API Gateway to error. Validation errors raise correctly for keys that have been defined when provided the wrong type, but if I add a random key to the request body that has not been defined in the model then API Gateway just carries on processing the request and returns a 200 Success response.

How can I define, in the model, that random keys are not allowed?

Model

{
  "type" : "object",
  "properties" : {
    "bool" : {
      "type" : "boolean"
    },
    "string" : {
      "type" : "string"
    },
    "int" : {
      "type" : "integer",
      "format" : "int32"
    }
  }
}

Request Body API GW Correctly Gives 200 Response

{
    "bool": true,
    "string": "foo",
    "int": 123
}

Invalid Request Body API GW Correctly Gives 400 Response

{
    "bool": "foo",
    "string": 123,
    "int": false
}

Invalid Request Body API GW Incorrectly Gives 200 Response

{
    "bool": true,
    "string": "foo",
    "int": 123,
    "bar":""
}

Any ideas on what I'm missing in the JSON schema?

Nick
  • 151
  • 9

1 Answers1

3

I needed to add the following to the model

"additionalProperties":false

Model

{
  "type" : "object",
  "additionalProperties":false,
  "properties" : {
    "bool" : {
      "type" : "boolean"
    },
    "string" : {
      "type" : "string"
    },
    "int" : {
      "type" : "integer",
      "format" : "int32"
    }
  }
}

from this issue Only allow properties that are declared in JSON schema

Nick
  • 151
  • 9