Skip to content

Fix cli bug about getting arguments["validator"]. #725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions jsonschema/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down
44 changes: 43 additions & 1 deletion jsonschema/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):

Expand Down Expand Up @@ -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):
Expand Down