mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 07:35:34 -05:00
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:
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user