Commits

Andy Mikhailenko  committed 7e2d8f1

Changed inheritance of validation-related exceptions.

  • Participants
  • Parent commits 5cdc27c

Comments (0)

Files changed (2)

File monk/validation.py

 from monk.helpers import walk_dict
 
 
-class StructureSpecificationError(Exception):
+class ValidationError(Exception):
+    "Raised when a document or its part cannot pass validation."
+
+
+class StructureSpecificationError(ValidationError):
     "Raised when malformed document structure is detected."
-    pass
 
 
-class MissingKey(KeyError):
+class MissingKey(ValidationError):
     """ Raised when a key is defined in the structure spec but is missing from
     a data dictionary.
     """
 
 
-class UnknownKey(KeyError):
+class UnknownKey(ValidationError):
     """ Raised when a key in data dictionary is missing from the corresponding
     structure spec.
     """

File unittests/test_validation.py

 import pytest
 
 from monk.validation import (
-    validate_structure_spec, validate_structure, StructureSpecificationError
+    validate_structure_spec, validate_structure, StructureSpecificationError,
+    MissingKey, UnknownKey
 )
 
 
 
     def test_missing(self):
         validate_structure({'a': unicode}, {}, skip_missing=True)
-        with pytest.raises(KeyError):
+        with pytest.raises(MissingKey):
             validate_structure({'a': unicode}, {})
-        with pytest.raises(KeyError):
+        with pytest.raises(MissingKey):
             validate_structure({'a': unicode, 'b': int}, {'b': 1})
 
     def test_unknown_keys(self):
         validate_structure({}, {'x': 123}, skip_unknown=True)
-        with pytest.raises(KeyError):
+        with pytest.raises(UnknownKey):
             validate_structure({}, {'x': 123})
-        with pytest.raises(KeyError):
+        with pytest.raises(UnknownKey):
             validate_structure({'a': unicode}, {'a': u'A', 'x': 123})
         with pytest.raises(TypeError) as excinfo:
             validate_structure({'a': [{'b': [int]}]}, {'a': [{'b': ['bad']}]})