validators_base.py 999 B

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