add more friendly datetime validator to jsonschema

add `datetime` format (note, not the built-in `date-time`) to our json
schemas. this uses the iso8601 library to try and parse the string.

also, move `strict-rfc3339` and `rfc3987` (used by jsonschema to
validate `date-time` and `uri` formats respectively from test
requirements to regular requirements. if they're not installed,
validation silently succeeds, so validation wouldnt reject anything bad
on prod, only in unit tests.
This commit is contained in:
Leo Hemsted
2020-07-09 12:59:09 +01:00
parent 67f6dcae45
commit 61a5730596
5 changed files with 21 additions and 9 deletions

View File

@@ -54,6 +54,17 @@ def validate_schema_date_with_hour(instance):
return True
@format_checker.checks('datetime', raises=ValidationError)
def validate_schema_datetime(instance):
if isinstance(instance, str):
try:
iso8601.parse_date(instance)
except ParseError:
raise ValidationError("datetime format is invalid. It must be a valid ISO8601 date time format, "
"https://en.wikipedia.org/wiki/ISO_8601")
return True
def validate(json_to_validate, schema):
validator = Draft7Validator(schema, format_checker=format_checker)
errors = list(validator.iter_errors(json_to_validate))