Skip to content

Commit 0955ad0

Browse files
committed
Support --base-uri. and local full path and remote URI
1 parent 26aa142 commit 0955ad0

File tree

2 files changed

+17
-64
lines changed

2 files changed

+17
-64
lines changed

jsonschema/cli.py

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import os
1111
import sys
1212
import traceback
13-
import urllib
1413

1514
import attr
1615

@@ -264,30 +263,17 @@ def load(_):
264263

265264
if arguments["base_uri"] is None:
266265
resolver = None
267-
else:
268-
scheme = urllib.parse.urlsplit(arguments["base_uri"]).scheme
269-
alphabet_list = [chr(ord('a') + i) for i in range(0, 26)]
270-
local_path_flag = True \
271-
if scheme == '' or scheme in alphabet_list else False
272-
273-
if local_path_flag:
274-
file_prefix = "file:///{}/" if "nt" == os.name else "file://{}/"
275-
abs_path_flag = os.path.isabs(arguments["base_uri"])
276-
277-
resolver = RefResolver(
278-
base_uri=file_prefix.format(arguments["base_uri"]),
279-
referrer=schema,
280-
) if abs_path_flag is True else RefResolver(
281-
base_uri=file_prefix.format(
282-
os.path.abspath(arguments["base_uri"])
283-
),
266+
elif arguments["base_uri"] == ".":
267+
file_prefix = "file:///{}/" if "nt" == os.name else "file://{}/"
268+
resolver = RefResolver(
269+
base_uri=file_prefix.format(os.getcwd()),
284270
referrer=schema,
285271
)
286-
else:
287-
resolver = RefResolver(
288-
base_uri=arguments["base_uri"],
289-
referrer=schema,
290-
)
272+
else:
273+
resolver = RefResolver(
274+
base_uri=arguments["base_uri"],
275+
referrer=schema,
276+
)
291277

292278
validator = arguments["validator"](schema, resolver=resolver)
293279
exit_code = 0

jsonschema/tests/test_cli.py

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ def test_successful_validate_with_specifying_base_uri_relative_path(self):
690690
mode='w+',
691691
prefix='schema',
692692
suffix='.json',
693-
dir='..',
693+
dir='.',
694694
delete=False
695695
)
696696
self.addCleanup(os.remove, schema_file.name)
@@ -705,7 +705,7 @@ def test_successful_validate_with_specifying_base_uri_relative_path(self):
705705

706706
self.assertOutputs(
707707
files=dict(some_schema=schema, some_instance='{"KEY1": "1"}'),
708-
argv=["-i", "some_instance", "--base-uri", "..", "some_schema"],
708+
argv=["-i", "some_instance", "--base-uri", ".", "some_schema"],
709709
stdout="",
710710
stderr="",
711711
)
@@ -716,7 +716,7 @@ def test_failure_validate_with_specifying_base_uri_relative_path(self):
716716
mode='w+',
717717
prefix='schema',
718718
suffix='.json',
719-
dir='..',
719+
dir='.',
720720
delete=False
721721
)
722722
self.addCleanup(os.remove, schema_file.name)
@@ -731,7 +731,7 @@ def test_failure_validate_with_specifying_base_uri_relative_path(self):
731731

732732
self.assertOutputs(
733733
files=dict(some_schema=schema, some_instance='{"KEY1": 1}'),
734-
argv=["-i", "some_instance", "--base-uri", "..", "some_schema"],
734+
argv=["-i", "some_instance", "--base-uri", ".", "some_schema"],
735735
exit_code=1,
736736
stdout="",
737737
stderr="1: 1 is not of type 'string'\n",
@@ -757,6 +757,8 @@ def test_successful_validate_with_specifying_base_uri_absolute_path(self):
757757
finally:
758758
schema_file.close()
759759

760+
file_prefix = "file:///{}/" if "nt" == os.name else "file://{}/"
761+
absolute_path = file_prefix.format(absolute_path)
760762
self.assertOutputs(
761763
files=dict(some_schema=schema, some_instance='{"KEY1": "1"}'),
762764
argv=[
@@ -788,6 +790,8 @@ def test_failure_validate_with_specifying_base_uri_absolute_path(self):
788790
finally:
789791
schema_file.close()
790792

793+
file_prefix = "file:///{}/" if "nt" == os.name else "file://{}/"
794+
absolute_path = file_prefix.format(absolute_path)
791795
self.assertOutputs(
792796
files=dict(some_schema=schema, some_instance='{"KEY1": 1}'),
793797
argv=[
@@ -800,43 +804,6 @@ def test_failure_validate_with_specifying_base_uri_absolute_path(self):
800804
stderr="1: 1 is not of type 'string'\n",
801805
)
802806

803-
def test_successful_validate_with_specifying_base_uri_remote_path(self):
804-
schema = """
805-
{"type": "object", "properties": {
806-
"KEY1":{"$ref": "organization.json"}}}
807-
"""
808-
self.assertOutputs(
809-
files=dict(some_schema=schema,
810-
some_instance='{"KEY1": {"name": "remote"}}'
811-
),
812-
argv=[
813-
"-i", "some_instance",
814-
"--base-uri", "https://project-open-data.cio.gov/v1.1/schema/",
815-
"some_schema",
816-
],
817-
stdout="",
818-
stderr="",
819-
)
820-
821-
def test_failure_validate_with_specifying_base_uri_remote_path(self):
822-
schema = """
823-
{"type": "object", "properties": {
824-
"KEY1":{"$ref": "organization.json"}}}
825-
"""
826-
self.assertOutputs(
827-
files=dict(some_schema=schema,
828-
some_instance='{"KEY1": {"fail": "remote"}}'
829-
),
830-
argv=[
831-
"-i", "some_instance",
832-
"--base-uri", "https://project-open-data.cio.gov/v1.1/schema/",
833-
"some_schema",
834-
],
835-
exit_code=1,
836-
stdout="",
837-
stderr="{'fail': 'remote'}: 'name' is a required property\n",
838-
)
839-
840807
def test_it_validates_using_the_latest_validator_when_unspecified(self):
841808
# There isn't a better way now I can think of to ensure that the
842809
# latest version was used, given that the call to validator_for

0 commit comments

Comments
 (0)