validators.py 3.9 KB

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