Repository: nteract/vdom

Issue:
Write JSON Schema for the VDOM Spec
Write [JSON Schema](http://json-schema.org/), like Jupyter does for nbformat.

To accompany [the spec doc](https://github.com/nteract/vdom/blob/master/docs/spec.md).

Comments:

Patch:
diff --git a/vdom/core.py b/vdom/core.py
index bea7cd7..798560e 100644
--- a/vdom/core.py
+++ b/vdom/core.py
@@ -14,7 +14,7 @@ import os
 import io
 
 _vdom_schema_file_path = os.path.join(
-    os.path.dirname(__file__), "schemas", "vdom_schema_v0.json")
+    os.path.dirname(__file__), "schemas", "vdom_schema_v1.json")
 with io.open(_vdom_schema_file_path, "r") as f:
     VDOM_SCHEMA = json.load(f)
 _validate_err_template = "Your object didn't match the schema: {}. \n {}"
diff --git a/vdom/schemas/vdom_schema_v1.json b/vdom/schemas/vdom_schema_v1.json
new file mode 100644
index 0000000..f6f5de5
--- /dev/null
+++ b/vdom/schemas/vdom_schema_v1.json
@@ -0,0 +1,34 @@
+{
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "additionalProperties": false,
+    "required": ["tagName", "children", "attributes"],
+    "$ref": "#/definitions/vdomEl",
+    "definitions": {
+      "vdomEl": {
+        "type": "object",
+        "required": ["tagName", "children", "attributes"],
+        "properties": {
+          "tagName": {
+            "type": "string" 
+          },
+          "children": {
+            "$ref": "#/definitions/vdomNode"
+          },
+          "attributes": {
+            "type": "object"   
+          },
+          "key": {
+            "oneOf": [{"type": "number"}, {"type": "string"}, {"type": "null"}]
+          }
+        }
+      },
+      "vdomNode": {
+        "oneOf": [
+          { "$ref": "#/definitions/vdomEl" },
+          {"type": "array", "items": {"$ref": "#/definitions/vdomNode"}},
+          {"type": "null"},
+          {"type": "string"}
+        ]
+      }  
+    }
+  }
\ No newline at end of file


Test patch:
diff --git a/vdom/tests/test_core.py b/vdom/tests/test_core.py
index a915a81..85c26b4 100644
--- a/vdom/tests/test_core.py
+++ b/vdom/tests/test_core.py
@@ -4,9 +4,17 @@
 from ..core import _flatten_children, create_component, to_json, VDOM
 from ..helpers import div, p, img, h1, b
 from jsonschema import ValidationError, validate
+import os
+import io
+import json
 import pytest
 
 
+_vdom_schema_file_path = os.path.join(
+    os.path.dirname(__file__), "..", "schemas", "vdom_schema_v1.json")
+with io.open(_vdom_schema_file_path, "r") as f:
+    VDOM_SCHEMA = json.load(f)
+
 def test_flatten_children():
     # when the first argument is an array, interpret it as the primary argument
     assert _flatten_children([1, 2, 3]) == [1, 2, 3]
@@ -97,6 +105,7 @@ def test_to_json():
 
 
 _valid_vdom_obj = {'tagName': 'h1', 'children': 'Hey', 'attributes': {}}
+_invalid_vdom_obj = {'tagName': 'h1', 'children':[{'randomProperty': 'randomValue'}]}
 
 
 def test_schema_validation():
@@ -106,8 +115,15 @@ def test_schema_validation():
     # make sure you can pass empty schema
     assert (VDOM([_valid_vdom_obj],
                  schema={}).json_contents == [_valid_vdom_obj])
+  
+    # check that you can pass a valid schema
+    assert (VDOM(_valid_vdom_obj, schema=VDOM_SCHEMA))
 
+    # check that an invalid schema throws ValidationError
+    with pytest.raises(ValidationError):
+        test_vdom = VDOM([_invalid_vdom_obj], )    
 
+    
 def test_component_allows_children():
     nonvoid = create_component('nonvoid', allow_children=True)
     test_component = nonvoid(div())