Is there a way to tell cerberus to have required
set to True
by default for all keys in the schema? This would save me some time, because most often I want to assert the existence of a key.
Have required set to True by default in cerberus
645 views Asked by lue3Seba At
1
I think there's no general solution to this, and different approaches are suited for different overall scenarios. Let me propose two:
Extending the schema
This is fairly simple, just add the
required
rule to all fields of a schema before employing it:Custom validator
As the
Validator
class has a method that checks all fields in regard to this rule, it can be overridden in a subclass:Note that this proposal doesn't consider the
excludes
rule as the original implementation.However, as this is part of the non-public methods, the underlying design might change unannounced in the future.