0

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.

  • Does this answer your question? [Only allow properties that are declared in JSON schema](https://stackoverflow.com/questions/17530762/only-allow-properties-that-are-declared-in-json-schema) – derpirscher Sep 17 '22 at 11:04
  • Hi @derpirscher, thankyou for this i really appreciated your comment. I found solution to my problem here -> https://developer.zendesk.com/documentation/custom-data/custom-objects/schema-validation-reference-for-object-types/ – Poor programmer Sep 17 '22 at 11:09

1 Answers1

0

I already found my solution.

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

Tyler2P
  • 2,324
  • 26
  • 22
  • 31