-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathvalidators.py
More file actions
35 lines (28 loc) · 954 Bytes
/
validators.py
File metadata and controls
35 lines (28 loc) · 954 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import re
def phone_is_valid(phone):
"""
:param phone: Телефон для валидации
:type phone: str
:return: Результат валидации
:rtype: bool
"""
return True if re.match(r'^((8|\+7)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{7,10}$', phone) else False
def length_is_valid(obj, min_=None, max_=None):
"""
:param obj: Любой объект, у которого релизован метод __len__
:param min_: минимальное значение для сравнения
:param max_: максимальное значение для сравнения
:type min_: int or float
:type max_: int or float
:return: Результат сравнения
:rtype: bool or None
"""
length = len(obj)
res = None
if min_:
res = length >= min_
if not res:
return res
if max_:
res = length <= max_
return res