How can I validate that certain type is a list and it contains only eg string elements, of unknown number?
My current solution is
'categories_id' : {'required' : False, 'type' : ['string','list']},
but it doesn't do the trick, is also returns True
when you supply just a single string, not in a list.
The schema mentioned in the question 'categories_id': {'required': False, 'type': ['string','list']}
makes Cerberus check categories_id
field is either a string or a list. So a single string will return true.
To make Cerberus check that categories_id
is a list of strings with any number of items, following schema should be used
{'categories_id': {'required': False, 'type': 'list', 'schema': {'type': 'string'}}}
I haven't found a way to do that using cerberus
, but it should be pretty simple without any modules:
def validate(l):
if isinstance(l, list):
return all(isinstance(I, str) for i in l)
This can be done using valuesrules
>>> schema = {'categories_id':
... {'type': 'list',
... 'valuesrules': {'type': 'string'}}}
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.