validators.py 3.7 KB

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