validators.py 781 B

1234567891011121314151617181920212223242526272829
  1. # Copyright: (c) OpenSpug Organization. https://github.com/openspug/spug
  2. # Copyright: (c) <spug.dev@gmail.com>
  3. # Released under the AGPL-3.0 License.
  4. import ipaddress
  5. from datetime import datetime
  6. # 判断是否是ip地址
  7. def ip_validator(value):
  8. try:
  9. ipaddress.ip_address(value)
  10. return True
  11. except ValueError:
  12. return False
  13. # 判断是否是日期字符串,支持 2018-04-11 或 2018-04-11 14:55:30
  14. def date_validator(value: str) -> bool:
  15. value = value.strip()
  16. try:
  17. if len(value) == 10:
  18. datetime.strptime(value, '%Y-%m-%d')
  19. return True
  20. elif len(value) == 19:
  21. datetime.strptime(value, '%Y-%m-%d %H:%M:%S')
  22. return True
  23. except ValueError:
  24. pass
  25. return False