##// END OF EJS Templates
raise ValidationError in v4 validate...
raise ValidationError in v4 validate also only load the schema once and store it in a module global

File last commit:

r18572:14ea49e4
r18572:14ea49e4
Show More
validator.py
69 lines | 1.7 KiB | text/x-python | PythonLexer
MinRK
update v4 schema...
r18569 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
from __future__ import print_function
import json
import os
try:
MinRK
raise ValidationError in v4 validate...
r18572 from jsonschema import ValidationError
MinRK
update v4 schema...
r18569 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,
)
MinRK
raise ValidationError in v4 validate...
r18572 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
MinRK
update v4 schema...
r18569
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:
MinRK
raise ValidationError in v4 validate...
r18572 validate(nbjson, ref)
except ValidationError:
MinRK
update v4 schema...
r18569 return False
MinRK
raise ValidationError in v4 validate...
r18572 else:
return True
MinRK
update v4 schema...
r18569
def validate(nbjson, ref=None):
"""Checks whether the given notebook JSON conforms to the current
notebook format schema.
MinRK
raise ValidationError in v4 validate...
r18572 Raises ValidationError if not valid.
MinRK
update v4 schema...
r18569 """
MinRK
raise ValidationError in v4 validate...
r18572 _load_schema()
MinRK
update v4 schema...
r18569
if ref:
MinRK
raise ValidationError in v4 validate...
r18572 return validator.validate(nbjson, {'$ref' : '#/definitions/%s' % ref})
MinRK
update v4 schema...
r18569 else:
MinRK
raise ValidationError in v4 validate...
r18572 return validator.validate(nbjson)