forked from GithubBackups/healthchecks
Handle "required" keyword.
This commit is contained in:
parent
6920439f92
commit
eece7c7551
@ -47,10 +47,15 @@ def validate(obj, schema, obj_name="value"):
|
||||
if not isinstance(obj, dict):
|
||||
raise ValidationError("%s is not an object" % obj_name)
|
||||
|
||||
for key, spec in schema["properties"].items():
|
||||
properties = schema.get("properties", {})
|
||||
for key, spec in properties.items():
|
||||
if key in obj:
|
||||
validate(obj[key], spec, obj_name=key)
|
||||
|
||||
for key in schema.get("required", []):
|
||||
if key not in obj:
|
||||
raise ValidationError("key %s absent in %s" % (key, obj_name))
|
||||
|
||||
if "enum" in schema:
|
||||
if obj not in schema["enum"]:
|
||||
raise ValidationError("%s has unexpected value" % obj_name)
|
||||
|
@ -52,6 +52,13 @@ class JsonSchemaTestCase(TestCase):
|
||||
}
|
||||
})
|
||||
|
||||
def test_it_handles_required_properties(self):
|
||||
with self.assertRaises(ValidationError):
|
||||
validate({"foo": "bar"}, {
|
||||
"type": "object",
|
||||
"required": ["baz"]
|
||||
})
|
||||
|
||||
def test_it_validates_arrays(self):
|
||||
validate(["foo", "bar"], {
|
||||
"type": "array",
|
||||
|
Loading…
x
Reference in New Issue
Block a user