##// END OF EJS Templates
Use /notebook as root key
Jessica B. Hamrick -
Show More
@@ -1,70 +1,70 b''
1 from __future__ import print_function
1 from __future__ import print_function
2 #!/usr/bin/env python
2 #!/usr/bin/env python
3 # -*- coding: utf8 -*-
3 # -*- coding: utf8 -*-
4 import json
4 import json
5 import os
5 import os
6
6
7 from IPython.external.jsonschema import Draft3Validator
7 from IPython.external.jsonschema import Draft3Validator
8 import IPython.external.jsonpointer as jsonpointer
8 import IPython.external.jsonpointer as jsonpointer
9 from IPython.utils.py3compat import iteritems
9 from IPython.utils.py3compat import iteritems
10
10
11
11
12 from .current import nbformat, nbformat_schema
12 from .current import nbformat, nbformat_schema
13 schema_path = os.path.join(
13 schema_path = os.path.join(
14 os.path.split(__file__)[0], "v%d" % nbformat, nbformat_schema)
14 os.path.split(__file__)[0], "v%d" % nbformat, nbformat_schema)
15
15
16
16
17 def validate(nbjson, key='', verbose=True):
17 def validate(nbjson, key='/notebook', verbose=False):
18 # load the schema file
18 # load the schema file
19 with open(schema_path, 'r') as fh:
19 with open(schema_path, 'r') as fh:
20 schema_json = json.load(fh)
20 schema_json = json.load(fh)
21
21
22 # resolve internal references
22 # resolve internal references
23 v3schema = resolve_ref(schema_json)
23 v3schema = resolve_ref(schema_json)
24 v3schema = jsonpointer.resolve_pointer(v3schema, key)
24 v3schema = jsonpointer.resolve_pointer(v3schema, key)
25
25
26 errors = 0
26 errors = 0
27 v = Draft3Validator(v3schema)
27 v = Draft3Validator(v3schema)
28 for error in v.iter_errors(nbjson):
28 for error in v.iter_errors(nbjson):
29 errors = errors + 1
29 errors = errors + 1
30 if verbose:
30 if verbose:
31 print(error)
31 print(error)
32
32
33 return errors
33 return errors
34
34
35
35
36 def resolve_ref(json, schema=None):
36 def resolve_ref(json, schema=None):
37 """return a json with resolved internal references
37 """return a json with resolved internal references
38
38
39 only support local reference to the same json
39 only support local reference to the same json
40 """
40 """
41
41
42 if not schema:
42 if not schema:
43 schema = json
43 schema = json
44
44
45 # if it's a list, resolve references for each item in the list
45 # if it's a list, resolve references for each item in the list
46 if type(json) is list:
46 if type(json) is list:
47 resolved = []
47 resolved = []
48 for item in json:
48 for item in json:
49 resolved.append(resolve_ref(item, schema=schema))
49 resolved.append(resolve_ref(item, schema=schema))
50
50
51 # if it's a dictionary, resolve references for each item in the
51 # if it's a dictionary, resolve references for each item in the
52 # dictionary
52 # dictionary
53 elif type(json) is dict:
53 elif type(json) is dict:
54 resolved = {}
54 resolved = {}
55 for key, ref in iteritems(json):
55 for key, ref in iteritems(json):
56 # if the key is equal to $ref, then replace the entire
56 # if the key is equal to $ref, then replace the entire
57 # dictionary with the resolved value
57 # dictionary with the resolved value
58 if key == '$ref':
58 if key == '$ref':
59 assert len(json) == 1
59 assert len(json) == 1
60 pointer = jsonpointer.resolve_pointer(schema, ref)
60 pointer = jsonpointer.resolve_pointer(schema, ref)
61 resolved = resolve_ref(pointer, schema=schema)
61 resolved = resolve_ref(pointer, schema=schema)
62
62
63 else:
63 else:
64 resolved[key] = resolve_ref(ref, schema=schema)
64 resolved[key] = resolve_ref(ref, schema=schema)
65
65
66 # otherwise it's a normal object, so just return it
66 # otherwise it's a normal object, so just return it
67 else:
67 else:
68 resolved = json
68 resolved = json
69
69
70 return resolved
70 return resolved
General Comments 0
You need to be logged in to leave comments. Login now