| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- class validator:
- """
- This is base validator class. It implements base validator mechanism,
- and other validators must implements it.
- """
- def __init__(self, target: str) -> None:
- """
- This create new validator, from string.
-
- Parameters:
- target (str): Content to validate
- """
- self.__target = target
- self._failed()
- @property
- def target(self) -> str:
- """ This return content to validate. """
- return self.__target
- def _failed(self) -> None:
- """
- This would mark content as invalid.
- """
- self.__result = False
- def _check_all(self) -> None:
- """
- This would check all marks of the content.
- It must be overwrite by target validator.
- """
- pass
- def _contain(self, *args) -> bool:
- """
- This check that target contain any given phrase or letter.
- Args:
- (str): Phrase to check that content contain it
- Returns:
- (bool): True when any of given phrase is in the target
- """
- for letter in args:
- if self.__target.find(letter) != -1:
- return True
-
- return False
- @property
- def result(self) -> bool:
- """ Validate content, True when valid or False when not. """
- self.__result = True
- self._check_all()
- return self.__result
- @property
- def invalid(self) -> bool:
- """ Validate, and return True when target is invalid. """
-
- return not self.result
- @property
- def valid(self) -> bool:
- """ It validate target, and return True when valid. """
- return self.result
- class name_validator(validator):
- """
- This is validator for product name. It check that it is not blank,
- and it not contain white chars before and after content. It must have
- more or qeual of 4 and less than 40 characters.
- """
- def _check_all(self) -> None:
- # Minimum 4 characters
- if len(self.target) < 4:
- return self._failed()
- # Maximum 40 characters
- if len(self.target) > 40:
- return self._failed()
- # Must be trimed
- if len(self.target.strip()) != len(self.target):
- return self._failed()
- class description_validator(validator):
- """
- This is validator for product description. It could be blank, but can not
- have white chars before and after content (must be trimmed), and coud has
- only 500 chars.
- """
- def _check_all(self) -> None:
- # Could be blank
- if len(self.target) == 0:
- return
- # Must be trimmed
- if len(self.target.strip()) != len(self.target):
- return self._failed()
- # Must has max 500 characters.
- if len(self.target) > 500:
- return self._failed()
- class barcode_validator(validator):
- """
- This is barcode validator. Barcode must be string, which contain number
- with 8, 12 or 13 digits. That is standard EAN.
- """
- def _check_all(self) -> None:
- # Must has only digits
- if not self.target.isdigit():
- return self._failed()
- lenght = len(self.target)
- # Must has 8, 12 or 13 chars
- if lenght != 8 and lenght != 12 and lenght != 13:
- return self._failed()
- class author_validator(validator):
- """
- This validate author name. Author can not have less than 3 characters,
- and can not have more than 40 characters. It also must be trimmed.
- """
- def _check_all(self) -> None:
- # Must be trimmed
- if len(self.target) != len(self.target.strip()):
- return self._failed()
- # Must have more than 3 characters
- if len(self.target) < 3:
- return self._failed()
- # Must has max 40 characters
- if len(self.target) > 40:
- return self._failed()
|