From fb16bfda293b520ceb9d4dbea3bde0f15cc441bb Mon Sep 17 00:00:00 2001 From: "john.lin" Date: Fri, 8 Dec 2017 15:31:42 -0800 Subject: [PATCH 1/2] Recursively validate JSON while preserving helper function types --- tests/test_apigateway.py | 2 +- troposphere/validators.py | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/tests/test_apigateway.py b/tests/test_apigateway.py index a0adf3949..18cea5f5a 100644 --- a/tests/test_apigateway.py +++ b/tests/test_apigateway.py @@ -37,7 +37,7 @@ def test_schema(self): Schema=d ) model.validate() - self.assertEqual(model.properties['Schema'], '{"c": "d"}') + self.assertEqual(model.properties['Schema'], {"c": "d"}) # Check invalid Schema type with self.assertRaises(TypeError): diff --git a/troposphere/validators.py b/troposphere/validators.py index b6e1ef34c..a2250ec79 100644 --- a/troposphere/validators.py +++ b/troposphere/validators.py @@ -224,11 +224,16 @@ def json_checker(name, prop): # Verify it is a valid json string json.loads(prop) return prop - elif isinstance(prop, dict): - # Convert the dict to a basestring - return json.dumps(prop) elif isinstance(prop, AWSHelperFn): return prop + elif isinstance(prop, dict): + # Recursively validate JSON + for attr, attr_val in prop.items(): + if isinstance(attr_val, basestring): + json.dumps(attr_val) + elif isinstance(attr_val, dict): + prop[attr] = json_checker(attr, attr_val) + return prop else: raise ValueError("%s must be a str or dict" % name) From dfb84a919f903691bd5221f20f8c278a09e90033 Mon Sep 17 00:00:00 2001 From: "john.lin" Date: Mon, 11 Dec 2017 11:08:33 -0800 Subject: [PATCH 2/2] Update cloudwatch tests to comply with new json_checker behavior --- tests/test_cloudwatch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_cloudwatch.py b/tests/test_cloudwatch.py index cc281a79d..459f69265 100644 --- a/tests/test_cloudwatch.py +++ b/tests/test_cloudwatch.py @@ -26,7 +26,7 @@ def test_dashboard(self): DashboardBody=d ) dashboard.validate() - self.assertEqual(dashboard.properties['DashboardBody'], '{"c": "d"}') + self.assertEqual(dashboard.properties['DashboardBody'], {"c": "d"}) # Check invalid Dashboard type with self.assertRaises(TypeError):