'setting required on a json-schema array

I am trying to figure out how to set required on my json-schema array of objects. The required property works fine on an object just not an array.

Here is the items part of my json schema:

        "items": {
        "type": "array",
        "properties": {
            "item_id": {"type" : "number"},
            "quantity": {"type": "number"},
            "price": {"type" : "decimal"},
            "title": {"type": "string"},
            "description": {"type": "string"}
        },
        "required": ["item_id","quantity","price","title","description"],
        "additionalProperties" : false
    }

Here is the json array I am sending over. The json validation should fail since I am not passing a description in these items.

       "items": [
        {
            "item_id": 1,
            "quantity": 3,
            "price": 30,
            "title": "item1 new name"
        },
        {
            "item_id": 1,
            "quantity": 16,
            "price": 30,
            "title": "Test Two"
        }
    ]


Solution 1:[1]

Maybe your validator only supports JSONSchema v3?

The way required works changed between v3 and v4:

Solution 2:[2]

I realize this is an old thread, but since this question is linked from jsonschema.net, I thought it might be worth chiming in...

The problem with your original example is that you're declaring "properties" for an "array" type, rather than declaring "items" for the array, and then declaring an "object" type (with "properties") that populates the array. Here's a revised version of the original schema snippet:

"items": {
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "item_id": {"type" : "number"},
            "quantity": {"type": "number"},
            "price": {"type" : "decimal"},
            "title": {"type": "string"},
            "description": {"type": "string"}
        },
        "required": ["item_id","quantity","price","title","description"],
        "additionalProperties" : false
    }
}

I would recommend against using the term "items" for the name of the array, to avoid confusion, but there's nothing stopping you from doing that...

Solution 3:[3]

In Python, for me it worked like this:


schema = {
    "type": "object",
    "properties": {
        "price": {"type": "number"},
        "name": {"type": "string", 'required': True}, # set require = True
        "details": {"type": "object"},
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Greg Stumph
Solution 3 Horlando Leão