Skip to content

Commit 646d99b

Browse files
committed
Add the specified base-uri function in the CLI
1 parent 1db81c2 commit 646d99b

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

jsonschema/cli.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,10 @@ def _namedAnyWithDefault(name):
181181
)
182182
parser.add_argument(
183183
"--base-uri",
184-
action="store_true",
185184
help="""
186185
use this option to indicate that the schema contains some references
187-
to some local files. With this option, the validator will try to
188-
resolve those references as paths relative to the given schema.
186+
to some local or remote files. With this option, the validator will
187+
try to resolve those references as paths relative to the given schema.
189188
""",
190189
)
191190
parser.add_argument(
@@ -261,15 +260,21 @@ def load(_):
261260
raise _CannotLoadFile()
262261
instances = ["<stdin>"]
263262

264-
if arguments["base_uri"]:
265-
file_prefix = "file:///{}/" if "nt" == os.name else "file://{}/"
266-
schema_dirname = os.path.dirname(arguments["schema"])
263+
if arguments["base_uri"] is None:
264+
resolver = None
265+
elif "http:" in arguments["base_uri"] or "https:" in arguments["base_uri"]\
266+
or "urn:" in arguments["base_uri"]:
267267
resolver = RefResolver(
268-
base_uri=file_prefix.format(os.path.abspath(schema_dirname)),
268+
base_uri=arguments["base_uri"],
269269
referrer=schema,
270270
)
271271
else:
272-
resolver = None
272+
file_prefix = "file:///{}/" if "nt" == os.name else "file://{}/"
273+
resolver = RefResolver(
274+
base_uri=file_prefix.format(os.path.abspath(arguments["base_uri"])),
275+
referrer=schema,
276+
)
277+
273278
validator = arguments["validator"](schema, resolver=resolver)
274279
exit_code = 0
275280
for each in instances:

jsonschema/tests/test_cli.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import subprocess
99
import sys
10+
import tempfile
1011

1112
from jsonschema import Draft4Validator, cli, __version__
1213
from jsonschema.exceptions import SchemaError, ValidationError
@@ -684,21 +685,30 @@ def test_successful_validation__of_just_the_schema_pretty_output(self):
684685
)
685686

686687
def test_successful_validation_with_specifying_base_uri(self):
687-
schema = """\
688-
{"type": "object", "properties": {"KEY1":
689-
{"$ref": "schema.json#definitions/schemas"}},
690-
"definitions": {"schemas": {"type": "string"}}}
691-
"""
692-
fp = open("schema.json", "w+")
693-
fp.write(schema)
694-
fp.close()
688+
try:
689+
schema_file = tempfile.NamedTemporaryFile(
690+
mode='w+',
691+
prefix='schema',
692+
suffix='.json',
693+
dir='..',
694+
delete=False
695+
)
696+
self.addCleanup(os.remove, schema_file.name)
697+
schema = """
698+
{"type": "object", "properties": {"KEY1":
699+
{"$ref": %s%s#definitions/schemas"}},
700+
"definitions": {"schemas": {"type": "string"}}}
701+
""" % ("\"", os.path.basename(schema_file.name))
702+
schema_file.write(schema)
703+
finally:
704+
schema_file.close()
705+
695706
self.assertOutputs(
696707
files=dict(some_schema=schema, some_instance='{"KEY1": "1"}'),
697-
argv=["-i", "some_instance", "--base-uri", "some_schema"],
708+
argv=["-i", "some_instance", "--base-uri", "..", "some_schema"],
698709
stdout="",
699710
stderr="",
700711
)
701-
os.remove("schema.json")
702712

703713
def test_real_validator(self):
704714
self.assertOutputs(

0 commit comments

Comments
 (0)