我真的很喜欢 Cerberus,但我无法在文档中找到一个简单的案例。我想要fudge
一个带有~
前缀的字符串类型。我根本不知道该怎么做..
fudge_type = cerberus.TypeDefinition('fudge_type', (str,), ())
class MyValidator(cerberus.Validator):
types_mapping = cerberus.Validator.types_mapping.copy()
types_mapping['fudge'] = fudge_type
def __init__(self, *args, **kwargs):
if 'additional_context' in kwargs:
self.additional_context = kwargs['additional_context']
super(MyValidator, self).__init__(*args, **kwargs)
@property
def additional_context(self):
self._error(field, "INVALID!")
return self._config.get('additional_context', 'bar')
def _validate_type_fudge(self, field, value):
self._error(field, "INVALID!")
make_use_of(self.additional_context)
validator = MyValidator()
validator.validate({'name': 'yada'}, {'name': {'type': 'fudge'}}) # => True
这似乎是一个简单的案例..所以感觉我在做完全错误的事情。