Add hex color validator (#110846)

This commit is contained in:
Franck Nijhof 2024-02-18 16:03:21 +01:00 committed by GitHub
parent 70d1bbb20d
commit 67ac60d042
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 0 deletions

View File

@ -431,6 +431,19 @@ def icon(value: Any) -> str:
raise vol.Invalid('Icons should be specified in the form "prefix:name"')
_COLOR_HEX = re.compile(r"^#[0-9A-F]{6}$", re.IGNORECASE)
def color_hex(value: Any) -> str:
"""Validate a hex color code."""
str_value = str(value)
if not _COLOR_HEX.match(str_value):
raise vol.Invalid("Color should be in the format #RRGGBB")
return str_value
_TIME_PERIOD_DICT_KEYS = ("days", "hours", "minutes", "seconds", "milliseconds")
time_period_dict = vol.All(

View File

@ -1647,3 +1647,27 @@ def test_domain() -> None:
assert cv.domain_key("hue1") == "hue1"
assert cv.domain_key("hue 1") == "hue"
assert cv.domain_key("hue 1") == "hue"
def test_color_hex() -> None:
"""Test color validation in hex format."""
assert cv.color_hex("#123456") == "#123456"
assert cv.color_hex("#FFaaFF") == "#FFaaFF"
assert cv.color_hex("#FFFFFF") == "#FFFFFF"
assert cv.color_hex("#000000") == "#000000"
msg = r"Color should be in the format #RRGGBB"
with pytest.raises(vol.Invalid, match=msg):
cv.color_hex("#777")
with pytest.raises(vol.Invalid, match=msg):
cv.color_hex("FFFFF")
with pytest.raises(vol.Invalid, match=msg):
cv.color_hex("FFFFFF")
with pytest.raises(vol.Invalid, match=msg):
cv.color_hex("#FFFFFFF")
with pytest.raises(vol.Invalid, match=msg):
cv.color_hex(123456)