Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json_checker - recursively validate dictionary while ignoring non-string types #946

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/test_apigateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cloudwatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
11 changes: 8 additions & 3 deletions troposphere/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down