我想验证url和电子邮件输入数据与python voluptuous,可能是这样的:
schema = Schema({
Required('url'): All(str, Url()),
Required('email'): All(str, Email())
})
Run Code Online (Sandbox Code Playgroud)
看看源代码,我看到妖娆有一个内置的Url功能,在电子邮件的情况下它没有,所以我想建立我自己的,问题是我不知道要调用这个功能在架构内部.
小智 9
更新:现在voluptuous有电子邮件验证器.
您可以像这样编写自己的验证器
import re
from voluptuous import All, Invalid, Required, Schema
def Email(msg=None):
def f(v):
if re.match("[\w\.\-]*@[\w\.\-]*\.\w+", str(v)):
return str(v)
else:
raise Invalid(msg or ("incorrect email address"))
return f
schema = Schema({
Required('email') : All(Email())
})
schema({'email' : "invalid_email.com"}) # <-- this will result in a MultipleInvalid Exception
schema({'email' : "valid@email.com"}) # <-- this should validate the email address
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2408 次 |
| 最近记录: |