CSenshi / Validator

Easy-to-use, Highly Configurable Python Data Validator. Inspired by Laravel Validator
https://pypi.org/project/validator/
MIT License
46 stars 23 forks source link

New Rule: Date #56

Closed CSenshi closed 4 years ago

CSenshi commented 4 years ago

Write rule that validates data to be date. If data is already datetime class return true. For strings check if it's correctly formatted.

Description: "The field under validation must be a valid date string or class"

Please see CONTRIBUTING.md for Contribution details :100:

CSenshi commented 4 years ago

HINT:

if you want you can use dateutil package. It has parser method that can be used to check for valid date formats

>>>from dateutil import parser

>>> parser.parse("25-08-1900")
datetime.datetime(1900, 8, 25, 0, 0)

>>> parser.parse("25-30-1900")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/dateutil/parser/_parser.py", line 1356, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
  File "/usr/lib/python3/dist-packages/dateutil/parser/_parser.py", line 653, in parse
    ret = self._build_naive(res, default)
  File "/usr/lib/python3/dist-packages/dateutil/parser/_parser.py", line 1227, in _build_naive
    naive = default.replace(**repl)
ValueError: month must be in 1..12

Incorrect formats raise ValueError