Skip to main content

No project description provided

Project description

基于pydantic的BaseModel下实现的validationRules

描述

目前标准结构化的python都开始使用pydantic做结构限制以及校验,每一次的validator都要调用validator装饰器后自己实现。本库是为了整合涵盖所有可能出现的需要校验方法进行封装的规则包。目的是开箱即用注解形式方便使用

前置

需要配合pydantic的validator使用

Install

pip install python-validation-rules==0.1.0

使用方式

例如:校验该字段是否是标准手机号

from pydantic import BaseModel, Field, validator

from validationRules.fieldRules import StringRules


class TestUserBase(BaseModel):
    userName: str = Field(description="用户名", alias="user_name")
    mobileNumber: str = Field(description="手机号", alias="mobile_number")
    password: str = Field(description="密码", alias="password")
    email: str = Field(description="邮箱", alias="email")
    IDCard: str = Field(description="身份证号", alias="id_card")

    @validator("userName")
    @StringRules.accountRule
    def check_account(cls, column):
        return column

    @validator("mobileNumber")
    @StringRules.mobileRule
    def check_mobile_number(cls, column):
        return column

    @validator("password")
    @StringRules.keywordCheckRule(password_level=1, min_length=8)
    def check_keyword(cls, column):
        return column

    @validator("email")
    @StringRules.emailRule(required=True)
    def check_email(cls, column):
        return column

    @validator("IDCard")
    @StringRules.IDCardRule(required=True)
    def check_id_card(cls, column):
        return column


# user_obj: TestUserBase = TestUserBase(
#
#     user_name="symoon",
#     mobile_number="13232633123"
# )
#
# print(user_obj)
# userName='symoon' mobileNumber='13232633123'

user_obj: TestUserBase = TestUserBase(

    user_name="symoon",
    mobile_number="13232633123",
    password="gsy121994",
    email="fsdfsfsf@qq.com",
    id_card="130"
)
print(user_obj)
# [132326331231] mobile is not 11 length (type=value_error)

方法描述

参数 类型 描述
required bool 是否为必填

字符串校验

accountRule:账号校验

标准账号校验方法

    @validator("userName")
    @StringRules.accountRule
    def check_account(cls, column):
        return column

mobileRule:手机号校验

手机号校验

    @validator("mobileNumber")
    @StringRules.mobileRule
    def check_mobile_number(cls, column):
        return column

keywordCheckRule:关键字符校验(密码校验)

参数 类型 描述
keyword_list List[str] 校验字符是否包含列表中字符
startswith str 校验是否以字符开头
endswith str 校验是否以字符结尾
min_length int 校验最小长度
max_length int 校验最大长度
password_level int 验证等级,默认为0 {3:"三种必须条件",2:"两种必须条件",3:"必须包含字母及数字",0:"不做验证"}
    @validator("password")
    @StringRules.keywordCheckRule(password_level=1, min_length=8)
    def check_keyword(cls, column):
        return column

emailRule:邮箱校验

邮箱校验

    @validator("email")
    @StringRules.emailRule(required=True)
    def check_email(cls, column):
        return column

IDCardRule:身份证校验

身份证校验

    @validator("IDCard")
    @StringRules.IDCardRule(required=True)
    def check_id_card(cls, column):
        return column

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-validation-rules-0.1.17.tar.gz (8.6 kB view details)

Uploaded Source

File details

Details for the file python-validation-rules-0.1.17.tar.gz.

File metadata

  • Download URL: python-validation-rules-0.1.17.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.10

File hashes

Hashes for python-validation-rules-0.1.17.tar.gz
Algorithm Hash digest
SHA256 5a0e02ba314a8af7d61e13b80e0fa21172133ffdab562a3b69e517717256adf7
MD5 c417d34b5a5f720f0241e4bca4643aed
BLAKE2b-256 eba3f238975307bdff191eac9b644e8333220274045478ee1e44d24b8487de64

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page