From b569704c7258341768c468069a4e6f5199cb42d7 Mon Sep 17 00:00:00 2001 From: Wen Liang Date: Mon, 28 Jun 2021 07:41:37 -0400 Subject: [PATCH] arg_validator: accept None as valid input for ArgValidatorDict Users might want to use jinja2 templates to set properties. As such, it's convenient to accept None as an alias for an empty dictionary. For exmaple, setting like `"match": None` will be allowed by the role: e.g. network_connections: - name: enp0s8 type: ethernet persistent_state: present state: up match: ip: route_metric4: 10 Signed-off-by: Wen Liang --- module_utils/network_lsr/argument_validator.py | 5 +++++ tests/unit/test_network_connections.py | 1 + 2 files changed, 6 insertions(+) diff --git a/module_utils/network_lsr/argument_validator.py b/module_utils/network_lsr/argument_validator.py index b5a9253..49a4419 100644 --- a/module_utils/network_lsr/argument_validator.py +++ b/module_utils/network_lsr/argument_validator.py @@ -350,6 +350,11 @@ class ArgValidatorDict(ArgValidator): def _validate_impl(self, value, name): result = {} seen_keys = set() + if value is None: + # Users might want to use jinja2 templates to set properties. As such, + # it's convenient to accept None as an alias for an empty dictionary + # e.g. setting like `"match": None` will be allowed by the role + return {} try: items = list(value.items()) except AttributeError: diff --git a/tests/unit/test_network_connections.py b/tests/unit/test_network_connections.py index 74f4d37..e32e20c 100644 --- a/tests/unit/test_network_connections.py +++ b/tests/unit/test_network_connections.py @@ -437,6 +437,7 @@ class TestValidator(unittest.TestCase): {"i": 5, "s": "s_default", "l": "6"}, v.validate({"i": "5", "l": "6"}) ) self.assertValidationError(v, {"k": 1}) + self.assertEqual(v.validate(None), {}) def test_validate_list(self):