網站首頁 編程語言 正文
前言
首先安裝validators庫:
pip install validators
validators.between(value, min=None, max=None)
驗證一個數(shù)字value是否在最小值min和最大值max之間,value不僅僅可以是整數(shù),也可以是其它數(shù)據(jù)類型,例如floats, decimals 和 dates。
源碼解析:
def between(value, min=None, max=None):
"""
Validate that a number is between minimum and/or maximum value.
This will work with any comparable type, such as floats, decimals and dates
not just integers.
This validator is originally based on `WTForms NumberRange validator`_.
.. _WTForms NumberRange validator:
https://github.com/wtforms/wtforms/blob/master/wtforms/validators.py
Examples::
>>> from datetime import datetime
>>> between(5, min=2)
True
>>> between(13.2, min=13, max=14)
True
>>> between(500, max=400)
ValidationFailure(func=between, args=...)
>>> between(
... datetime(2000, 11, 11),
... min=datetime(1999, 11, 11)
... )
True
:param min:
The minimum required value of the number. If not provided, minimum
value will not be checked.
:param max:
The maximum value of the number. If not provided, maximum value
will not be checked.
.. versionadded:: 0.2
"""
示例代碼:
from validators import between
from datetime import datetime
aa = between(3, min=2)
print(aa)
bb = between(3, min=2, max=5)
print(bb)
cc = between(3, max=2)
print(cc)
dd = between(datetime(2022, 9, 21), min=datetime(2022, 9, 19))
print(dd)
ee = between(datetime(2022, 9, 21), min=datetime(2022, 9, 19), max=datetime(2022, 10, 19))
print(ee)
ff = between(datetime(2022, 9, 21), max=datetime(2022, 9, 19))
print(ff)
運行結果:
validators.domain(value)
驗證value是否是一個有效域。如果value是一個有效域名,函數(shù)返回True, 否則返回 ValidationFailure.
源碼解析:
def domain(value):
"""
Return whether or not given value is a valid domain.
If the value is valid domain name this function returns ``True``, otherwise
:class:`~validators.utils.ValidationFailure`.
Examples::
>>> domain('example.com')
True
>>> domain('example.com/')
ValidationFailure(func=domain, ...)
Supports IDN domains as well::
>>> domain('xn----gtbspbbmkef.xn--p1ai')
True
.. versionadded:: 0.9
.. versionchanged:: 0.10
Added support for internationalized domain name (IDN) validation.
:param value: domain string to validate
"""
示例代碼:
from validators import domain
aa = domain('example.com')
print(aa)
bb = domain('example..com')
print(bb)
運行結果:
validators.email(value, whitelist=None)
驗證是否是合法的郵件地址,如果是,函數(shù)返回True, 否則返回 ValidationFailure.
源碼解析:
def email(value, whitelist=None):
"""
Validate an email address.
This validator is based on `Django's email validator`_. Returns
``True`` on success and :class:`~validators.utils.ValidationFailure`
when validation fails.
Examples::
>>> email('someone@example.com')
True
>>> email('bogus@@')
ValidationFailure(func=email, ...)
.. _Django's email validator:
https://github.com/django/django/blob/master/django/core/validators.py
.. versionadded:: 0.1
:param value: value to validate
:param whitelist: domain names to whitelist
:copyright: (c) Django Software Foundation and individual contributors.
:license: BSD
"""
示例代碼:
from validators import email
aa = email("123@qq.com")
print(aa)
bb = email("123@.com")
print(bb)
運行結果:
validators.ip_address.ipv4(value)?
驗證是否是合法的ipv4地址,如果是,函數(shù)返回True, 否則返回 ValidationFailure.
源碼解析:
def ipv4(value):
"""
Return whether a given value is a valid IP version 4 address.
This validator is based on `WTForms IPAddress validator`_
.. _WTForms IPAddress validator:
https://github.com/wtforms/wtforms/blob/master/wtforms/validators.py
Examples::
>>> ipv4('123.0.0.7')
True
>>> ipv4('900.80.70.11')
ValidationFailure(func=ipv4, args={'value': '900.80.70.11'})
.. versionadded:: 0.2
:param value: IP address string to validate
"""
示例代碼:
from validators.ip_address import ipv4
aa = ipv4("127.0.0.1")
print(aa)
bb = ipv4("333.1.1.1")
print(bb)
運行結果:
validators.ip_address.ipv6(value)
驗證是否是合法的ipv6地址,如果是,函數(shù)返回True, 否則返回 ValidationFailure.
源碼解析:
def ipv6(value):
"""
Return whether a given value is a valid IP version 6 address
(including IPv4-mapped IPv6 addresses).
This validator is based on `WTForms IPAddress validator`_.
.. _WTForms IPAddress validator:
https://github.com/wtforms/wtforms/blob/master/wtforms/validators.py
Examples::
>>> ipv6('abcd:ef::42:1')
True
>>> ipv6('::ffff:192.0.2.128')
True
>>> ipv6('::192.0.2.128')
True
>>> ipv6('abc.0.0.1')
ValidationFailure(func=ipv6, args={'value': 'abc.0.0.1'})
.. versionadded:: 0.2
:param value: IP address string to validate
"""
示例代碼:
from validators.ip_address import ipv6
aa = ipv6("abcd:ef::42:1")
print(aa)
bb = ipv6("abc.0.0.1")
print(bb)
運行效果:
validators.length(value, min=None, max=None)?
驗證給定的字符串長度是否在指定范圍內。
示例代碼:
from validators import length
aa = length('aaa', min=2)
print(aa)
bb = length("aaa", min=2, max=5)
print(bb)
cc = length("aaa", max=2)
print(cc)
運行結果:
validators.mac_address(value)?
驗證是否是合法的mac地址,如果是,函數(shù)返回True, 否則返回 ValidationFailure.
示例代碼:
from validators import mac_address
aa = mac_address('01:23:45:67:ab:CD')
print(aa)
bb = mac_address('00:00:00:00:00')
print(bb)
運行結果:
validators.url(value, public=False)?
驗證是否是合法的url,如果是,函數(shù)返回True, 否則返回 ValidationFailure.
Parameters:
- value – 要驗證的url
- public – (default=False) Set True to only allow a public IP address
示例代碼:
from validators import url
aa = url('http://www.baidu.com')
print(aa)
bb = url("www.baidu.com")
print(bb)
cc = url("http://127.0.0.1")
print(cc)
dd = url("http://127.0.0.1", public=True)
print(dd)
運行結果:
原文鏈接:https://blog.csdn.net/weixin_44799217/article/details/126981838
相關推薦
- 2022-07-09 Python3中的re.findall()方法及re.compile()_python
- 2023-01-31 iOS?底層alloc?init?new?源碼流程示例分析_IOS
- 2022-06-30 React-hooks中的useEffect使用步驟_React
- 2022-05-19 關于python中不同函數(shù)讀取圖片格式的區(qū)別淺析_python
- 2022-07-24 docker搭建Zookeeper集群的方法步驟_docker
- 2022-09-17 ASP.NET?Core中Grpc通信的簡單用法_實用技巧
- 2022-10-03 Python中不同圖表的數(shù)據(jù)可視化的實現(xiàn)_python
- 2022-12-01 Python?Flask前端自動登錄功能實現(xiàn)詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支