Commits

Ian Lewis  committed 958ce34

Added support for a new validation option interactive_mode. This specifies whether the original document will be updated with default values etc. during validation.

  • Participants
  • Parent commits 6a6ae88

Comments (0)

Files changed (3)

File jsonschema/__init__.py

 __all__ = [ 'validate', 'JSONSchemaValidator' ]
 __version__ = '0.1a'
 
-def validate(data, schema, validator_cls=None):
+def validate(data, schema, validator_cls=None, interactive_mode=True):
   '''Validates a parsed json document against the provided schema'''
   if validator_cls == None:
     validator_cls = JSONSchemaValidator
-  v = validator_cls()
+  v = validator_cls(interactive_mode)
   return v.validate(data,schema)
 
 if __name__ == '__main__':

File jsonschema/tests/test_default.py

       if (data.get("test") == 10):
         self.fail("Default value was set when the schema indicates it should be readonly")
     except ValueError:
+      self.fail("Unexpected failure: %s" % e)
+  
+  def test_default_interactive(self):
+    
+    data = {}
+    
+    try:
+      # Explicitly set the interactive_mode
+      jsonschema.validate(data, self.schema, interactive_mode=True)
+      if (data.get("test") != 10):
+        self.fail("Default value was not set")
+    except ValueError, e:
+      self.fail("Unexpected failure: %s" % e)
+  
+  def test_default_non_interactive(self):
+    
+    data = {}
+    
+    try:
+      jsonschema.validate(data, self.schema, interactive_mode=False)
+      if (data.get("test") == 10):
+        self.fail("Default value was set when in non-interactive mode.")
+    except ValueError:
       self.fail("Unexpected failure: %s" % e)

File jsonschema/validator.py

   
   _refmap = {}
   
+  _interactive_mode = True
+  
+  def __init__(self, interactive_mode=True):
+    self._interactive_mode = interactive_mode
+  
   def validate_id(self, x, fieldname, schema, ID=None):
     '''Validates a schema id and adds it to the schema reference map'''
     if ID is not None:
   def validate_default(self, x, fieldname, schema, default=None):
     '''Adds default data to the original json document if the document is
        not readonly'''
-    if fieldname not in x.keys() and default is not None:
+    if self._interactive_mode and fieldname not in x.keys() and default is not None:
       if not schema.get("readonly"):
         x[fieldname] = default
     return x