Fix validation issue addon options (#1898)

* Fix validation issue addon options

* Add some basic tests

* Update supervisor/addons/validate.py

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
Pascal Vizeli 2020-08-12 14:49:46 +02:00 committed by GitHub
parent 0df5b7d87b
commit a9bdab4b49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 0 deletions

View File

@ -424,6 +424,11 @@ def _nested_validate_list(coresys, typ, data_list, key):
"""Validate nested items."""
options = []
# Make sure it is a list
if not isinstance(data_list, list):
raise vol.Invalid(f"Invalid list for {key}")
# Process list
for element in data_list:
# Nested?
if isinstance(typ, dict):
@ -439,6 +444,11 @@ def _nested_validate_dict(coresys, typ, data_dict, key):
"""Validate nested items."""
options = {}
# Make sure it is a dict
if not isinstance(data_dict, dict):
raise vol.Invalid(f"Invalid dict for {key}")
# Process dict
for c_key, c_value in data_dict.items():
# Ignore unknown options / remove from list
if c_key not in typ:

View File

@ -0,0 +1,65 @@
"""Test add-ons schema to UI schema convertion."""
import pytest
import voluptuous as vol
from supervisor.addons.validate import validate_options
def test_simple_schema(coresys):
"""Test with simple schema."""
assert validate_options(
coresys,
{"name": "str", "password": "password", "fires": "bool", "alias": "str?"},
)({"name": "Pascal", "password": "1234", "fires": True, "alias": "test"})
assert validate_options(
coresys,
{"name": "str", "password": "password", "fires": "bool", "alias": "str?"},
)({"name": "Pascal", "password": "1234", "fires": True})
with pytest.raises(vol.error.Invalid):
validate_options(
coresys,
{"name": "str", "password": "password", "fires": "bool", "alias": "str?"},
)({"name": "Pascal", "password": "1234", "fires": "hah"})
with pytest.raises(vol.error.Invalid):
validate_options(
coresys,
{"name": "str", "password": "password", "fires": "bool", "alias": "str?"},
)({"name": "Pascal", "fires": True})
def test_complex_schema_list(coresys):
"""Test with complex list schema."""
assert validate_options(
coresys, {"name": "str", "password": "password", "extend": ["str"]},
)({"name": "Pascal", "password": "1234", "extend": ["test", "blu"]})
with pytest.raises(vol.error.Invalid):
validate_options(
coresys, {"name": "str", "password": "password", "extend": ["str"]},
)({"name": "Pascal", "password": "1234", "extend": ["test", 1]})
with pytest.raises(vol.error.Invalid):
validate_options(
coresys, {"name": "str", "password": "password", "extend": ["str"]},
)({"name": "Pascal", "password": "1234", "extend": "test"})
def test_complex_schema_dict(coresys):
"""Test with complex dict schema."""
assert validate_options(
coresys, {"name": "str", "password": "password", "extend": {"test": "int"}},
)({"name": "Pascal", "password": "1234", "extend": {"test": 1}})
with pytest.raises(vol.error.Invalid):
validate_options(
coresys, {"name": "str", "password": "password", "extend": {"test": "int"}},
)({"name": "Pascal", "password": "1234", "extend": {"wrong": 1}})
with pytest.raises(vol.error.Invalid):
validate_options(
coresys, {"name": "str", "password": "password", "extend": ["str"]},
)({"name": "Pascal", "password": "1234", "extend": "test"})