-
Notifications
You must be signed in to change notification settings - Fork 20
Use fastjsonschema for json schema validation #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
kiendang
wants to merge
12
commits into
jupyter:master
Choose a base branch
from
kiendang:fastjsonschema
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ca3dd3e
Add fastjsonschema for json schema validation
kiendang e5eba7c
Use both jsonschema and fastjsonschema for all tests
kiendang fe4fa2a
Flake8
kiendang 6f0b187
Lower case json_validator
kiendang 4d2632a
Simplify JSON schema errors
kiendang 1e0281b
Merge branch 'master' into fastjsonschema
kiendang 294ad47
Remove redundant test arg
kiendang 3c969af
Improve tests
kiendang d480d80
Add both jsonschema and fastjsonschema to tests
kiendang b412a38
Unexpose categories.py entirely
kiendang d65bf7a
Restrict json schema error interface
kiendang f7c62f7
Remove inaccurate type hint
kiendang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,75 @@ | ||
import jsonschema | ||
import fastjsonschema | ||
|
||
|
||
class JSONSchemaError(Exception): | ||
pass | ||
|
||
|
||
class JSONValidationError(Exception): | ||
pass | ||
|
||
|
||
class EventSchemaValidator: | ||
def __init__(self, schema): | ||
pass | ||
|
||
def validate(self, instance): | ||
pass | ||
|
||
|
||
class JSONSchemaValidator(EventSchemaValidator): | ||
def __init__(self, schema): | ||
ivalidator = jsonschema.validators.validator_for(schema) | ||
|
||
try: | ||
ivalidator.check_schema(schema) | ||
except jsonschema.SchemaError as e: | ||
raise(JSONSchemaError(e.message)) | ||
except Exception as e: | ||
raise JSONSchemaError(str(e)) | ||
|
||
self._validator = ivalidator(schema) | ||
|
||
def validate(self, instance): | ||
try: | ||
self._validator.validate(instance) | ||
except jsonschema.ValidationError as e: | ||
raise(JSONValidationError(e.message)) | ||
except Exception as e: | ||
raise JSONValidationError(str(e)) | ||
|
||
|
||
class FastJSONSchemaValidator(EventSchemaValidator): | ||
def __init__(self, schema): | ||
try: | ||
self._validate = fastjsonschema.compile(schema) | ||
except fastjsonschema.JsonSchemaDefinitionException as e: | ||
raise JSONSchemaError(e.message) | ||
except Exception as e: | ||
raise JSONSchemaError(str(e)) | ||
|
||
def validate(self, instance): | ||
try: | ||
self._validate(instance) | ||
except fastjsonschema.JsonSchemaValueException as e: | ||
raise JSONValidationError(e.message) | ||
except Exception as e: | ||
raise JSONValidationError(str(e)) | ||
|
||
|
||
JSON_SCHEMA_VALIDATORS = { | ||
'jsonschema': JSONSchemaValidator, | ||
'fastjsonschema': FastJSONSchemaValidator | ||
} | ||
|
||
DEFAULT_SCHEMA_VALIDATOR = FastJSONSchemaValidator | ||
|
||
|
||
class EventSchema: | ||
def __init__(self, schema, validator_cls=DEFAULT_SCHEMA_VALIDATOR): | ||
self.schema = schema | ||
self._validator = validator_cls(schema) | ||
|
||
def validate(self, instance): | ||
self._validator.validate(instance) |
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -43,6 +43,7 @@ | |
'jsonschema', | ||
'python-json-logger', | ||
'traitlets', | ||
'ruamel.yaml' | ||
'ruamel.yaml', | ||
'fastjsonschema' | ||
], | ||
) |
This file contains hidden or 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,6 @@ | ||
from jupyter_telemetry.eventschema import JSON_SCHEMA_VALIDATORS | ||
|
||
|
||
def pytest_generate_tests(metafunc): | ||
if "json_validator" in metafunc.fixturenames: | ||
metafunc.parametrize("json_validator", JSON_SCHEMA_VALIDATORS.keys()) |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Zsailer Should we allow users to subclass here? They might want to customize the default validator or use their own. Or we can compromise and allow them to past a class name but restricted to only
JSONSchemaValidator
andFastJSONSchemaValidator
and later can consider passing any class if someone asks about that use case. Is there any downside to allowing subclassing?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. I don’t see any downside to allowing custom validators by subclassing
EventSchemaValidator
. We can make this trait aType(default_value='FastJSONSchemaValidator', klass='EventSchemaValidator')
.