diff --git a/jsonschema/cli.py b/jsonschema/cli.py index 4c2bb4f20..713193979 100644 --- a/jsonschema/cli.py +++ b/jsonschema/cli.py @@ -191,8 +191,6 @@ def _namedAnyWithDefault(name): def parse_args(args): arguments = vars(parser.parse_args(args=args or ["--help"])) - if arguments["validator"] is None: - arguments["validator"] = validator_for(arguments["schema"]) if arguments["output"] != "plain" and arguments["error_format"]: raise parser.error( "--error-format can only be used with --output plain" @@ -229,6 +227,9 @@ def run(arguments, stdout=sys.stdout, stderr=sys.stderr, stdin=sys.stdin): except _CannotLoadFile: return 1 + if arguments["validator"] is None: + arguments["validator"] = validator_for(schema) + try: arguments["validator"].check_schema(schema) except SchemaError as error: diff --git a/jsonschema/tests/test_cli.py b/jsonschema/tests/test_cli.py index 7ca32fc73..d0ca811d3 100644 --- a/jsonschema/tests/test_cli.py +++ b/jsonschema/tests/test_cli.py @@ -11,7 +11,7 @@ from jsonschema import Draft4Validator, __version__, cli from jsonschema.exceptions import SchemaError, ValidationError from jsonschema.tests._helpers import captured_output -from jsonschema.validators import _LATEST_VERSION, validate +from jsonschema.validators import _LATEST_VERSION, validate, validator_for def fake_validator(*errors): @@ -691,6 +691,47 @@ def test_real_validator(self): stderr="", ) + def test_draft07_check_const(self): + schema = """ + { + "$schema": "http://json-schema.org/draft-07/schema#", + "properties": { + "value": { + "type": "string", + "const": "check" + } + } + } + """ + instance = """{"value": "2"}""" + self.assertOutputs( + files=dict(some_schema=schema, some_instance=instance), + argv=["-i", "some_instance", "some_schema"], + exit_code=1, + stdout="", + stderr="2: 'check' was expected\n", + ) + + def test_draft04_not_check_const(self): + schema = """ + { + "$schema": "http://json-schema.org/draft-04/schema#", + "properties": { + "value": { + "type": "string", + "const": "check" + } + } + } + """ + instance = """{"value": "2"}""" + self.assertOutputs( + files=dict(some_schema=schema, some_instance=instance), + argv=["-i", "some_instance", "some_schema"], + stdout="", + stderr="", + ) + class TestParser(TestCase): @@ -724,6 +765,7 @@ def test_latest_validator_is_the_default(self): "mem://some/schema", ] ) + arguments["validator"] = validator_for(arguments["schema"]) self.assertIs(arguments["validator"], _LATEST_VERSION) def test_unknown_output(self):