Strict schema validation for Cerberus

377 Views Asked by At

I'm using Cerberus version 1.1.

The Cerberus required validation rule appears to default to False, with the result being that an empty document is perfectly valid.

>>> schema = {
    'spam': {'type': 'string'}
}

>>> v = Validator()
>>> v.validate({}, schema)
True

But I have a very lengthy schema document which needs to be strictly enforced. Is there a way to default to 'required': True for the entire schema, such that every defined field is required?

As it stands, I seem to have to paste 'required': True into every field definition, which seems needlessly redundant.

1

There are 1 best solutions below

0
On

Sure, you can use simple Python idioms to design your schema:

schema = {
    'spam': {'type': 'string'},
    …
}
for field in schema:
    schema[field].update({'required': True})