From 066e15c5726968c727311c316d1db37614d67f05 Mon Sep 17 00:00:00 2001 From: goanpeca Date: Thu, 24 Sep 2020 15:56:06 -0500 Subject: [PATCH] Add review comments. --- nbformat/json_compat.py | 14 +++++++++++--- nbformat/validator.py | 21 +++++---------------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/nbformat/json_compat.py b/nbformat/json_compat.py index 6f458c2b..936d3c87 100644 --- a/nbformat/json_compat.py +++ b/nbformat/json_compat.py @@ -38,8 +38,6 @@ class FastJsonSchemaValidator(JsonSchemaValidator): name = "fastjsonschema" def __init__(self, schema): - super().__init__(schema) - self._validator = fastjsonschema.compile(schema) def validate(self, data): @@ -48,6 +46,16 @@ def validate(self, 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), @@ -68,7 +76,7 @@ def _validator_for_name(validator_name): def get_current_validator(): """ - Return the current validator based on the value of an environment variable. + 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) diff --git a/nbformat/validator.py b/nbformat/validator.py index baf37202..bd44729c 100644 --- a/nbformat/validator.py +++ b/nbformat/validator.py @@ -229,7 +229,7 @@ def better_validation_error(error, version, version_minor): def validate(nbdict=None, ref=None, version=None, version_minor=None, - relax_add_props=False, nbjson=None, use_fast=False): + relax_add_props=False, nbjson=None): """Checks whether the given notebook dict-like object conforms to the relevant notebook format schema. @@ -245,7 +245,6 @@ def validate(nbdict=None, ref=None, version=None, version_minor=None, else: raise TypeError("validate() missing 1 required argument: 'nbdict'") - if ref is None: # if ref is not specified, we have a whole notebook, so we can get the version nbdict_version, nbdict_version_minor = get_version(nbdict) @@ -258,20 +257,10 @@ def validate(nbdict=None, ref=None, version=None, version_minor=None, if version is None: version, version_minor = 1, 0 - validator = get_validator(version, version_minor, relax_add_props=relax_add_props) - if validator is None: - raise ValidationError("No schema for validating v%s notebooks" % version) - elif validator.name != "jsonschema": - # If not using default validator then, skip iter_validate, and provide - # less legible errors - validator.validate(nbdict) - else: - # If using default validator then use iter_validate, and provide - # more readable errors - for error in iter_validate(nbdict, ref=ref, version=version, - version_minor=version_minor, - relax_add_props=relax_add_props): - raise error + for error in iter_validate(nbdict, ref=ref, version=version, + version_minor=version_minor, + relax_add_props=relax_add_props): + raise error def iter_validate(nbdict=None, ref=None, version=None, version_minor=None,