I'm looking to validate the datetime type with custom validator, rather than the built in one.
The code looks like this:
schema_text = """
run_date:
type: datetime
required: true
"""
s.schema = yaml.load(schema_text)
s.validate(yaml.load("run_date: 2017-01-01T00:00+00:00:00"))
I could do this using checks_with: my_custom_validator
, which would be ok but I'm hoping to open these schemas up to the public, so asking them all to contribute to them would be a bother. I think this could also be done using a normalizer but, again, I'd prefer not to munge with the input.
Any suggestions here? The dateutil
parser is exactly what I want to use.
As your input data is a string which represents a datetime
in ISO 8601 format, you can use two approaches without any customization.
Either (try to) convert the string to a datetime.datetime
object:
from datetime import datetime
schema = {
"run_date": {"coerce": datetime.fromisoformat}
}
This would need to be validated with normalization and either cause an error or cast the run_date
field's value into a datetime.datetime
object.
If you want to stick with a string as data type, use the regex
rule:
schema = {
"run_date": {"type": "string", "regex": r"\d{4}-\d\d-\d\d-etc.pp."}
}