Merge pull request #1599 from minrk/validate_nb

fix notebook validation
Thomas Kluyver 10 years ago committed by GitHub
commit 1176f29c61

@ -13,7 +13,7 @@ from tornado.web import HTTPError
from .checkpoints import Checkpoints
from traitlets.config.configurable import LoggingConfigurable
from nbformat import sign, validate, ValidationError
from nbformat import sign, validate as validate_nb, ValidationError
from nbformat.v4 import new_notebook
from ipython_genutils.importstring import import_item
from traitlets import (
@ -299,9 +299,9 @@ class ContentsManager(LoggingConfigurable):
def validate_notebook_model(self, model):
"""Add failed-validation message to model"""
try:
validate(model['content'])
validate_nb(model['content'])
except ValidationError as e:
model['message'] = u'Notebook Validation failed: {}:\n{}'.format(
model['message'] = u'Notebook validation failed: {}:\n{}'.format(
e.message, json.dumps(e.instance, indent=1, default=lambda obj: '<UNKNOWN>'),
)
return model

@ -292,6 +292,24 @@ class APITest(NotebookTestBase):
self.assertIn('content', nb)
self.assertEqual(nb['content'], None)
def test_get_nb_invalid(self):
nb = {
'nbformat': 4,
'metadata': {},
'cells': [{
'cell_type': 'wrong',
'metadata': {},
}],
}
path = u'å b/Validate tést.ipynb'
self.make_txt(path, py3compat.cast_unicode(json.dumps(nb)))
model = self.api.read(path).json()
self.assertEqual(model['path'], path)
self.assertEqual(model['type'], 'notebook')
self.assertIn('content', model)
self.assertIn('message', model)
self.assertIn("validation failed", model['message'].lower())
def test_get_contents_no_such_file(self):
# Name that doesn't exist - should be a 404
with assert_http_error(404):

Loading…
Cancel
Save