def validate_model(self, model): """Checks that the model given to the API handler meets bookstore's expected structure for a notebook. Pattern for surfacing nbformat validation errors originally written in https://github.com/jupyter/notebook/blob/a44a367c219b60a19bee003877d32c3ff1ce2412/notebook/services/contents/manager.py#L353-L355 Parameters ---------- model: dict Request model for publishing describing the type and content of the object. Raises ------ tornado.web.HTTPError Your model does not validate correctly """ if not model: raise web.HTTPError(400, "Bookstore cannot publish an empty model") if model.get('type', "") != 'notebook': raise web.HTTPError(415, "Bookstore only publishes notebooks") content = model.get('content', {}) if content == {}: raise web.HTTPError(422, "Bookstore cannot publish empty contents") try: validate_nb(content) except ValidationError as e: raise web.HTTPError( 422, "Bookstore cannot publish invalid notebook. " "Validation errors are as follows: " f"{e.message} {json.dumps(e.instance, indent=1, default=lambda obj: '<UNKNOWN>')}", )
def validate_notebook_model(self, model): """Add failed-validation message to model""" try: validate_nb(model['content']) except ValidationError as e: model['message'] = u'Notebook validation failed: {}:\n{}'.format( e.message, json.dumps(e.instance, indent=1, default=lambda obj: '<UNKNOWN>'), ) return model
def validate_notebook_model(self, model, validation_error=None): """Add failed-validation message to model""" try: # If we're given a validation_error dictionary, extract the exception # from it and raise the exception, else call nbformat's validate method # to determine if the notebook is valid. This 'else' condition may # pertain to server extension not using the server's notebook read/write # functions. if validation_error is not None: e = validation_error.get("ValidationError") if isinstance(e, ValidationError): raise e else: validate_nb(model["content"]) except ValidationError as e: model["message"] = "Notebook validation failed: {}:\n{}".format( str(e), json.dumps(e.instance, indent=1, default=lambda obj: "<UNKNOWN>"), ) return model