Skip to content

Commit 066e15c

Browse files
committed
Add review comments.
1 parent 7b4274c commit 066e15c

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

nbformat/json_compat.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ class FastJsonSchemaValidator(JsonSchemaValidator):
3838
name = "fastjsonschema"
3939

4040
def __init__(self, schema):
41-
super().__init__(schema)
42-
4341
self._validator = fastjsonschema.compile(schema)
4442

4543
def validate(self, data):
@@ -48,6 +46,16 @@ def validate(self, data):
4846
except _JsonSchemaException as error:
4947
raise ValidationError(error.message, schema_path=error.path)
5048

49+
def iter_errors(self, data, schema=None):
50+
errors = []
51+
validate_func = self._validator if schema is None else fastjsonschema.compile(schema)
52+
try:
53+
validate_func(data)
54+
except _JsonSchemaException as error:
55+
errors = [ValidationError(error.message, schema_path=error.path)]
56+
57+
return errors
58+
5159

5260
_VALIDATOR_MAP = [
5361
("fastjsonschema", fastjsonschema, FastJsonSchemaValidator),
@@ -68,7 +76,7 @@ def _validator_for_name(validator_name):
6876

6977
def get_current_validator():
7078
"""
71-
Return the current validator based on the value of an environment variable.
79+
Return the default validator based on the value of an environment variable.
7280
"""
7381
validator_name = os.environ.get("NBFORMAT_VALIDATOR", "jsonschema")
7482
return _validator_for_name(validator_name)

nbformat/validator.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def better_validation_error(error, version, version_minor):
229229

230230

231231
def validate(nbdict=None, ref=None, version=None, version_minor=None,
232-
relax_add_props=False, nbjson=None, use_fast=False):
232+
relax_add_props=False, nbjson=None):
233233
"""Checks whether the given notebook dict-like object
234234
conforms to the relevant notebook format schema.
235235
@@ -245,7 +245,6 @@ def validate(nbdict=None, ref=None, version=None, version_minor=None,
245245
else:
246246
raise TypeError("validate() missing 1 required argument: 'nbdict'")
247247

248-
249248
if ref is None:
250249
# if ref is not specified, we have a whole notebook, so we can get the version
251250
nbdict_version, nbdict_version_minor = get_version(nbdict)
@@ -258,20 +257,10 @@ def validate(nbdict=None, ref=None, version=None, version_minor=None,
258257
if version is None:
259258
version, version_minor = 1, 0
260259

261-
validator = get_validator(version, version_minor, relax_add_props=relax_add_props)
262-
if validator is None:
263-
raise ValidationError("No schema for validating v%s notebooks" % version)
264-
elif validator.name != "jsonschema":
265-
# If not using default validator then, skip iter_validate, and provide
266-
# less legible errors
267-
validator.validate(nbdict)
268-
else:
269-
# If using default validator then use iter_validate, and provide
270-
# more readable errors
271-
for error in iter_validate(nbdict, ref=ref, version=version,
272-
version_minor=version_minor,
273-
relax_add_props=relax_add_props):
274-
raise error
260+
for error in iter_validate(nbdict, ref=ref, version=version,
261+
version_minor=version_minor,
262+
relax_add_props=relax_add_props):
263+
raise error
275264

276265

277266
def iter_validate(nbdict=None, ref=None, version=None, version_minor=None,

0 commit comments

Comments
 (0)