This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
506 additions
and
115 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
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
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
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
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,36 @@ | ||
# Copied from https://github.com/DataBiosphere/azul/blob/develop/src/azul/__init__.py | ||
|
||
|
||
class RequirementError(RuntimeError): | ||
""" | ||
Unlike assertions, unsatisfied requirements do not constitute a bug in the program. | ||
""" | ||
|
||
|
||
def require(condition: bool, *args, exception: type = RequirementError): | ||
""" | ||
Raise a RequirementError, or an instance of the given exception class, if the given condition is False. | ||
:param condition: the boolean condition to be required | ||
:param args: optional positional arguments to be passed to the exception constructor. Typically only one such | ||
argument should be provided: a string containing a textual description of the requirement. | ||
:param exception: a custom exception class to be instantiated and raised if the condition does not hold | ||
""" | ||
reject(not condition, *args, exception=exception) | ||
|
||
|
||
def reject(condition: bool, *args, exception: type = RequirementError): | ||
""" | ||
Raise a RequirementError, or an instance of the given exception class, if the given condition is True. | ||
:param condition: the boolean condition to be rejected | ||
:param args: optional positional arguments to be passed to the exception constructor. Typically only one such | ||
argument should be provided: a string containing a textual description of the rejected condition. | ||
:param exception: a custom exception class to be instantiated and raised if the condition occurs | ||
""" | ||
if condition: | ||
raise exception(*args) |
42 changes: 42 additions & 0 deletions
42
src/humancellatlas/data/metadata/helpers/schema_validation.py
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,42 @@ | ||
from functools import ( | ||
lru_cache, | ||
) | ||
import json | ||
import logging | ||
|
||
from jsonschema import ( | ||
FormatChecker, | ||
ValidationError, | ||
validate, | ||
) | ||
import requests | ||
|
||
from humancellatlas.data.metadata.api import ( | ||
JSON, | ||
) | ||
from humancellatlas.data.metadata.helpers.exception import ( | ||
RequirementError, | ||
) | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class SchemaValidator: | ||
|
||
def validate_json(self, file_json: JSON, file_name: str): | ||
try: | ||
schema = self._download_schema(file_json['describedBy']) | ||
except json.decoder.JSONDecodeError as e: | ||
schema_url = file_json['describedBy'] | ||
raise RequirementError('Failed to parse schema JSON', | ||
file_name, schema_url) from e | ||
try: | ||
validate(file_json, schema, format_checker=FormatChecker()) | ||
except ValidationError as e: | ||
raise RequirementError(*e.args, file_name) from e | ||
|
||
@lru_cache(maxsize=None) | ||
def _download_schema(self, schema_url: str) -> JSON: | ||
response = requests.get(schema_url, allow_redirects=False) | ||
response.raise_for_status() | ||
return response.json() |
Oops, something went wrong.