model.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import re
  2. from darabonba.exceptions import RequiredArgumentException, ValidateException
  3. from Tea.model import TeaModel
  4. class DaraModel(TeaModel):
  5. _map = None
  6. def validate(self):
  7. pass
  8. def to_map(self):
  9. return self._map
  10. def from_map(self, map=None):
  11. pass
  12. @staticmethod
  13. def validate_required(prop, prop_name):
  14. if prop is None:
  15. raise RequiredArgumentException(prop_name)
  16. @staticmethod
  17. def validate_max_length(prop, prop_name, max_length):
  18. if len(prop) > max_length:
  19. raise ValidateException(f'{prop_name} is exceed max-length: {max_length}')
  20. @staticmethod
  21. def validate_min_length(prop, prop_name, min_length):
  22. if len(prop) < min_length:
  23. raise ValidateException(f'{prop_name} is less than min-length: {min_length}')
  24. @staticmethod
  25. def validate_pattern(prop, prop_name, pattern):
  26. match_obj = re.search(pattern, str(prop), re.M | re.I)
  27. if not match_obj:
  28. raise ValidateException(f'{prop_name} is not match: {pattern}')
  29. @staticmethod
  30. def validate_maximum(num, prop_name, maximum):
  31. if num > maximum:
  32. raise ValidateException(f'{prop_name} is greater than the maximum: {maximum}')
  33. @staticmethod
  34. def validate_minimum(num, prop_name, minimum):
  35. if num < minimum:
  36. raise ValidateException(f'{prop_name} is less than the minimum: {minimum}')
  37. def __str__(self):
  38. s = self.to_map()
  39. if s:
  40. return str(s)
  41. else:
  42. return object.__str__(self)