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 <liangwen12year@gmail.com>
This commit is contained in:
Wen Liang 2021-06-28 07:41:37 -04:00 committed by Gris Ge
parent af07c2a58d
commit b569704c72
2 changed files with 6 additions and 0 deletions

View file

@ -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:

View file

@ -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):