'Python: How to validate against date-time format via CLI with jsonschema
I'm using jsonschema via the CLI and looking to validate a JSON document containing a date/time using "format": "date-time" in the schema. However, when I enter in a value which is not a date/time it seems to pass validation.
Using the jsonschema Python library via the CLI, how can I correctly validate?
JSON schema:
{
"$schema": "http://json-schema.org/draft-04/schema",
"id": "http://example.com/example.json",
"type": "object",
"title": "The root schema",
"description": "The root schema comprises the entire JSON document.",
"required": [
"data"
],
"properties": {
"data": {
"id": "#data",
"type": "array",
"items": {
"id": "#items",
"type": "object",
"required": [
"date"
],
"properties": {
"date": {
"id": "#date",
"type": "string",
"format": "date-time",
"examples": [
"2021-11-08T08:33:19+00:00"
]
}
},
"additionalProperties": false
}
}
},
"additionalProperties": false
}
JSON data:
{
"data": [
{
"date": "x"
}
]
}
Run:
pipenv run jsonschema -i data.json schema.json -V Draft4Validator
No errors are returned with the above run command.
I have the following packages installed in my Pipfile:
jsonschema = {extras = ["format"], version = "*"}
strict-rfc3339 = "*"
rfc3339-validator = "*"
I've setup a test case which correctly validates using the exact same data
Solution 1:[1]
The libraries documentation for draft-04 support states the following...
JSON Schema defines the format property which can be used to check if primitive types (strings, numbers, booleans) conform to well-defined formats. By default, no validation is enforced, but optionally, validation can be enabled by hooking in a format-checking object into an Validator.
https://python-jsonschema.readthedocs.io/en/latest/validate/#validating-formats
This is in line with the draft-04 specification.
Based on the code for the CLI, it doesn't look like there IS an argument which provides the function to specify enabling format validation.
If this is something you need, consider creating a Pull Request with tests or sponsoring the person who develops the project to add it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Relequestual |
