一个简单的验证数据包
vlight的Python项目详细描述
重量
验证数据的简单包
开始
安装
如果您有旧版本,请执行以下操作:
pip uninstall vlight
安装最新版本:
pip install vlight
使用vlight
fromvlightimportv
示例
使用电子邮件验证简单的登录表单:
fromvlightimportvalid# also you can use "v" helper method
v Django的灯光
create validation rules and set data
fromvlightimportvvalid=v({"email":"req|email","password":"req|min:8|max:255"},data={"email":"test@gmail.com","password":"123"# this field have minimum character error})#check data is validifvalid.isOk():# submit your formpasserror=valid.errors()#check have errorifvalid.fails():msgs=error.all()#check a field have erroriferror.has("email"):#this block run when your email field have errorpass#get field errors messagesprint(error.get("email"))# you can set returned error lengthprint(error.get("email",2))#customize error messages#to customize all fields default messages:valid=v({"email":"required|email","password":"required|min:8|max:255"},data={"email":"test@gmail.com","password":"123"},messages={"required":"{field} required ","email":"email field with value [{value}] not valid",})#or you can customize only a field messagesvalid=v({"email":"required|email""|required.msg:enter your email""|email.msg:email is not valid","password":"required|min:8|max:255""|str.min:your password is easy""|str.max:your selected password so hard"},data={"email":"test@gmail.com","password":"123"})
Django的一个例子
views.py
from django.shortcuts import render
from vlight import v
def index(request, **kwargs):
msgs = []
if request.method == "POST":
valid = v({
"email": "required|email",
"password": "required|min:8|max:255"
}, data=request.POST)
if valid.fails():
msgs = valid.errors().all()
else:
msgs.append("your form submitted!")
return render(request, "index.html", {"login_errors": msgs})
index.html
金贾2
<form action="/" method="post">
<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
<label for="email">email:</label>
<input id="email" type="text" name="email">
<br>
<label for="password">password:</label>
<input id="password" type="password" name="password">
<input type="submit" value="login">
{% for error in login_errors %}
<li><a href="#">{{ error }}</a></li>
{% endfor %}
</form>
支持的规则
required
检查数据是否有字段且数据不为空
"required"
email
模式:^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$
"email"
ip
模式:^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$
"ip"
url
模式:^http(s?):\/\/(www\.)?(((\w+(([\.\-]{1}([a-z]{2,})+)+)(\/[a-zA-Z0-9\_\=\?\&\.\#\-\W]*)*$)|(\w+((\.([a-z]{2,})+)+)(\:[0-9]{1,5}(\/[a-zA-Z0-9\_\=\?\&\.\#\-\W]*)*$)))|(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}(([0-9]|([1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]+)+)(\/[a-zA-Z0-9\_\=\?\&\.\#\-\W]*)*)((\:[0-9]{1,5}(\/[a-zA-Z0-9\_\=\?\&\.\#\-\W]*)*$)*))$
"url"
not_in
in
检查项中的值
"in:small,larg"
regex
not_regex
即将推出
ext
检查路径扩展名
"ext:jpg,png,jpeg"
int
整数验证
"int"
^
"max:255"
您可以更改int value的规则以检查max integer value
"int|max:255"
[即将添加更多验证规则]
制定规则
docs coming soon
最新版本更改
0.1
- 修复错误
联系人
许可证
这个项目是根据麻省理工学院的许可证授权的