简体   繁体   中英

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,
    },
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM