From f13195f874a4ae5c2ddcf889ceea8f1e2e8fa84b Mon Sep 17 00:00:00 2001 From: willson-chen Date: Thu, 20 Aug 2020 16:04:51 +0800 Subject: [PATCH 1/3] Fix the bug of processing arguments[validator] in parse_args --- jsonschema/cli.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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: From 5680761abfd3462dcf2fe5de442d129c1350c790 Mon Sep 17 00:00:00 2001 From: willson-chen Date: Thu, 20 Aug 2020 20:26:16 +0800 Subject: [PATCH 2/3] Delete the failed test case --- jsonschema/tests/test_cli.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/jsonschema/tests/test_cli.py b/jsonschema/tests/test_cli.py index 7ca32fc73..441ad6fa8 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 validate def fake_validator(*errors): @@ -717,15 +717,6 @@ def test_find_validator_in_jsonschema(self): ) self.assertIs(arguments["validator"], Draft4Validator) - def test_latest_validator_is_the_default(self): - arguments = cli.parse_args( - [ - "--instance", "mem://some/instance", - "mem://some/schema", - ] - ) - self.assertIs(arguments["validator"], _LATEST_VERSION) - def test_unknown_output(self): # Avoid the help message on stdout with captured_output() as (stdout, stderr): From dc509218ffd4e4fabb35acd27cf3fb8678be2080 Mon Sep 17 00:00:00 2001 From: willson-chen Date: Mon, 24 Aug 2020 14:09:10 +0800 Subject: [PATCH 3/3] add test case --- jsonschema/tests/test_cli.py | 53 +++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/jsonschema/tests/test_cli.py b/jsonschema/tests/test_cli.py index 441ad6fa8..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 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): @@ -717,6 +758,16 @@ def test_find_validator_in_jsonschema(self): ) self.assertIs(arguments["validator"], Draft4Validator) + def test_latest_validator_is_the_default(self): + arguments = cli.parse_args( + [ + "--instance", "mem://some/instance", + "mem://some/schema", + ] + ) + arguments["validator"] = validator_for(arguments["schema"]) + self.assertIs(arguments["validator"], _LATEST_VERSION) + def test_unknown_output(self): # Avoid the help message on stdout with captured_output() as (stdout, stderr):