-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
8e5610a
commit 7d9e25c
Showing
1 changed file
with
61 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import json | ||
import pathlib | ||
from jschon import create_catalog, JSON, JSONSchema, URI, LocalSource, RemoteSource | ||
from ravens.schema.decompose_schema import _default_base_uri | ||
|
||
|
||
class RavensValidator: | ||
def __init__(self, schema_base_uri: str = _default_base_uri, schema_url: str = None, local_path_to_schema: pathlib.PosixPath = None): | ||
self.catalog = create_catalog("2020-12") | ||
|
||
if schema_url is None: | ||
schema_url = "/".join([schema_base_uri, "Root.json"]) | ||
|
||
if local_path_to_schema is not None: | ||
self.catalog.add_uri_source(URI(schema_base_uri), LocalSource(local_path_to_schema.parent, suffix="")) | ||
self.schema = JSONSchema.loadf(local_path_to_schema) | ||
else: | ||
self.catalog.add_uri_source(URI(schema_base_uri + "/"), RemoteSource(schema_base_uri)) | ||
self.schema = JSONSchema.loadr(schema_url) | ||
|
||
self.result = None | ||
|
||
def validate(self, data: JSON, print_result: bool = True): | ||
self.result = self.schema.evaluate(data) | ||
|
||
if print_result: | ||
self.print_result() | ||
|
||
def validate_string(self, json_string: str, print_result: bool = True): | ||
self.validate(data=JSON.loads(json_string), print_result=print_result) | ||
|
||
def validate_file(self, file_path: str, print_result: bool = True): | ||
self.validate(data=JSON.loadf(file_path), print_result=print_result) | ||
|
||
def validate_dict(self, data_dict: dict, print_result: bool = True): | ||
self.validate(data=JSON.loads(json.dumps(data_dict)), print_result=print_result) | ||
|
||
def print_result(self): | ||
if self.result is not None: | ||
if self.result.valid: | ||
print(self.result.output("flag")) | ||
else: | ||
print(json.dumps(self.result.output("basic"), indent=2)) | ||
else: | ||
print("No validator results available") | ||
|
||
|
||
if __name__ == "__main__": | ||
import os | ||
import pathlib | ||
|
||
# Online version of Validator, does not work with proxy | ||
validator = RavensValidator() | ||
|
||
# Local version of Validator | ||
schema_path = pathlib.Path(os.getcwd()) / "out/schema/separate/Root.json" | ||
validator = RavensValidator(schema_base_uri=f"file://{os.getcwd()}/out/schema/separate/", local_path_to_schema=schema_path) | ||
|
||
# Validation example | ||
data_dir = pathlib.Path(os.getcwd()) / "examples/schema" | ||
validator.validate_file(data_dir / "AlgorithmProperties.json") |