From a7ef7e80d0024b71794c22fd09e35389c04de964 Mon Sep 17 00:00:00 2001 From: Dave Shawley Date: Tue, 17 Nov 2020 08:15:25 -0500 Subject: fix #110: Validate patch documents during creation. --- jsonpatch.py | 3 +++ tests.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/jsonpatch.py b/jsonpatch.py index 7d5489a..022972b 100644 --- a/jsonpatch.py +++ b/jsonpatch.py @@ -222,6 +222,9 @@ class JsonPatch(object): 'copy': CopyOperation, } + for op in self.patch: + self._get_operation(op) + def __str__(self): """str(self) -> self.to_string()""" return self.to_string() diff --git a/tests.py b/tests.py index 0abf4d2..a7c1a43 100755 --- a/tests.py +++ b/tests.py @@ -671,6 +671,22 @@ class JsonPointerTests(unittest.TestCase): self.assertEqual(result, expected) +class JsonPatchCreationTest(unittest.TestCase): + + def test_creation_fails_with_invalid_patch(self): + invalid_patches = [ + { 'path': '/foo', 'value': 'bar'}, + {'op': 0xADD, 'path': '/foo', 'value': 'bar'}, + {'op': 'boo', 'path': '/foo', 'value': 'bar'}, + {'op': 'add', 'value': 'bar'}, + ] + for patch in invalid_patches: + with self.assertRaises(jsonpatch.InvalidJsonPatch): + jsonpatch.JsonPatch([patch]) + + with self.assertRaises(jsonpointer.JsonPointerException): + jsonpatch.JsonPatch([{'op': 'add', 'path': 'foo', 'value': 'bar'}]) + if __name__ == '__main__': modules = ['jsonpatch'] @@ -687,6 +703,7 @@ if __name__ == '__main__': suite.addTest(unittest.makeSuite(ConflictTests)) suite.addTest(unittest.makeSuite(OptimizationTests)) suite.addTest(unittest.makeSuite(JsonPointerTests)) + suite.addTest(unittest.makeSuite(JsonPatchCreationTest)) return suite -- cgit v1.2.1