I have a JSON and a JSON-schema
JSON:
{
"aaa": "4000-02-01 00:00:00"
}
JSON-schema:
{
"$schema": "http://json-schema.org/draft-04/schema",
"type": "object",
"properties": {
"aaa": {
"type": "string",
"format": "date-time"
}
}, "required": ["aaa"]
}
The JSON gets validated by the JSON-schema. However if I change the field aaa
to "bla" the schema does not notice that it is not a date-time any longer.
Did I miss anything in the schema?
I found a workaround by using this library. It checks the content of the field in javascript code:
It is highly likely that the implementation of JSON schema validation that you're using is requiring the
T
separator between the date and time components. This is a staple of the RFC3339 spec and ISO8601 which it is based upon. While both have provisions for omitting theT
, they both make it something that can be done by agreement, rather then a mandatory thing to support. (Go figure.)Also, RFC3339 does require that you include either a time zone offset or a
Z
to indicate UTC. This locks it down to a particular moment in time, rather than a human representation of one in some unknown time zone. Since you have required neither, that's likely while it has failed validation.From the JSON Schema spec:
You can change the source code for the python jsonschema module.
Find the datetime-related code, at
jsonschema/_format.py
funcis_date_time(instance)
. Like this, about line 204 - 225, for version 2.6.0:Comment out the above and paste this, or replace the
_check_drafts
function above with this:For Python's jsonschema library, specify the format checker when calling
validate
:To validate a date-time format, the strict-rfc3339 package should be installed.
See Validating Formats.
Validation with
"format"
is optional. This is partly because schema authors are allowed to completely make up new formats, so expecting all formats to be validated is not reasonable.Your library should (if it is decent) have a way to register custom validators for particular formats. For example, the
tv4
validation library (in JavaScript) has thetv4.addFormat()
method:Once you've done this, then
"format": "date-time"
in the schema should validate dates correctly.