Merge remote-tracking branch 'wspeirs/master'
* wspeirs/master: Added unit test for cli Added call to check_schema before checking instance Made required_draft4 more robust when used with Draft3
This commit is contained in:
commit
5e935ad0fe
|
@ -296,6 +296,11 @@ def properties_draft4(validator, properties, instance, schema):
|
|||
def required_draft4(validator, required, instance, schema):
|
||||
if not validator.is_type(instance, "object"):
|
||||
return
|
||||
|
||||
if not isinstance(required, list):
|
||||
yield ValidationError("Found Draft3 style 'required' restriction")
|
||||
return
|
||||
|
||||
for property in required:
|
||||
if property not in instance:
|
||||
yield ValidationError("%r is a required property" % property)
|
||||
|
|
|
@ -65,6 +65,10 @@ def run(arguments, stdout=sys.stdout, stderr=sys.stderr):
|
|||
error_format = arguments["error_format"]
|
||||
validator = arguments["validator"](schema=arguments["schema"])
|
||||
errored = False
|
||||
|
||||
# add a check to the schema file first
|
||||
validator.check_schema(arguments["schema"])
|
||||
|
||||
for instance in arguments["instances"] or ():
|
||||
for error in validator.iter_errors(instance):
|
||||
stderr.write(error_format.format(error=error))
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from jsonschema import Draft4Validator, ValidationError, cli
|
||||
from jsonschema.compat import StringIO
|
||||
from jsonschema.tests.compat import mock, unittest
|
||||
from jsonschema.exceptions import SchemaError
|
||||
|
||||
|
||||
def fake_validator(*errors):
|
||||
|
@ -14,6 +15,10 @@ def fake_validator(*errors):
|
|||
if errors:
|
||||
return errors.pop()
|
||||
return []
|
||||
|
||||
def check_schema(self, schema):
|
||||
return True
|
||||
|
||||
return FakeValidator
|
||||
|
||||
|
||||
|
@ -55,6 +60,26 @@ class TestParser(unittest.TestCase):
|
|||
|
||||
|
||||
class TestCLI(unittest.TestCase):
|
||||
def test_draft3_schema_draft4_validator(self):
|
||||
stdout, stderr = StringIO(), StringIO()
|
||||
with self.assertRaises(SchemaError):
|
||||
exit_code = cli.run(
|
||||
{
|
||||
"validator": Draft4Validator,
|
||||
"schema": {
|
||||
"anyOf": [
|
||||
{"minimum": 20},
|
||||
{"type": "string"},
|
||||
{"required": True}
|
||||
]
|
||||
},
|
||||
"instances": [1],
|
||||
"error_format": "{error.message}",
|
||||
},
|
||||
stdout=stdout,
|
||||
stderr=stderr,
|
||||
)
|
||||
|
||||
def test_successful_validation(self):
|
||||
stdout, stderr = StringIO(), StringIO()
|
||||
exit_code = cli.run(
|
||||
|
|
Loading…
Reference in New Issue