电子邮件和 URL 验证器

StrictYAML 可以验证电子邮件(使用简化的正则表达式)和 URL。

from strictyaml import Email, Url, Map, load
from ensure import Ensure

schema = Map({"a": Email(), "b": Url()})

解析

a: [email protected]
b: https://user:[email protected]:443/path?k=v#frag
Ensure(load(yaml_snippet, schema)).equals({"a": "[email protected]", "b": "https://user:[email protected]:443/path?k=v#frag"})

异常

a: notanemail
b: notaurl
load(yaml_snippet, schema)
strictyaml.exceptions.YAMLValidationError:
when expecting an email address
found non-matching string
  in "<unicode string>", line 1, column 1:
    a: notanemail
     ^ (line: 1)

可执行规范

文档从 email-url.story storytests 自动生成。