Commits

Ian Lewis committed 6a3aaca

Added missing support for schemas in the type field.

  • Participants
  • Parent commits 2b2e090

Comments (0)

Files changed (2)

File jsonschema/tests/test_type.py

 import jsonschema
 
 class TestType(TestCase):
+    
+    def test_schema(self):
+      schema = { 
+        "type": [
+          { "type": "array",  "minItems": 10 },
+          { "type": "string", "pattern": "^0+$" } 
+        ]
+      }
+      
+      data1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+      data2 = "0";
+      data3 = 1203;
+      
+      for x in [data1, data2]:
+        try:
+          jsonschema.validate(x, schema)
+        except ValueError, e:
+          self.fail("Unexpected failure: %s" % e)
+      
+      try:
+        jsonschema.validate(data3, schema)
+      except ValueError:
+        pass
+      else:
+        self.fail("Expected failure for %s" % repr(x))
+      
     def test_integer(self):
       for x in [1, 89, 48, 32, 49, 42]:
         try:

File jsonschema/validator.py

             pass
         if not datavalid:
           raise ValueError("Value %r for field '%s' is not of type %r" % (value, fieldname, fieldtype))
+      elif isinstance(converted_fieldtype, types.DictType):
+        try:
+          self.__validate(fieldname, x, converted_fieldtype)
+        except ValueError,e:
+          raise e
       else:
         # isinstance(True, types.IntType) returns true so we need to write a
         # workaround
           elif isinstance(items, types.DictType):
             for eachItem in value:
                 try:
-                  # print eachItem
-                  # print repr(items)
                   self._validate(eachItem, items)
                 except ValueError, e:
                   raise ValueError("Failed to validate field '%s' list schema: %r" % (fieldname, e.message))
     return x
   
   def _convert_type(self, fieldtype):
-    if isinstance(fieldtype, types.TypeType):
+    if type(fieldtype) in (types.TypeType, types.DictType):
       return fieldtype
     elif isinstance(fieldtype, types.ListType):
       converted_fields = []