JSONSchema – required properties not working

I’m new to JSON and I wouldn’t be surprised if I missed something really simple, however I tried and failed to find what exactly I am doing wrong in my schema and why its validating some things incorrectly. This is my schema:

apartment_schema = {
    "type": "object",
    "properties": {
        "Apartments": {"type": "object"},
        "properties": {"ap1": {"type": "object",
                                 "required": ["count", "ages"],
                                 "properties": {"count": {"type": "number"},
                                                "ages": {"type": "array", "items": {"type": "number"}}},
                                 "additionalProperties": False,
                                 },
                       "ap2": {"type": "object",
                                "required": ["count", "ages"],
                                "properties": {"count": {"type": "number"},
                                               "ages": {"type": "array", "items": {"type": "number"}}},
                                "additionalProperties": False,
                                },
                       "ap3": {"type": "object",
                                     "required": ["count", "ages"],
                                     "properties": {"count": {"type": "number"},
                                                    "ages": {"type": "array", "items": {"type": "number"}}},
                                     "additionalProperties": False,
                                     },
                       },
        "required": ["ap1", "ap2", "ap3"], 
        "additionalProperties": False,
            },
    "additionalProperties": False,
    "required": ["Apartments"]
}

I am trying to validate a string by using json.loads and then the validate function against this schema, but when i try this, I get this message:

jsonschema.exceptions.SchemaError: ['ap1', 'ap2', 'ap3'] is not of type 'object', 'boolean'

Here is my how I try to validate it, and against what:

def validateJson(jsonData):
    try:
        jsonschema.validate(instance=jsonData, schema=apartment_schema)
    except jsonschema.exceptions.ValidationError:
        return False
    return True
print(validateJson(json.loads("{"Apartments": {"ap1": {"count": 1, "ages": [40]},"ap3": {"ages": [10,15]}}}"))

This validation passes and I don’t get the error message if I just remove only the one required part out of the schema, even though it shouldn’t pass, since it is missing one of the required parameters (count). When I put in different strings it aslo seems that none of the other “required” fields seem to be working even though they don’t raise an error. What am I doing incorrectly here?

Answer

You have an extra properties keyword right below the “Apartments” properties declaration that shouldn’t be there — so everything below that is being parsed at the wrong level. I think you are intending that the properties “ap1”, “ap2” and “ap3” should be at the same level in the data as “Apartments”?