Skip to content

Commit

Permalink
Add review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
goanpeca committed Sep 24, 2020
1 parent 7b4274c commit 066e15c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
14 changes: 11 additions & 3 deletions nbformat/json_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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),
Expand All @@ -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)
21 changes: 5 additions & 16 deletions nbformat/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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,
Expand Down

0 comments on commit 066e15c

Please sign in to comment.