Specify list length range in Python Cerberus

Question:

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,
    },
}
Asked By: Asad Amir Khwaja

||

Answers:

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.

Answered By: Jay
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.