Python Eve:文档级验证
Python Eve: document-level validation
我们已经多次使用字段级验证,它非常棒而且功能强大。但有时,文档本身只有通过评估多个领域才有效。更改涉及的任何字段都必须触发验证。
我们现在所做的是将验证应用于涉及的每个字段 - 它在 POST.
上多次运行验证
有没有办法将验证规则应用于文档本身?
例如假设 some_thing 有两个字段,验证会考虑这两个字段。如果其中一个发生变化,我们必须针对另一个进行验证。
这有效...
验证器(为清楚起见进行了简化):
def _validate_custom_validation(self, custom_validation, field, value):
if field == "field1":
f1 = value
f2 = self.document.get('field2')
if field == "field2":
f1 = self.document.get('field1')
f2 = value
if custom_validation and not is_validate(f1, f2):
self._error(field, "validation failed...")
然后是架构定义:
DOMAIN = {
some_thing: {
schema: {
field1: {
'type': 'string',
'custom_validation': True
},
field1: {
'type': 'string',
'custom_validation': True
}
}
}
}
但是我们想做这样的事情:
验证者
def _validate_custom_validation(self, custom_validation):
f1 = self.document.get('field1')
f2 = self.document.get('field2')
if custom_validation and not is_validate(f1, f2):
self._error(resource, "validation failed...")
然后是架构定义:
DOMAIN = {
some_thing: {
'custom_validation': True,
schema: {
field1: {
'type': 'string'
},
field1: {
'type': 'string'
}
}
}
}
这可能吗?
您可以重写主要验证方法,使其首先检查标准规则,然后再检查架构级别规则:
class validator_decorator(Validator):
def validate(self, document, schema=None, update=False, normalize=True):
super(validator_decorator, self).validate(document, schema=schema, update=update, normalize=normalize)
def validate_schema_rule(rule, document):
validator = self.__get_rule_handler('validate', rule)
validator(self.schema, document)
schema_rules = app.config['DOMAIN'][self.resource].get('validation')
if schema_rules:
for rule in schema_rules:
validate_schema_rule(rule, document)
return not bool(self._errors)
此验证器允许您执行以下操作
'users': {
'validation': ['validator_name'],
'schema': ...
}
当然你需要实现 validator_name,与 documantation says 相同 - 在 validator_decorator class
中
我们已经多次使用字段级验证,它非常棒而且功能强大。但有时,文档本身只有通过评估多个领域才有效。更改涉及的任何字段都必须触发验证。
我们现在所做的是将验证应用于涉及的每个字段 - 它在 POST.
上多次运行验证有没有办法将验证规则应用于文档本身?
例如假设 some_thing 有两个字段,验证会考虑这两个字段。如果其中一个发生变化,我们必须针对另一个进行验证。
这有效...
验证器(为清楚起见进行了简化):
def _validate_custom_validation(self, custom_validation, field, value):
if field == "field1":
f1 = value
f2 = self.document.get('field2')
if field == "field2":
f1 = self.document.get('field1')
f2 = value
if custom_validation and not is_validate(f1, f2):
self._error(field, "validation failed...")
然后是架构定义:
DOMAIN = {
some_thing: {
schema: {
field1: {
'type': 'string',
'custom_validation': True
},
field1: {
'type': 'string',
'custom_validation': True
}
}
}
}
但是我们想做这样的事情:
验证者
def _validate_custom_validation(self, custom_validation):
f1 = self.document.get('field1')
f2 = self.document.get('field2')
if custom_validation and not is_validate(f1, f2):
self._error(resource, "validation failed...")
然后是架构定义:
DOMAIN = {
some_thing: {
'custom_validation': True,
schema: {
field1: {
'type': 'string'
},
field1: {
'type': 'string'
}
}
}
}
这可能吗?
您可以重写主要验证方法,使其首先检查标准规则,然后再检查架构级别规则:
class validator_decorator(Validator):
def validate(self, document, schema=None, update=False, normalize=True):
super(validator_decorator, self).validate(document, schema=schema, update=update, normalize=normalize)
def validate_schema_rule(rule, document):
validator = self.__get_rule_handler('validate', rule)
validator(self.schema, document)
schema_rules = app.config['DOMAIN'][self.resource].get('validation')
if schema_rules:
for rule in schema_rules:
validate_schema_rule(rule, document)
return not bool(self._errors)
此验证器允许您执行以下操作
'users': {
'validation': ['validator_name'],
'schema': ...
}
当然你需要实现 validator_name,与 documantation says 相同 - 在 validator_decorator class
中