Skip to content

Instantly share code, notes, and snippets.

@meddulla
Last active February 28, 2019 18:37
Show Gist options
  • Select an option

  • Save meddulla/48a7bcc225ca9c221d54e1d099bb2de7 to your computer and use it in GitHub Desktop.

Select an option

Save meddulla/48a7bcc225ca9c221d54e1d099bb2de7 to your computer and use it in GitHub Desktop.
jsonschema issue
import json
from jsonschema import ValidationError, validate
from jsonschema import Draft4Validator, Draft6Validator, Draft7Validator
schema = {
"type" : "object",
"required": ["name"],
"properties" : {
"name" : {
"type" : "string"
},
"nr_periods" : {
"type": "integer",
"maximum": 2147483647,
"minimum": -2147483648
},
"initial_date" : {
"type": "string",
"format": "date",
"nullable": True,
#"default": None,
},
},
}
schema_wrapper = {
# "openapi": "3.0.0",
"schemas": schema
}
def test_validate(data, schema, **kwargs):
print()
try:
json_data = json.loads(data)
validate(json_data, schema, **kwargs)
print("Valid. Data: %s" % data)
except ValidationError as e:
print("Invalid. Data %s. Exc: %s" % (data, str(e)))
data = """
{
"name": "a name",
"nr_periods": 1
}
"""
test_validate(data, schema) # OK
test_validate(data, schema_wrapper) # OK
data = """
{
"name": "a name",
"nr_periods": 1,
"initial_date": null
}
"""
test_validate(data, schema) # Expected valid but it's invalid
test_validate(data, schema_wrapper) # OK
Valid. Data:
{
"name": "a name",
"nr_periods": 1
}
Valid. Data:
{
"name": "a name",
"nr_periods": 1
}
Invalid. Data
{
"name": "a name",
"nr_periods": 1,
"initial_date": null
}
. Exc: None is not of type 'string'
Failed validating 'type' in schema['properties']['initial_date']:
{'format': 'date', 'nullable': True, 'type': 'string'}
On instance['initial_date']:
None
Valid. Data:
{
"name": "a name",
"nr_periods": 1,
"initial_date": null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment