5
votes

I want to validate a json input through json schema. The positive case works for intended objects and properties. But I want to validate against extra objects, parameters which are not mentioned in the schema.

Basically fail the validation if garbage data detected in the json

1
Can you post some things that you've tried? - Dan Hoerst
{ "type":"object", "required":true, "properties":{ "name": { "type":"object", "id": "build", "required":true, "properties":{ "address": { "type":"string", "id": "job_name", "required":true }, "email": { "type":"string", "id": "publishing_area", "required":true } } } } } {"name":{"address":"x","email":"y","additional":"z"}} - Gaurav
I want to the request to be invalidated against the schema as "additional" is an extra key in that dictionary. Basically, strict dictionary keys - Gaurav
What are your schemas? What is your data? What are the expected results etc? - fge

1 Answers

12
votes

If you want to only have a certain set of properties in JSON objects and refuse others:

  • ensure the properties you want have a matching schema in either properties and patternProperties,
  • define additionalProperties to false:

    {
        "type": "object",
        "properties": { "p": {}, "q": {} },
        "additionalProperties": false
    }
    

will only allow for properties p and q to exist in object instances.