Skip to content

Commit

Permalink
Merge pull request #198 from goanpeca/fix/fast-error
Browse files Browse the repository at this point in the history
PR: Fallback to default validator if subschema is provided
  • Loading branch information
willingc committed Nov 21, 2020
2 parents 22396b8 + 9cc08ad commit 0ba4ea5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion nbformat/json_compat.py
Expand Up @@ -38,6 +38,7 @@ class FastJsonSchemaValidator(JsonSchemaValidator):
name = "fastjsonschema"

def __init__(self, schema):
super().__init__(schema)
self._validator = fastjsonschema.compile(schema)

def validate(self, data):
Expand All @@ -47,8 +48,11 @@ def validate(self, data):
raise ValidationError(error.message, schema_path=error.path)

def iter_errors(self, data, schema=None):
if schema is not None:
return self._default_validator.iter_errors(data, schema)

errors = []
validate_func = self._validator if schema is None else fastjsonschema.compile(schema)
validate_func = self._validator
try:
validate_func(data)
except _JsonSchemaException as error:
Expand Down
12 changes: 12 additions & 0 deletions nbformat/tests/test_validator.py
Expand Up @@ -197,3 +197,15 @@ def test_invalid_validator_raises_value_error_after_read():
set_validator("foobar")
with pytest.raises(ValueError):
validate(nb)


def test_fallback_validator_with_iter_errors_using_ref():
"""
Test that when creating a standalone object (code_cell etc)
the default validator is used as fallback.
"""
import nbformat
set_validator("fastjsonschema")
nbformat.v4.new_code_cell()
nbformat.v4.new_markdown_cell()
nbformat.v4.new_raw_cell()

0 comments on commit 0ba4ea5

Please sign in to comment.