From 27c06bc00b796bbf9f93f1278896d0a9fdb0b9ef Mon Sep 17 00:00:00 2001 From: Romain Taprest Date: Thu, 9 Apr 2020 09:32:37 +0200 Subject: [PATCH] [CLI] Add new option -r to resolve local file references --- jsonschema/cli.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/jsonschema/cli.py b/jsonschema/cli.py index abea97666..6a23464ed 100644 --- a/jsonschema/cli.py +++ b/jsonschema/cli.py @@ -8,6 +8,7 @@ import errno import json import sys +import os import traceback import attr @@ -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): @@ -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", @@ -252,7 +262,14 @@ def load(_): raise _CannotLoadFile() instances = [""] - 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: