-
Notifications
You must be signed in to change notification settings - Fork 163
PR: Use fastjsonschema if installed and add tests #191
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
41bb87d
Use fastjsonschema if installed and tests.
goanpeca c235609
Refactor code into a common validator
goanpeca 4c708b6
Split classes and add reading the current validator from environment …
goanpeca 49da28f
Add review comments.
goanpeca eb74cf7
Update docs, readme and changelog and CI for tests
goanpeca 99603b1
Fix extras
goanpeca e977d22
Add fastjsonshcema to test deps and update CI
goanpeca 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 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
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
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,82 @@ | ||
| # Copyright (c) Jupyter Development Team. | ||
| # Distributed under the terms of the Modified BSD License. | ||
| """ | ||
| Common validator wrapper to provide a uniform usage of other schema validation | ||
| libraries. | ||
| """ | ||
|
|
||
| import os | ||
|
|
||
| import jsonschema | ||
| from jsonschema import Draft4Validator as _JsonSchemaValidator | ||
| from jsonschema import ValidationError | ||
|
|
||
| try: | ||
| import fastjsonschema | ||
| from fastjsonschema import JsonSchemaException as _JsonSchemaException | ||
| except ImportError: | ||
| fastjsonschema = None | ||
| _JsonSchemaException = ValidationError | ||
|
|
||
|
|
||
| class JsonSchemaValidator: | ||
| name = "jsonschema" | ||
|
|
||
| def __init__(self, schema): | ||
| self._schema = schema | ||
| self._default_validator = _JsonSchemaValidator(schema) # Default | ||
| self._validator = self._default_validator | ||
|
|
||
| def validate(self, data): | ||
| self._default_validator.validate(data) | ||
|
|
||
| def iter_errors(self, data, schema=None): | ||
| return self._default_validator.iter_errors(data, schema) | ||
|
|
||
|
|
||
| class FastJsonSchemaValidator(JsonSchemaValidator): | ||
| name = "fastjsonschema" | ||
|
|
||
| def __init__(self, schema): | ||
| self._validator = fastjsonschema.compile(schema) | ||
|
|
||
| def validate(self, data): | ||
| try: | ||
| self._validator(data) | ||
| except _JsonSchemaException as error: | ||
| raise ValidationError(error.message, schema_path=error.path) | ||
|
|
||
| def iter_errors(self, data, schema=None): | ||
| errors = [] | ||
| validate_func = self._validator if schema is None else fastjsonschema.compile(schema) | ||
| try: | ||
| validate_func(data) | ||
| except _JsonSchemaException as error: | ||
| errors = [ValidationError(error.message, schema_path=error.path)] | ||
|
|
||
| return errors | ||
|
|
||
|
|
||
| _VALIDATOR_MAP = [ | ||
| ("fastjsonschema", fastjsonschema, FastJsonSchemaValidator), | ||
| ("jsonschema", jsonschema, JsonSchemaValidator), | ||
| ] | ||
| VALIDATORS = [item[0] for item in _VALIDATOR_MAP] | ||
|
|
||
|
|
||
| def _validator_for_name(validator_name): | ||
| if validator_name not in VALIDATORS: | ||
| raise ValueError("Invalid validator '{0}' value!\nValid values are: {1}".format( | ||
| validator_name, VALIDATORS)) | ||
|
|
||
| for (name, module, validator_cls) in _VALIDATOR_MAP: | ||
| if module and validator_name == name: | ||
| return validator_cls | ||
|
|
||
|
|
||
| def get_current_validator(): | ||
| """ | ||
| Return the default validator based on the value of an environment variable. | ||
| """ | ||
| validator_name = os.environ.get("NBFORMAT_VALIDATOR", "jsonschema") | ||
| return _validator_for_name(validator_name) | ||
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,46 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 1, | ||
| "metadata": {}, | ||
| "outputs": [ | ||
| { | ||
| "ename": "NameError", | ||
| "evalue": "name 'iAmNotDefined' is not defined", | ||
| "output_type": "error", | ||
| "traceback": [ | ||
| "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | ||
| "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", | ||
| "\u001b[0;32m<ipython-input-22-56e1109ae320>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0miAmNotDefined\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | ||
| "\u001b[0;31mNameError\u001b[0m: name 'iAmNotDefined' is not defined" | ||
| ] | ||
| } | ||
| ], | ||
| "source": [ | ||
| "# Imagine this cell called a function which runs things on a cluster and you have an error" | ||
| ] | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "kernelspec": { | ||
| "display_name": "Python 3", | ||
| "language": "python", | ||
| "name": "python3" | ||
| }, | ||
| "language_info": { | ||
| "codemirror_mode": { | ||
| "name": "ipython", | ||
| "version": 3 | ||
| }, | ||
| "file_extension": ".py", | ||
| "mimetype": "text/x-python", | ||
| "name": "python", | ||
| "nbconvert_exporter": "python", | ||
| "pygments_lexer": "ipython3", | ||
| "version": "3.8.5" | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 4 | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.