diff options
author | Alanscut <wp_scut@163.com> | 2020-05-23 18:16:06 +0800 |
---|---|---|
committer | Alanscut <wp_scut@163.com> | 2020-05-23 18:16:06 +0800 |
commit | 1015d7f35958f196478f32148656171f87358cde (patch) | |
tree | f074eee156da3c25390a1658d742df0c5fec03e0 | |
parent | 9e4d423c22bedad6ffc260541ae587114dfed70a (diff) | |
download | python-json-patch-1015d7f35958f196478f32148656171f87358cde.tar.gz |
fix #111: optimizing exception message
-rw-r--r-- | jsonpatch.py | 8 | ||||
-rwxr-xr-x | tests.py | 11 |
2 files changed, 18 insertions, 1 deletions
diff --git a/jsonpatch.py b/jsonpatch.py index ca22e34..22e1156 100644 --- a/jsonpatch.py +++ b/jsonpatch.py @@ -334,12 +334,18 @@ class PatchOperation(object): def __init__(self, operation): + if not operation.__contains__('path'): + raise InvalidJsonPatch("Operation must have a 'path' member") + if isinstance(operation['path'], JsonPointer): self.location = operation['path'].path self.pointer = operation['path'] else: self.location = operation['path'] - self.pointer = JsonPointer(self.location) + try: + self.pointer = JsonPointer(self.location) + except TypeError as ex: + raise InvalidJsonPatch("Invalid 'path'") self.operation = operation @@ -219,6 +219,17 @@ class ApplyPatchTestCase(unittest.TestCase): ]) self.assertEqual(res['foo'], [1, 2, 3, 4]) + def test_add_missing_path(self): + obj = {'bar': 'qux'} + self.assertRaises(jsonpatch.InvalidJsonPatch, + jsonpatch.apply_patch, + obj, [{'op': 'test', 'value': 'bar'}]) + + def test_path_with_null_value(self): + obj = {'bar': 'qux'} + self.assertRaises(jsonpatch.InvalidJsonPatch, + jsonpatch.apply_patch, + obj, '[{"op": "add", "path": null, "value": "bar"}]') class EqualityTestCase(unittest.TestCase): |