Search code examples
pythoncerberus

Specify list length range in Python Cerberus


I need to set the minimum and maximum length of a list between 2 - 5. Is there a way to specify this in Python Cerberus. Here's what I have currently but this allows lists of all sizes:

  {
    "levels": {
        "type": "list",
        "schema": {
            "type": "string",
            "required": True,
            "nullable": False,
            "empty": False,
        },
        "required": True,
    },
}

Solution

  • You can use minlength/maxlength rules in your schema for your list -

    {
        "levels": {
            "type": "list",
            "minlength": 2,
            "maxlength": 5,
            "schema": {
                "type": "string",
                "required": True,
                "nullable": False,
                "empty": False,
            },
            "required": True,
        },
    }
    

    This will make sure that length of levels list is between 2-5.