##// END OF EJS Templates
raise ValidationError in v4 validate...
MinRK -
Show More
@@ -1,66 +1,69 b''
1 # Copyright (c) IPython Development Team.
1 # Copyright (c) IPython Development Team.
2 # Distributed under the terms of the Modified BSD License.
2 # Distributed under the terms of the Modified BSD License.
3
3
4 from __future__ import print_function
4 from __future__ import print_function
5 import json
5 import json
6 import os
6 import os
7
7
8 try:
8 try:
9 from jsonschema import SchemaError
9 from jsonschema import ValidationError
10 from jsonschema import Draft4Validator as Validator
10 from jsonschema import Draft4Validator as Validator
11 except ImportError as e:
11 except ImportError as e:
12 verbose_msg = """
12 verbose_msg = """
13
13
14 IPython notebook format depends on the jsonschema package:
14 IPython notebook format depends on the jsonschema package:
15
15
16 https://pypi.python.org/pypi/jsonschema
16 https://pypi.python.org/pypi/jsonschema
17
17
18 Please install it first.
18 Please install it first.
19 """
19 """
20 raise ImportError(str(e) + verbose_msg)
20 raise ImportError(str(e) + verbose_msg)
21
21
22
22
23 from .nbbase import nbformat, nbformat_schema
23 from .nbbase import nbformat, nbformat_schema
24 schema_path = os.path.join(
24 schema_path = os.path.join(
25 os.path.dirname(__file__), nbformat_schema,
25 os.path.dirname(__file__), nbformat_schema,
26 )
26 )
27
27
28 validator = None
29
30 def _load_schema():
31 """Load the JSON schema into the global Validator"""
32 global validator
33 if validator is None:
34 # load the schema file
35 with open(schema_path, 'r') as fh:
36 schema_json = json.load(fh)
37
38 # create the validator
39 validator = Validator(schema_json)
40 return validator
28
41
29 def isvalid(nbjson, ref=None):
42 def isvalid(nbjson, ref=None):
30 """Checks whether the given notebook JSON conforms to the current
43 """Checks whether the given notebook JSON conforms to the current
31 notebook format schema. Returns True if the JSON is valid, and
44 notebook format schema. Returns True if the JSON is valid, and
32 False otherwise.
45 False otherwise.
33
46
34 To see the individual errors that were encountered, please use the
47 To see the individual errors that were encountered, please use the
35 `validate` function instead.
48 `validate` function instead.
36 """
49 """
37
38 it = validate(nbjson, ref)
39 try:
50 try:
40 it.next()
51 validate(nbjson, ref)
41 except StopIteration:
52 except ValidationError:
42 return True
43 else:
44 return False
53 return False
54 else:
55 return True
45
56
46
57
47 def validate(nbjson, ref=None):
58 def validate(nbjson, ref=None):
48 """Checks whether the given notebook JSON conforms to the current
59 """Checks whether the given notebook JSON conforms to the current
49 notebook format schema.
60 notebook format schema.
50
61
51 Returns a generator for errors.
62 Raises ValidationError if not valid.
52 """
63 """
64 _load_schema()
53
65
54 # load the schema file
55 with open(schema_path, 'r') as fh:
56 schema_json = json.load(fh)
57
58 # create the validator
59 v = Validator(schema_json)
60
61 # return the iterator on errors
62 if ref:
66 if ref:
63 errors = v.iter_errors(nbjson, {'$ref' : '#/definitions/%s' % ref})
67 return validator.validate(nbjson, {'$ref' : '#/definitions/%s' % ref})
64 else:
68 else:
65 errors = v.iter_errors(nbjson)
69 return validator.validate(nbjson)
66 return errors
General Comments 0
You need to be logged in to leave comments. Login now