Andy Mikhailenko avatar Andy Mikhailenko committed eca4bf4

Fix key encoding for UnknownKey exception message

Comments (0)

Files changed (2)

monk/validation.py

 #    if missing and not skip_missing:
 #        raise MissingKey('Missing keys: {0}'.format(', '.join(missing)))
 
+    def _safe_str(text):
+        return text.encode('utf-8') if isinstance(text, unicode) else str(text)
+
     if unknown and not skip_unknown:
-        raise UnknownKey('Unknown keys: {0}'.format(', '.join(unknown)))
+        raise UnknownKey('Unknown keys: {0}'.format(
+            ', '.join(_safe_str(x) for x in unknown)))
 
     # check types and deal with nested lists
     for key in spec_keys | data_keys:

unittests/test_validation.py

         with pytest.raises(UnknownKey):
             validate_structure({'a': unicode}, {'a': u'A', 'x': 123})
 
+    def test_unknown_keys_encoding(self):
+        with pytest.raises(UnknownKey):
+            validate_structure({'a': unicode}, {'привет': 1})
+        with pytest.raises(UnknownKey):
+            validate_structure({'a': unicode}, {u'привет': 1})
+
     def test_bool(self):
         validate_structure({'a': bool}, {'a': None})
         validate_structure({'a': bool}, {'a': True})
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 ProjectModifiedEvent.java.
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.