diff --git a/IPython/nbformat/v4/nbbase.py b/IPython/nbformat/v4/nbbase.py index 7b482c4..886a216 100644 --- a/IPython/nbformat/v4/nbbase.py +++ b/IPython/nbformat/v4/nbbase.py @@ -19,7 +19,7 @@ from IPython.utils.py3compat import cast_unicode, unicode_type # Change this when incrementing the nbformat version nbformat = 4 nbformat_minor = 0 -nbformat_schema = 'v4.withref.json' +nbformat_schema = 'nbformat.v4.schema.json' class NotebookNode(Struct): pass diff --git a/IPython/nbformat/v4/v4.withref.json b/IPython/nbformat/v4/nbformat.v4.schema.json similarity index 98% rename from IPython/nbformat/v4/v4.withref.json rename to IPython/nbformat/v4/nbformat.v4.schema.json index 6d382d8..2049ed6 100644 --- a/IPython/nbformat/v4/v4.withref.json +++ b/IPython/nbformat/v4/nbformat.v4.schema.json @@ -141,8 +141,7 @@ "level": { "description": "Level of heading cells.", "type": "integer", - "minimum": 1, - "maximum": 6 + "minimum": 1 } } }, diff --git a/IPython/nbformat/v4/tests/test_validate.py b/IPython/nbformat/v4/tests/test_validate.py index a0cbbe7..d1d47de 100644 --- a/IPython/nbformat/v4/tests/test_validate.py +++ b/IPython/nbformat/v4/tests/test_validate.py @@ -8,119 +8,123 @@ import os import nose.tools as nt -from ..validator import isvalid, validate, ValidationError +from IPython.nbformat.validator import validate, ValidationError from ..nbjson import reads +from ..nbbase import nbformat from ..compose import ( new_code_cell, new_heading_cell, new_markdown_cell, new_notebook, new_output, new_raw_cell, ) +def validate4(obj, ref=None): + return validate(obj, ref, version=nbformat) + def test_valid_code_cell(): cell = new_code_cell() - validate(cell, 'code_cell') + validate4(cell, 'code_cell') def test_invalid_code_cell(): cell = new_code_cell() cell['source'] = 5 with nt.assert_raises(ValidationError): - validate(cell, 'code_cell') + validate4(cell, 'code_cell') cell = new_code_cell() del cell['metadata'] with nt.assert_raises(ValidationError): - validate(cell, 'code_cell') + validate4(cell, 'code_cell') cell = new_code_cell() del cell['source'] with nt.assert_raises(ValidationError): - validate(cell, 'code_cell') + validate4(cell, 'code_cell') cell = new_code_cell() del cell['cell_type'] with nt.assert_raises(ValidationError): - validate(cell, 'code_cell') + validate4(cell, 'code_cell') def test_invalid_markdown_cell(): cell = new_markdown_cell() cell['source'] = 5 with nt.assert_raises(ValidationError): - validate(cell, 'markdown_cell') + validate4(cell, 'markdown_cell') cell = new_markdown_cell() del cell['metadata'] with nt.assert_raises(ValidationError): - validate(cell, 'markdown_cell') + validate4(cell, 'markdown_cell') cell = new_markdown_cell() del cell['source'] with nt.assert_raises(ValidationError): - validate(cell, 'markdown_cell') + validate4(cell, 'markdown_cell') cell = new_markdown_cell() del cell['cell_type'] with nt.assert_raises(ValidationError): - validate(cell, 'markdown_cell') + validate4(cell, 'markdown_cell') def test_invalid_heading_cell(): cell = new_heading_cell() cell['source'] = 5 with nt.assert_raises(ValidationError): - validate(cell, 'heading_cell') + validate4(cell, 'heading_cell') cell = new_heading_cell() del cell['metadata'] with nt.assert_raises(ValidationError): - validate(cell, 'heading_cell') + validate4(cell, 'heading_cell') cell = new_heading_cell() del cell['source'] with nt.assert_raises(ValidationError): - validate(cell, 'heading_cell') + validate4(cell, 'heading_cell') cell = new_heading_cell() del cell['cell_type'] with nt.assert_raises(ValidationError): - validate(cell, 'heading_cell') + validate4(cell, 'heading_cell') def test_invalid_raw_cell(): cell = new_raw_cell() cell['source'] = 5 with nt.assert_raises(ValidationError): - validate(cell, 'raw_cell') + validate4(cell, 'raw_cell') cell = new_raw_cell() del cell['metadata'] with nt.assert_raises(ValidationError): - validate(cell, 'raw_cell') + validate4(cell, 'raw_cell') cell = new_raw_cell() del cell['source'] with nt.assert_raises(ValidationError): - validate(cell, 'raw_cell') + validate4(cell, 'raw_cell') cell = new_raw_cell() del cell['cell_type'] with nt.assert_raises(ValidationError): - validate(cell, 'raw_cell') + validate4(cell, 'raw_cell') def test_sample_notebook(): here = os.path.dirname(__file__) with io.open(os.path.join(here, "v4-test.ipynb"), encoding='utf-8') as f: nb = reads(f.read()) - validate(nb) + validate4(nb) diff --git a/IPython/nbformat/v4/validator.py b/IPython/nbformat/v4/validator.py deleted file mode 100644 index c238e2e..0000000 --- a/IPython/nbformat/v4/validator.py +++ /dev/null @@ -1,69 +0,0 @@ -# Copyright (c) IPython Development Team. -# Distributed under the terms of the Modified BSD License. - -from __future__ import print_function -import json -import os - -try: - from jsonschema import ValidationError - from jsonschema import Draft4Validator as Validator -except ImportError as e: - verbose_msg = """ - - IPython notebook format depends on the jsonschema package: - - https://pypi.python.org/pypi/jsonschema - - Please install it first. - """ - raise ImportError(str(e) + verbose_msg) - - -from .nbbase import nbformat, nbformat_schema -schema_path = os.path.join( - os.path.dirname(__file__), nbformat_schema, -) - -validator = None - -def _load_schema(): - """Load the JSON schema into the global Validator""" - global validator - if validator is None: - # load the schema file - with open(schema_path, 'r') as fh: - schema_json = json.load(fh) - - # create the validator - validator = Validator(schema_json) - return validator - -def isvalid(nbjson, ref=None): - """Checks whether the given notebook JSON conforms to the current - notebook format schema. Returns True if the JSON is valid, and - False otherwise. - - To see the individual errors that were encountered, please use the - `validate` function instead. - """ - try: - validate(nbjson, ref) - except ValidationError: - return False - else: - return True - - -def validate(nbjson, ref=None): - """Checks whether the given notebook JSON conforms to the current - notebook format schema. - - Raises ValidationError if not valid. - """ - _load_schema() - - if ref: - return validator.validate(nbjson, {'$ref' : '#/definitions/%s' % ref}) - else: - return validator.validate(nbjson) diff --git a/setupbase.py b/setupbase.py index fd745d0..749b594 100644 --- a/setupbase.py +++ b/setupbase.py @@ -199,6 +199,7 @@ def find_package_data(): 'IPython.nbformat' : [ 'tests/*.ipynb', 'v3/nbformat.v3.schema.json', + 'v4/nbformat.v4.schema.json', ] }