Skip to content
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

WIP: [CLI] Add new option -r to resolve local file references #680

Closed
Closed
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
21 changes: 19 additions & 2 deletions jsonschema/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import errno
import json
import sys
import os
import traceback

import attr
Expand All @@ -16,7 +17,7 @@
from jsonschema._reflect import namedAny
from jsonschema.compat import JSONDecodeError
from jsonschema.exceptions import SchemaError
from jsonschema.validators import validator_for
from jsonschema.validators import validator_for, RefResolver


class _CannotLoadFile(Exception):
Expand Down Expand Up @@ -179,6 +180,15 @@ def _namedAnyWithDefault(name):
of the class.
""",
)
parser.add_argument(
"-r", "--local-ref",
action="store_true",
help="""
use this option to indicate that the schema contains some references
to some local files. With this option, the validator will try to
resolve those references as paths relative to the given schema.
""",
)
parser.add_argument(
"--version",
action="version",
Expand Down Expand Up @@ -252,7 +262,14 @@ def load(_):
raise _CannotLoadFile()
instances = ["<stdin>"]

validator = arguments["validator"](schema)
if arguments["local_ref"]:
resolver = RefResolver(
base_uri="file:{}".format(os.path.abspath(arguments["schema"])),
referrer=schema,
)
else:
resolver = None
validator = arguments["validator"](schema, resolver=resolver)
exit_code = 0
for each in instances:
try:
Expand Down