程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Cerberus validator parameter validation

編輯:Python

The simplest way to use

# The simplest way to use
from cerberus import Validator
schema = {'name1': {'type': 'string'}, 'name2': {'type': 'string'}}
v = Validator(schema)
document = {'name1': 'john doe', 'name2': 0}
if v.validate(document):
print('data is valid')
else:
print(v.errors)

Output :

{'name2': ['must be of string type']}

Custom verification method code example : 

# coding=utf-8
from cerberus import Validator
class ParamValidator(Validator):
def __init__(self, *args, **kwargs):
self.res = {}
super(ParamValidator, self).__init__(*args, **kwargs)
def _validator_username(self, field, value):
print(" --- in _validator_username ---")
print("field = {}, value = {}".format(field, value))
name = value.get('username', None)
print("name = {}".format(name))
if not name:
return self._error('error_info', 'not username')
def _check_with_gender(self, field, value):
print("--- in _check_with_gender ---")
print("field = {} | value = {}".format(field, value))
self.res.update({"field": field, "value": value})
if __name__ == "__main__":
# No.1 Custom prefix is :_validator_<validator_name> The verification method of this paper is as follows
document = {'data': {'username': ''}}
# schema_rule in validator The corresponding value is username, Will automatically find _validator Prefixed _validator_username Method
schema_rule = {"data": {'validator': 'username'}}
pv = ParamValidator(schema_rule)
if not pv.validate(document):
print("pv.document = {}".format(pv.document))
print("pv.errors = {}".format(pv.errors))
else:
print("validator success end ---")
print('='*20 + ' Split line ' + '='*20)
# No.2 - Custom prefix is _check_with<field> The verification method of this paper is as follows
document = {'name': "1", 'age': 3, 'gender': 'male-'}
# schema_rule in gender In the corresponding value check_with The corresponding value of the field is gengder, Will automatically find _check_with Prefixed _check_with_gender Method execution
schema_rule = {
'name': {'type': 'string'},
'age': {'default': 18, 'type': 'integer'},
'gender': {'type': "string", "allowed": ["male", "female"], "check_with": "gender"}
}
pv = ParamValidator(schema_rule)
res = pv.validate(document)
print("res = {}".format(res))
print("pv.document = {}".format(pv.document))
print("pv.errors = {}".format(pv.errors))
print("pv.res = {}".format(pv.res))

Output :

 --- in _validator_username ---
field = data, value = {'username': ''}
name =
pv.document = {'data': {'username': ''}}
pv.errors = {'error_info': ['not username']}
==================== Split line ====================
--- in _check_with_gender ---
field = gender | value = male-
res = False
pv.document = {'gender': 'male-', 'age': 3, 'name': '1'}
pv.errors = {'gender': ['unallowed value male-']}
pv.res = {'field': 'gender', 'value': 'male-'}


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved