serverless framework request validator schema: How to not accept those object key and value pair that is not included in json schema?

Viewed 18

Im using request validator schema to validate incoming post request -> https://www.serverless.com/framework/docs/providers/aws/events/apigateway#request-schema-validators

test.json file:

{
  "definitions": {},
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "title": "POST/cashin",
  "required": ["name"],
  "properties": {
    "name": {
      "type": "string"
    }
  }
}

serverless.yml file:

testFunction:
  handler: src/controllers/test.create
  events:
    - http:
        method: POST
        path: /${self:custom.apiVersion}/create
        request:
          schemas:
            application/json: ${file(./src/schemas/test.json)}

It's fine, it is validating when property"name" is not included in the post request, however as you can see in my json file, only name is in the property. when i include like -> "name": "test", "age": 2, the age is accepting.

{
    "status": 200,
    "success": true,
    "data": {
        "name": "test",
        "age": 2
    }
}

What i want is only the key and value pairs that i set on test.json file will be accepted. I havent seen any documentations on json schema regarding this, hoping that has an alternative solution for this.

1 Answers

I already found my solution.

Simply, include "additionalProperties": false so that it wont accept any properties that is not included in .json file

Related