python极简主义pdf_XData: 为 Python 之禅写的极简主义数据验证工具
XData
一款非常实用的数据验证工具,
通常用于请求数据的验证.
Features
验证数据一步到位
容易扩展,容易自定义数据类型以及验证方式
无第三方依赖
Required
python >= 3.5
Installation
pip install xdata
Usage
Validated_data
from xdata import *
class UserSchema(Schema):
telephone = Str(length=11, required=True)
password = Str(min_length=8,max_length=16, required=True)
request_data = {
'telephone':'18180050000',
'password':'idonotknow'
}
schema = UserSchema(request_data)
if schema.valid:
print(schema.validated_data) # {'telephone': '18180050000', 'password': 'idonotknow'}
Errors
from xdata import *
class UserSchema(Schema):
telephone = Str(length=11, required=True)
password = Str(min_length=8, max_length=16, required=True)
request_data = {}
schema = UserSchema(request_data)
if not schema.valid:
print(schema.errors) # {'telephone': 'telephone is required', 'password': 'password is required'}
DataTypes
from xdata import *
DataType(required=True,default='11',choices=[])
Str(length=11, max_length=12,min_length=10,regex="")
Int(max=10000,min=12)
Bool(max=10000,min=12)
Decimal(left=5,right=2)
DateTime(max_datetime='2001-01-01 00:00:00', min_datetime='2000-01-01 00:00:00')
Date(max_date='2001-01-01', min_date='2000-01-01')
Time(max_time='06:00:00', min_time='05:00:00')
Test
coverage run --source=xdata -m pytest && coverage report
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
