XData: 为 Python 之禅写的极简主义数据验证工具

2017-03-24 13:24:57 +08:00
 prasanta

Github: https://github.com/gaojiuli/xdata

XData

一款非常实用的数据验证工具, 通常用于请求数据的验证.

Features

Required

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

Github: https://github.com/gaojiuli/xdata

1268 次点击
所在节点    问与答
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/350013

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX