Fix text dialog routing and club card registration recovery
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
47
src/utils/input_validation.py
Normal file
47
src/utils/input_validation.py
Normal file
@@ -0,0 +1,47 @@
|
||||
"""Validation shared by dialog steps and database services."""
|
||||
import re
|
||||
|
||||
|
||||
def club_card(value):
|
||||
value = value.strip() if isinstance(value, str) else ""
|
||||
if not re.fullmatch(r"[0-9]{1,50}", value):
|
||||
raise ValueError("Номер клубной карты должен содержать от 1 до 50 цифр без букв и разделителей.")
|
||||
return value # Leading zeroes are part of the card number.
|
||||
|
||||
|
||||
def phone_number(value):
|
||||
if value is None or value.strip() == "-":
|
||||
return None
|
||||
value = value.strip()
|
||||
if not value or len(value) > 20 or not re.fullmatch(r"\+?[0-9() -]+", value) or not re.search(r"[0-9]", value):
|
||||
raise ValueError("Введите телефон: цифры, при необходимости +, пробелы, скобки или дефисы; не более 20 символов. Для пропуска отправьте '-'.")
|
||||
return value
|
||||
|
||||
|
||||
def numeric_id(value, *, maximum=2**63 - 1, signed=False):
|
||||
value = str(value).strip()
|
||||
pattern = r"-?[0-9]{1,19}" if signed else r"[0-9]{1,19}"
|
||||
if not re.fullmatch(pattern, value) or not 0 < abs(int(value)) <= maximum:
|
||||
raise ValueError("Некорректный ID: требуется число в допустимом диапазоне.")
|
||||
return int(value)
|
||||
|
||||
|
||||
def lottery_field(field, value):
|
||||
if field == "title":
|
||||
if not isinstance(value, str) or not 1 <= len(value.strip()) <= 500:
|
||||
raise ValueError("Название должно содержать от 1 до 500 символов.")
|
||||
return value.strip()
|
||||
if field == "description":
|
||||
if value is None or value == "" or value.strip() == "-":
|
||||
return None
|
||||
if not value.strip() or len(value) > 4096:
|
||||
raise ValueError("Введите описание до 4096 символов или '-' для пропуска.")
|
||||
return value.strip()
|
||||
if field == "prizes":
|
||||
prizes = [line.strip() for line in value.splitlines() if line.strip()] if isinstance(value, str) else value
|
||||
if not isinstance(prizes, list) or not 1 <= len(prizes) <= 100 or any(
|
||||
not isinstance(prize, str) or not 1 <= len(prize.strip()) <= 500 for prize in prizes
|
||||
):
|
||||
raise ValueError("Введите от 1 до 100 призов, каждый с новой строки и не длиннее 500 символов.")
|
||||
return [prize.strip() for prize in prizes]
|
||||
raise ValueError("Неизвестное поле розыгрыша.")
|
||||
Reference in New Issue
Block a user