Andy Mikhailenko avatar Andy Mikhailenko committed 7e2d8f1

Changed inheritance of validation-related exceptions.

Comments (0)

Files changed (2)


 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.


 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']}]})
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.