##// END OF EJS Templates
assume valid if there's no schema (v2)
MinRK -
Show More
@@ -1,108 +1,117 b''
1 1 # Copyright (c) IPython Development Team.
2 2 # Distributed under the terms of the Modified BSD License.
3 3
4 4 from __future__ import print_function
5 5 import json
6 6 import os
7 7
8 8 try:
9 9 from jsonschema import ValidationError
10 10 from jsonschema import Draft4Validator as Validator
11 11 except ImportError as e:
12 12 verbose_msg = """
13 13
14 14 IPython notebook format depends on the jsonschema package:
15 15
16 16 https://pypi.python.org/pypi/jsonschema
17 17
18 18 Please install it first.
19 19 """
20 20 raise ImportError(str(e) + verbose_msg)
21 21
22 22 from IPython.utils.importstring import import_item
23 23
24 24
25 25 validators = {}
26 26
27 27 def get_validator(version=None):
28 28 """Load the JSON schema into a Validator"""
29 29 if version is None:
30 30 from .current import nbformat as version
31 31
32 32 if version not in validators:
33 33 v = import_item("IPython.nbformat.v%s" % version)
34 try:
35 v.nbformat_schema
36 except AttributeError:
37 # no validator
38 return None
34 39 schema_path = os.path.join(os.path.dirname(v.__file__), v.nbformat_schema)
35 40 with open(schema_path) as f:
36 41 schema_json = json.load(f)
37 42 validators[version] = Validator(schema_json)
38 43 return validators[version]
39 44
40 45 def isvalid(nbjson, ref=None, version=None):
41 46 """Checks whether the given notebook JSON conforms to the current
42 47 notebook format schema. Returns True if the JSON is valid, and
43 48 False otherwise.
44 49
45 50 To see the individual errors that were encountered, please use the
46 51 `validate` function instead.
47 52 """
48 53 try:
49 54 validate(nbjson, ref, version)
50 55 except ValidationError:
51 56 return False
52 57 else:
53 58 return True
54 59
55 60
56 61 def better_validation_error(error, version):
57 62 """Get better ValidationError on oneOf failures
58 63
59 64 oneOf errors aren't informative.
60 65 if it's a cell type or output_type error,
61 66 try validating directly based on the type for a better error message
62 67 """
63 68 key = error.schema_path[-1]
64 69 if key.endswith('Of'):
65 70
66 71 ref = None
67 72 if isinstance(error.instance, dict):
68 73 if 'cell_type' in error.instance:
69 74 ref = error.instance['cell_type'] + "_cell"
70 75 elif 'output_type' in error.instance:
71 76 ref = error.instance['output_type']
72 77
73 78 if ref:
74 79 try:
75 80 validate(error.instance,
76 81 ref,
77 82 version=version
78 83 )
79 84 except ValidationError as e:
80 85 return better_validation_error(e, version)
81 86 except:
82 87 # if it fails for some reason,
83 88 # let the original error through
84 89 pass
85 90
86 91 return error
87 92
88 93
89 94 def validate(nbjson, ref=None, version=None):
90 95 """Checks whether the given notebook JSON conforms to the current
91 96 notebook format schema.
92 97
93 98 Raises ValidationError if not valid.
94 99 """
95 100 if version is None:
96 101 from .current import nbformat
97 102 version = nbjson.get('nbformat', nbformat)
98 103
99 104 validator = get_validator(version)
100 105
106 if validator is None:
107 # no validator
108 return
109
101 110 try:
102 111 if ref:
103 112 return validator.validate(nbjson, {'$ref' : '#/definitions/%s' % ref})
104 113 else:
105 114 return validator.validate(nbjson)
106 115 except ValidationError as e:
107 116 raise better_validation_error(e, version)
108 117
General Comments 0
You need to be logged in to leave comments. Login now