validator.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. class validator:
  2. """
  3. This is base validator class. It implements base validator mechanism,
  4. and other validators must implements it.
  5. """
  6. def __init__(self, target: str) -> None:
  7. """
  8. This create new validator, from string.
  9. Parameters:
  10. target (str): Content to validate
  11. """
  12. self.__target = target
  13. self._failed()
  14. @property
  15. def target(self) -> str:
  16. """ This return content to validate. """
  17. return self.__target
  18. def _failed(self) -> None:
  19. """
  20. This would mark content as invalid.
  21. """
  22. self.__result = False
  23. def _check_all(self) -> None:
  24. """
  25. This would check all marks of the content.
  26. It must be overwrite by target validator.
  27. """
  28. pass
  29. def _contain(self, *args) -> bool:
  30. """
  31. This check that target contain any given phrase or letter.
  32. Args:
  33. (str): Phrase to check that content contain it
  34. Returns:
  35. (bool): True when any of given phrase is in the target
  36. """
  37. for letter in args:
  38. if self.__target.find(letter) != -1:
  39. return True
  40. return False
  41. @property
  42. def result(self) -> bool:
  43. """ Validate content, True when valid or False when not. """
  44. self.__result = True
  45. self._check_all()
  46. return self.__result
  47. @property
  48. def invalid(self) -> bool:
  49. """ Validate, and return True when target is invalid. """
  50. return not self.result
  51. @property
  52. def valid(self) -> bool:
  53. """ It validate target, and return True when valid. """
  54. return self.result
  55. class name_validator(validator):
  56. """
  57. This is validator for product name. It check that it is not blank,
  58. and it not contain white chars before and after content. It must have
  59. more or qeual of 4 and less than 40 characters.
  60. """
  61. def _check_all(self) -> None:
  62. # Minimum 4 characters
  63. if len(self.target) < 4:
  64. return self._failed()
  65. # Maximum 40 characters
  66. if len(self.target) > 40:
  67. return self._failed()
  68. # Must be trimed
  69. if len(self.target.strip()) != len(self.target):
  70. return self._failed()
  71. class description_validator(validator):
  72. """
  73. This is validator for product description. It could be blank, but can not
  74. have white chars before and after content (must be trimmed), and coud has
  75. only 500 chars.
  76. """
  77. def _check_all(self) -> None:
  78. # Could be blank
  79. if len(self.target) == 0:
  80. return
  81. # Must be trimmed
  82. if len(self.target.strip()) != len(self.target):
  83. return self._failed()
  84. # Must has max 500 characters.
  85. if len(self.target) > 500:
  86. return self._failed()
  87. class barcode_validator(validator):
  88. """
  89. This is barcode validator. Barcode must be string, which contain number
  90. with 8, 12 or 13 digits. That is standard EAN.
  91. """
  92. def _check_all(self) -> None:
  93. # Must has only digits
  94. if not self.target.isdigit():
  95. return self._failed()
  96. lenght = len(self.target)
  97. # Must has 8, 12 or 13 chars
  98. if lenght != 8 and lenght != 12 and lenght != 13:
  99. return self._failed()
  100. class author_validator(validator):
  101. """
  102. This validate author name. Author can not have less than 3 characters,
  103. and can not have more than 40 characters. It also must be trimmed.
  104. """
  105. def _check_all(self) -> None:
  106. # Must be trimmed
  107. if len(self.target) != len(self.target.strip()):
  108. return self._failed()
  109. # Must have more than 3 characters
  110. if len(self.target) < 3:
  111. return self._failed()
  112. # Must has max 40 characters
  113. if len(self.target) > 40:
  114. return self._failed()