Allow optional array configs (#2752)

This commit is contained in:
Mike Degatano 2021-03-26 05:42:58 -04:00 committed by GitHub
parent e6c57dfc80
commit 58c40cbef6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 1 deletions

View File

@ -241,8 +241,15 @@ class AddonOptions(CoreSysAttributes):
"""Check if all options are exists."""
missing = set(origin) - set(exists)
for miss_opt in missing:
if isinstance(origin[miss_opt], str) and origin[miss_opt].endswith("?"):
miss_schema = origin[miss_opt]
# If its a list then value in list decides if its optional like ["str?"]
if isinstance(miss_schema, list) and len(miss_schema) > 0:
miss_schema = miss_schema[0]
if isinstance(miss_schema, str) and miss_schema.endswith("?"):
continue
raise vol.Invalid(
f"Missing option '{miss_opt}' in {root} in {self._name} ({self._slug})"
) from None

View File

@ -70,6 +70,38 @@ def test_complex_schema_list(coresys):
)({"name": "Pascal", "password": "1234", "extend": "test"})
def test_optional_schema_list(coresys):
"""Test with an optional list schema."""
assert AddonOptions(
coresys,
{"name": "str", "password": "password", "extend": ["str?"]},
MOCK_ADDON_NAME,
MOCK_ADDON_SLUG,
)({"name": "Pascal", "password": "1234"})
assert AddonOptions(
coresys,
{"name": "str", "password": "password", "extend": ["str?"]},
MOCK_ADDON_NAME,
MOCK_ADDON_SLUG,
)({"name": "Pascal", "password": "1234", "extend": []})
with pytest.raises(vol.error.Invalid):
AddonOptions(
coresys,
{"name": "str", "password": "password", "extend": ["str"]},
MOCK_ADDON_NAME,
MOCK_ADDON_SLUG,
)({"name": "Pascal", "password": "1234"})
assert AddonOptions(
coresys,
{"name": "str", "password": "password", "extend": ["str"]},
MOCK_ADDON_NAME,
MOCK_ADDON_SLUG,
)({"name": "Pascal", "password": "1234", "extend": []})
def test_complex_schema_dict(coresys):
"""Test with complex dict schema."""
assert AddonOptions(