Skip to content

Commit 11f7dfa

Browse files
committed
Instrument use of custom --validator-class values
Implement the underlying arg-passing to set the `--validator-class` from CLI arguments when creating a SchemaLoader (the class used when `--schemafile` is used).
1 parent db1bf62 commit 11f7dfa

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/check_jsonschema/cli/main_command.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ def build_schema_loader(args: ParseResult) -> SchemaLoaderBase:
290290
args.cache_filename,
291291
args.disable_cache,
292292
base_uri=args.base_uri,
293+
validator_class=args.validator_class,
293294
)
294295
else:
295296
raise NotImplementedError("no valid schema option provided")

src/check_jsonschema/schema_loader/main.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,22 @@ def get_validator(
5656

5757

5858
class SchemaLoader(SchemaLoaderBase):
59+
validator_class: type[jsonschema.protocols.Validator] | None = None
60+
5961
def __init__(
6062
self,
6163
schemafile: str,
6264
cache_filename: str | None = None,
6365
disable_cache: bool = False,
6466
base_uri: str | None = None,
67+
validator_class: type[jsonschema.protocols.Validator] | None = None,
6568
) -> None:
6669
# record input parameters (these are not to be modified)
6770
self.schemafile = schemafile
6871
self.cache_filename = cache_filename
6972
self.disable_cache = disable_cache
7073
self.base_uri = base_uri
74+
self.validator_class = validator_class
7175

7276
# if the schema location is a URL, which may include a file:// URL, parse it
7377
self.url_info = None
@@ -132,9 +136,19 @@ def get_validator(
132136
self._parsers, retrieval_uri, schema
133137
)
134138

135-
# get the correct validator class and check the schema under its metaschema
136-
validator_cls = jsonschema.validators.validator_for(schema)
137-
validator_cls.check_schema(schema)
139+
if self.validator_class is None:
140+
# get the correct validator class and check the schema under its metaschema
141+
validator_cls = jsonschema.validators.validator_for(schema)
142+
validator_cls.check_schema(schema)
143+
else:
144+
# for a user-provided validator class, don't check_schema
145+
# on the grounds that it might *not* be valid but the user wants to use
146+
# their custom validator anyway
147+
#
148+
# in fact, there's no real guarantee that a user-provided
149+
# validator_class properly conforms to the jsonschema.Validator protocol
150+
# we *hope* that it does, but we can't be fully sure
151+
validator_cls = self.validator_class
138152

139153
# extend the validator class with default-filling behavior if appropriate
140154
if fill_defaults:

0 commit comments

Comments
 (0)