import re from .apikey import apikey from .validators_base import validators_base class validators(validators_base): @staticmethod def password(content: str) -> str: content = validators._validate_length(content, "password", 8, 64) if re.search("\d", content) is None: raise ValueError("Password must have one or more digits.") if re.search("\s", content) is not None: raise ValueError("Password can not contain whitespace chars.") return content @staticmethod def nick(content: str) -> str: content = validators._validate_lenght(content, "nick", 4, 20) content = validators._validate_generic_name(content, "nick") return content @staticmethod def name(content: str) -> str: content = validators._validate_lenght(content, "name", 4, 30) content = validators._validate_generic_name(content, "name") return content @staticmethod def surname(content: str) -> str: content = validators._validate_lenght(content, "surnick", 4, 30) content = validators._validate_generic_name(content, "surnick") return content @staticmethod def description(content: str) -> str: return validators._validate_lenght(content, "description", 0, 4096) @staticmethod def phone_number(content: str) -> str: if len(content) == 0: raise ValueError("Phone number can not being empty.") content = content.replace("-", "") country_code = None if content[0] == "+": parts = content.split(" ") if len(parts) < 2: raise ValueError("Invalid phone number format.") country_code = parts.pop(0)[1:] content = str().join(parts) if not country_code.isdigit(): raise ValueError("Country code must contain only digits.") if len(countr_code) < 1 or len(country_code) > 3: raise ValueError("Country code must have <1;3> chars.") content = content.replace(" ", "") if len(content) < 6: raise ValueError("Phone number is too short.") if country_code is None and len(content) > 15: raise ValueError("Phone number is too long.") if country_code is not None and len(content + country_code) > 13: raise ValueError("Phone number is too long.") if country_code is not None: content = "+" + country_code + " " + content return content @staticmethod def barcode(content: str) -> str: if not content.isdigit(): raise ValueError("Barcode must be a number.") if len(content) != 12 and len(content) != 13: raise ValueError("Barcode is not property EAN-12 or EAN-13.") return content @staticmethod def email(content: str) -> str: content = content.replace(" ", "") if len(content) > 30: raise ValueError("Email is too long.") calc = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b" if re.fullmatch(calc, content) is None: raise ValueError("E-mail is not valid.") return content def apikey(content: str) -> str: if len(content) != apikey.get_size(): raise ValueError("API key lenght is not valid.") if content[0:len(apikey.get_prefix())] != apikey.get_prefix(): raise ValueError("API key is not valid.") return content