Skip to content

Commit a7ef7e8

Browse files
committed
fix #110: Validate patch documents during creation.
1 parent bfc0f5a commit a7ef7e8

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

jsonpatch.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ def __init__(self, patch):
222222
'copy': CopyOperation,
223223
}
224224

225+
for op in self.patch:
226+
self._get_operation(op)
227+
225228
def __str__(self):
226229
"""str(self) -> self.to_string()"""
227230
return self.to_string()

tests.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,22 @@ def test_create_with_pointer(self):
671671
self.assertEqual(result, expected)
672672

673673

674+
class JsonPatchCreationTest(unittest.TestCase):
675+
676+
def test_creation_fails_with_invalid_patch(self):
677+
invalid_patches = [
678+
{ 'path': '/foo', 'value': 'bar'},
679+
{'op': 0xADD, 'path': '/foo', 'value': 'bar'},
680+
{'op': 'boo', 'path': '/foo', 'value': 'bar'},
681+
{'op': 'add', 'value': 'bar'},
682+
]
683+
for patch in invalid_patches:
684+
with self.assertRaises(jsonpatch.InvalidJsonPatch):
685+
jsonpatch.JsonPatch([patch])
686+
687+
with self.assertRaises(jsonpointer.JsonPointerException):
688+
jsonpatch.JsonPatch([{'op': 'add', 'path': 'foo', 'value': 'bar'}])
689+
674690

675691
if __name__ == '__main__':
676692
modules = ['jsonpatch']
@@ -687,6 +703,7 @@ def get_suite():
687703
suite.addTest(unittest.makeSuite(ConflictTests))
688704
suite.addTest(unittest.makeSuite(OptimizationTests))
689705
suite.addTest(unittest.makeSuite(JsonPointerTests))
706+
suite.addTest(unittest.makeSuite(JsonPatchCreationTest))
690707
return suite
691708

692709

0 commit comments

Comments
 (0)