|
@@ -1,3 +1,4 @@
|
|
|
|
+from ast import arg
|
|
import re
|
|
import re
|
|
from numbers import Number
|
|
from numbers import Number
|
|
from src.errors.argument_exception import argument_exception
|
|
from src.errors.argument_exception import argument_exception
|
|
@@ -9,7 +10,7 @@ class validator:
|
|
if not hasattr(cls, "instance"):
|
|
if not hasattr(cls, "instance"):
|
|
cls.instance = super(validator, cls).__new__(cls)
|
|
cls.instance = super(validator, cls).__new__(cls)
|
|
return cls.instance
|
|
return cls.instance
|
|
-
|
|
|
|
|
|
+
|
|
def check(self, value, method):
|
|
def check(self, value, method):
|
|
"""
|
|
"""
|
|
Валидация аргумента по лямбде
|
|
Валидация аргумента по лямбде
|
|
@@ -22,7 +23,7 @@ class validator:
|
|
|
|
|
|
if not method(value):
|
|
if not method(value):
|
|
raise argument_exception("Аргумент не прошел валидацию по лямбде")
|
|
raise argument_exception("Аргумент не прошел валидацию по лямбде")
|
|
-
|
|
|
|
|
|
+
|
|
return True
|
|
return True
|
|
|
|
|
|
def check_type(self, value, exp_type):
|
|
def check_type(self, value, exp_type):
|
|
@@ -36,10 +37,12 @@ class validator:
|
|
"""
|
|
"""
|
|
|
|
|
|
if not isinstance(value, exp_type):
|
|
if not isinstance(value, exp_type):
|
|
- raise argument_exception(f"Некорректный тип аргумента ({type(value)}, expected {exp_type})")
|
|
|
|
|
|
+ raise argument_exception(
|
|
|
|
+ f"Некорректный тип аргумента ({type(value)}, expected {exp_type})"
|
|
|
|
+ )
|
|
|
|
|
|
return True
|
|
return True
|
|
-
|
|
|
|
|
|
+
|
|
def check_type_any(self, value, *types):
|
|
def check_type_any(self, value, *types):
|
|
"""
|
|
"""
|
|
Валидация аргумента по соответствию одному из типов
|
|
Валидация аргумента по соответствию одному из типов
|
|
@@ -59,8 +62,10 @@ class validator:
|
|
break
|
|
break
|
|
|
|
|
|
if not flag:
|
|
if not flag:
|
|
- raise argument_exception("Аргумент не соответствует ни одному из ожидаемых типов "\
|
|
|
|
- + f"({type(value)}, expected={', '.join(types)})")
|
|
|
|
|
|
+ raise argument_exception(
|
|
|
|
+ "Аргумент не соответствует ни одному из ожидаемых типов "
|
|
|
|
+ + f"({type(value)}, expected={', '.join(types)})"
|
|
|
|
+ )
|
|
|
|
|
|
return True
|
|
return True
|
|
|
|
|
|
@@ -78,7 +83,9 @@ class validator:
|
|
value = str(value)
|
|
value = str(value)
|
|
|
|
|
|
if len(value) != length:
|
|
if len(value) != length:
|
|
- raise argument_exception(f"Несоответствующая длина аргумента ({len(value)}, expected {length})")
|
|
|
|
|
|
+ raise argument_exception(
|
|
|
|
+ f"Несоответствующая длина аргумента ({len(value)}, expected {length})"
|
|
|
|
+ )
|
|
|
|
|
|
return True
|
|
return True
|
|
|
|
|
|
@@ -95,10 +102,11 @@ class validator:
|
|
|
|
|
|
if value is int:
|
|
if value is int:
|
|
value = str(value)
|
|
value = str(value)
|
|
-
|
|
|
|
- if (len(value) > length if inclusive \
|
|
|
|
- else len(value) >= length):
|
|
|
|
- raise argument_exception(f"Превышена длина аргумента ({len(value)}, max={length})")
|
|
|
|
|
|
+
|
|
|
|
+ if len(value) > length if inclusive else len(value) >= length:
|
|
|
|
+ raise argument_exception(
|
|
|
|
+ f"Превышена длина аргумента ({len(value)}, max={length})"
|
|
|
|
+ )
|
|
|
|
|
|
return True
|
|
return True
|
|
|
|
|
|
@@ -116,13 +124,21 @@ class validator:
|
|
if value is int:
|
|
if value is int:
|
|
value = str(value)
|
|
value = str(value)
|
|
|
|
|
|
- if (len(value) < length if inclusive \
|
|
|
|
- else len(value) <= length):
|
|
|
|
- raise argument_exception("Недостаточная длина аргумента ({len(value)}, min={length})")
|
|
|
|
|
|
+ if len(value) < length if inclusive else len(value) <= length:
|
|
|
|
+ raise argument_exception(
|
|
|
|
+ "Недостаточная длина аргумента ({len(value)}, min={length})"
|
|
|
|
+ )
|
|
|
|
|
|
return True
|
|
return True
|
|
|
|
|
|
- def check_length_bound(self, value, min_length: int, max_length: int, inclusive_min=True, inclusive_max=True):
|
|
|
|
|
|
+ def check_length_bound(
|
|
|
|
+ self,
|
|
|
|
+ value,
|
|
|
|
+ min_length: int,
|
|
|
|
+ max_length: int,
|
|
|
|
+ inclusive_min=True,
|
|
|
|
+ inclusive_max=True,
|
|
|
|
+ ):
|
|
"""
|
|
"""
|
|
Валидация аргумента по минимальной и максимальной длинам
|
|
Валидация аргумента по минимальной и максимальной длинам
|
|
Args:
|
|
Args:
|
|
@@ -158,6 +174,23 @@ class validator:
|
|
|
|
|
|
return True
|
|
return True
|
|
|
|
|
|
|
|
+ def check_value_in(self, value, expected: list):
|
|
|
|
+ """
|
|
|
|
+ Валидация аргумента по принадлежности списку
|
|
|
|
+ Args:
|
|
|
|
+ value (any): Передаваемый аргумент
|
|
|
|
+ expected (list): Список допустимых значений
|
|
|
|
+ Raises:
|
|
|
|
+ argument_exception: Значение аргумента не принадлежит списку допустимых
|
|
|
|
+ """
|
|
|
|
+
|
|
|
|
+ if value not in expected:
|
|
|
|
+ raise argument_exception(
|
|
|
|
+ f"Значение аргумента не принадлежит списку допустимых ({value} not in {"".join(expected)})"
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+ return True
|
|
|
|
+
|
|
def check_regex(self, value, expression: str):
|
|
def check_regex(self, value, expression: str):
|
|
"""
|
|
"""
|
|
Валидация аргумента по регулярному выражению
|
|
Валидация аргумента по регулярному выражению
|
|
@@ -169,7 +202,9 @@ class validator:
|
|
"""
|
|
"""
|
|
|
|
|
|
if re.match(expression, value) is None:
|
|
if re.match(expression, value) is None:
|
|
- raise argument_exception(f"Аргумент не соответствует регулярному выражению ({str(value)}, regex: {expression})")
|
|
|
|
|
|
+ raise argument_exception(
|
|
|
|
+ f"Аргумент не соответствует регулярному выражению ({str(value)}, regex: {expression})"
|
|
|
|
+ )
|
|
|
|
|
|
return True
|
|
return True
|
|
|
|
|
|
@@ -183,10 +218,12 @@ class validator:
|
|
"""
|
|
"""
|
|
|
|
|
|
if not isinstance(value, Number):
|
|
if not isinstance(value, Number):
|
|
- raise argument_exception(f"Аргумент не является числом (type={type(value)})")
|
|
|
|
-
|
|
|
|
|
|
+ raise argument_exception(
|
|
|
|
+ f"Аргумент не является числом (type={type(value)})"
|
|
|
|
+ )
|
|
|
|
+
|
|
return True
|
|
return True
|
|
-
|
|
|
|
|
|
+
|
|
def check_collection_all(self, value, method):
|
|
def check_collection_all(self, value, method):
|
|
"""
|
|
"""
|
|
Валидация коллекции по соответствию лямбде
|
|
Валидация коллекции по соответствию лямбде
|
|
@@ -204,9 +241,9 @@ class validator:
|
|
except argument_exception as exc:
|
|
except argument_exception as exc:
|
|
raise argument_exception(f"Один из членов коллекции не прошел валидацию \
|
|
raise argument_exception(f"Один из членов коллекции не прошел валидацию \
|
|
({exc.error.error_text} on item #{num})")
|
|
({exc.error.error_text} on item #{num})")
|
|
-
|
|
|
|
|
|
+
|
|
return True
|
|
return True
|
|
-
|
|
|
|
|
|
+
|
|
def check_collection_types_all(self, value, exp_type):
|
|
def check_collection_types_all(self, value, exp_type):
|
|
"""
|
|
"""
|
|
Валидация коллекции по соответствию типа
|
|
Валидация коллекции по соответствию типа
|
|
@@ -223,5 +260,5 @@ class validator:
|
|
except argument_exception:
|
|
except argument_exception:
|
|
raise argument_exception(f"Один из членов коллекции не соответствует типу {exp_type} \
|
|
raise argument_exception(f"Один из членов коллекции не соответствует типу {exp_type} \
|
|
(excepted {exp_type}, got {type(val)} on element #{num})")
|
|
(excepted {exp_type}, got {type(val)} on element #{num})")
|
|
-
|
|
|
|
|
|
+
|
|
return True
|
|
return True
|