validator.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.
  59. """
  60. def _check_all(self) -> None:
  61. # Could not being blank
  62. if len(self.target.strip()) == 0:
  63. return self._failed()
  64. # Must be trimed
  65. if len(self.target.strip()) != len(self.target):
  66. return self._failed()
  67. class description_validator(validator):
  68. """
  69. This is validator for product description. It could be blank, but can not
  70. have white chars before and after content (must be trimmed), and coud has
  71. only 500 chars.
  72. """
  73. def _check_all(self) -> None:
  74. # Could be blank
  75. if len(self.target) == 0:
  76. return
  77. # Must be trimmed
  78. if len(self.target.strip()) != len(self.target):
  79. return self._failed()
  80. # Must has max 500 characters.
  81. if len(self.target) > 500:
  82. return self._failed()
  83. class barcode_validator(validator):
  84. """
  85. This is barcode validator. Barcode must be string, which contain number
  86. with 8, 12 or 13 digits. That is standard EAN.
  87. """
  88. def _check_all(self) -> None:
  89. # Must has only digits
  90. if not self.target.isdigit():
  91. return self._failed()
  92. lenght = len(self.target)
  93. # Must has 8, 12 or 13 chars
  94. if lenght != 8 and lenght != 12 and lenght != 13:
  95. return self._failed()