validators.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import re
  2. from .apikey import apikey
  3. from .validators_base import validators_base
  4. class validators(validators_base):
  5. @staticmethod
  6. def password(content: str) -> str:
  7. content = validators._validate_length(content, "password", 8, 64)
  8. if re.search("\d", content) is None:
  9. raise ValueError("Password must have one or more digits.")
  10. if re.search("\s", content) is not None:
  11. raise ValueError("Password can not contain whitespace chars.")
  12. return content
  13. @staticmethod
  14. def nick(content: str) -> str:
  15. content = validators._validate_lenght(content, "nick", 4, 20)
  16. content = validators._validate_generic_name(content, "nick")
  17. return content
  18. @staticmethod
  19. def name(content: str) -> str:
  20. content = validators._validate_lenght(content, "name", 4, 30)
  21. content = validators._validate_generic_name(content, "name")
  22. return content
  23. @staticmethod
  24. def surname(content: str) -> str:
  25. content = validators._validate_lenght(content, "surnick", 4, 30)
  26. content = validators._validate_generic_name(content, "surnick")
  27. return content
  28. @staticmethod
  29. def description(content: str) -> str:
  30. return validators._validate_lenght(content, "description", 0, 4096)
  31. @staticmethod
  32. def phone_number(content: str) -> str:
  33. if len(content) == 0:
  34. raise ValueError("Phone number can not being empty.")
  35. content = content.replace("-", "")
  36. country_code = None
  37. if content[0] == "+":
  38. parts = content.split(" ")
  39. if len(parts) < 2:
  40. raise ValueError("Invalid phone number format.")
  41. country_code = parts.pop(0)[1:]
  42. content = str().join(parts)
  43. if not country_code.isdigit():
  44. raise ValueError("Country code must contain only digits.")
  45. if len(countr_code) < 1 or len(country_code) > 3:
  46. raise ValueError("Country code must have <1;3> chars.")
  47. content = content.replace(" ", "")
  48. if len(content) < 6:
  49. raise ValueError("Phone number is too short.")
  50. if country_code is None and len(content) > 15:
  51. raise ValueError("Phone number is too long.")
  52. if country_code is not None and len(content + country_code) > 13:
  53. raise ValueError("Phone number is too long.")
  54. if country_code is not None:
  55. content = "+" + country_code + " " + content
  56. return content
  57. @staticmethod
  58. def barcode(content: str) -> str:
  59. if not content.isdigit():
  60. raise ValueError("Barcode must be a number.")
  61. if len(content) != 12 and len(content) != 13:
  62. raise ValueError("Barcode is not property EAN-12 or EAN-13.")
  63. return content
  64. @staticmethod
  65. def email(content: str) -> str:
  66. content = content.replace(" ", "")
  67. if len(content) > 30:
  68. raise ValueError("Email is too long.")
  69. calc = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b"
  70. if re.fullmatch(calc, content) is None:
  71. raise ValueError("E-mail is not valid.")
  72. return content
  73. def apikey(content: str) -> str:
  74. if len(content) != apikey.get_size():
  75. raise ValueError("API key lenght is not valid.")
  76. if content[0:len(apikey.get_prefix())] != apikey.get_prefix():
  77. raise ValueError("API key is not valid.")
  78. return content