Harsha Biyani
Harsha Biyani

Reputation: 7268

required and additionalProperties is not working for jsonschema module Python

I have following code :

from jsonschema import validate
schema_data = {
    "type" : "object",
    "properties" : {
        "price" : {"type" : "number"},
        "name" : {"type" : "string"},
        "additional" : {"type" : "number"},
        },
    "required": ["price", "name", "additional"],
    "additionalProperties": False
}

json_data = {"name" : "Eggs", "price" : 34.99, "new": 90}

This should give me an error for both required and additionalProperties as additional is not present in json_data and new is not present in schema_data. But script is not giving any error.

Do I need some extra to install? I am having following configuration:

Python 2.7.12,
jsonschema==3.0.1
attrs==19.1.0
six==1.12.0
pyrsistent==0.14.11

Upvotes: 1

Views: 2580

Answers (1)

Sachin Patel
Sachin Patel

Reputation: 499

You need to run below command for getting error

validate(json_data, schema_data)

So first you'll get error

'additional' is a required property

After fixing you'll get error for

Additional properties are not allowed ('new' was unexpected)

Upvotes: 2

Related Questions