validators_base.py 878 B

12345678910111213141516171819202122232425262728293031323334
  1. import re
  2. class validators_base:
  3. @staticmethod
  4. def _validate_generic_name(content: str, name: str = "it") -> str:
  5. if re.search("\s", content) is not None:
  6. raise ValueError(
  7. name.title() + " can not contain whitespace chars."
  8. )
  9. if re.search("\W", content) is not None:
  10. raise ValueError(
  11. name.title() + " can contain only _ and alphanumeric chars."
  12. )
  13. return content
  14. @staticmethod
  15. def _validate_lenght(
  16. content: str,
  17. name: str,
  18. minimum: int,
  19. maximum: int | None
  20. ) -> str:
  21. if len(content) < minimum:
  22. raise ValueError(name.title() + " is too short.")
  23. if maximum is not None and len(content) > maximum:
  24. raise ValueError(name.title() + " is too long.")
  25. return content