WR-Schemas
wr-schemas的Python项目详细描述
模式描述:
- a data structure
- a mapping of one data structure into another
架构由字段列表组成。
字段没有类型-在某种程度上,它本身就是一个类型。字段不是类型,而是双向的 地图绘制。给定两个不同的数据结构x和y,映射描述如何从 y.f以及如何从x.f计算y.f。
字段支持以下属性:
- name
- mapping
- default
- source_name (source_names)
- min_len, max_len, auto_trim
- min, max
- choices
- regex
- required, forbidden
- nullable
另外:
- Nested fields are supported.
- Fields are easy to clone for reuse.
- Fields and schemas are easy to reverse.
- Schemas are easy to chain.
fromwr_schemasimportField,Schema,MappingsclassFields:user_id=Field('id',mapping=int,min=1)user_username=Field('username',min_len=5,max_len=100,regex=r'^[a-zA-Z0-9_\-\.@]+$')user_password=Field('password',min_len=10,max_len=100,regex=r'^[a-zA-Z0-9]+$')user_dob=Field('date_of_birth',mapping=Mappings.date())CreateUser=Schema(Fields.user_username.clone(required=True),Fields.user_password.clone(default=None),Fields.user_dob.clone(default=None),)payload=CreateUser.load({'username':'marcus.aurelius@rome.gov'})assertpayload.username=='marcus.aurelius@rome.gov'assertpayload.passwordisNoneassertpayload.date_of_birthisNoneprint(CreateUser.dump(payload))
烧瓶:
fromwr_schemasimportField,Schema,Mappingsfromwr_schemas.flask_requestimportFlaskRequestSchemaMixinCreateUser=Schema(Field('username',required=True),Field('password',required=True),mixins=[FlaskRequestSchemaMixin],)user=CreateUser.from_request()