-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Opportunistically use refferencing library
- Loading branch information
1 parent
fb2794c
commit 7c68e35
Showing
4 changed files
with
98 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
"""Compatibility layer for jsonschema validation.""" | ||
|
||
from __future__ import annotations | ||
|
||
import warnings | ||
from typing import Any, Dict, List, NamedTuple, Optional, Union | ||
|
||
import jsonschema | ||
|
||
try: | ||
from jsonschema import validators | ||
from referencing import Registry, create | ||
from referencing.jsonschema import DRAFT4 | ||
MODERN_JSONSCHEMA = True | ||
except ImportError: | ||
MODERN_JSONSCHEMA = False | ||
|
||
|
||
class JsonSchemaValidationResult(NamedTuple): | ||
"""Represents JSON Schema validation result.""" | ||
result: bool | ||
errors: List[Any] | ||
|
||
|
||
def create_validator( | ||
schema: Dict[str, Any], | ||
schema_store: Optional[Dict[str, Any]] = None | ||
) -> Union[jsonschema.validators.Validator, jsonschema.validators.Draft4Validator]: | ||
"""Create a validator instance based on available jsonschema version.""" | ||
schema_store = schema_store or {} | ||
|
||
if MODERN_JSONSCHEMA: | ||
# Modern approach with referencing | ||
registry: Registry = Registry().with_resources([ | ||
(uri, create({'$schema': 'http://json-schema.org/draft-04/schema#', **contents})) | ||
for uri, contents in schema_store.items() | ||
]) | ||
cls = validators.create(meta_schema=DRAFT4, validators={}) | ||
return cls(schema=schema, registry=registry) | ||
else: | ||
# Legacy approach with RefResolver | ||
with warnings.catch_warnings(): | ||
warnings.filterwarnings('ignore', category=DeprecationWarning) | ||
try: | ||
resolver = jsonschema.RefResolver.from_schema( | ||
schema, store=schema_store) | ||
except AttributeError as error: | ||
from fmf.utils import JsonSchemaError | ||
raise JsonSchemaError(f'Provided schema cannot be loaded: {error}') | ||
return jsonschema.Draft4Validator(schema, resolver=resolver) | ||
|
||
|
||
def validate_data( | ||
data: Any, | ||
schema: Dict[str, Any], | ||
schema_store: Optional[Dict[str, Any]] = None | ||
) -> JsonSchemaValidationResult: | ||
""" | ||
Validate data with given JSON Schema and schema references. | ||
Args: | ||
data: The data to validate | ||
schema: The JSON Schema to validate against | ||
schema_store: Optional dict of schema references and their content | ||
Returns: | ||
JsonSchemaValidationResult with: | ||
result: boolean representing the validation result | ||
errors: A list of validation errors | ||
Raises: | ||
JsonSchemaError: If the supplied schema was invalid | ||
""" | ||
validator = create_validator(schema, schema_store) | ||
|
||
try: | ||
validator.validate(data) | ||
return JsonSchemaValidationResult(True, []) | ||
|
||
# Data file validated by schema contains errors | ||
except jsonschema.exceptions.ValidationError: | ||
return JsonSchemaValidationResult(False, list(validator.iter_errors(data))) | ||
|
||
# Schema file is invalid | ||
except ( | ||
jsonschema.exceptions.SchemaError, | ||
jsonschema.exceptions.RefResolutionError, | ||
jsonschema.exceptions.UnknownType | ||
) as error: | ||
from fmf.utils import JsonSchemaError | ||
raise JsonSchemaError(f'Errors found in provided schema: {error}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters