Validate UUID for tankerkoenig (#32805)
* Validate UUIDs against custom validator Instead of just validating against strings, use a custom validator, so that the format can be checked. * Add tests for custom UUID4 validator
This commit is contained in:
1 parent
ebc4804e04
commit
49c2a4a4e3
4 files changed
+54
-2
No files matched your search
@@ -1,6 +1,7 @@
|
||||
"""Ask tankerkoenig.de for petrol price information."""
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from uuid import UUID
|
||||
|
||||
import pytankerkoenig
|
||||
import voluptuous as vol
|
||||
@@ -24,11 +25,26 @@ _LOGGER = logging.getLogger(__name__)
|
||||
DEFAULT_RADIUS = 2
|
||||
DEFAULT_SCAN_INTERVAL = timedelta(minutes=30)
|
||||
|
||||
|
||||
def uuid4_string(value):
|
||||
"""Validate a v4 UUID in string format."""
|
||||
try:
|
||||
result = UUID(value, version=4)
|
||||
except (ValueError, AttributeError, TypeError) as error:
|
||||
raise vol.Invalid("Invalid Version4 UUID", error_message=str(error))
|
||||
|
||||
if str(result) != value.lower():
|
||||
# UUID() will create a uuid4 if input is invalid
|
||||
raise vol.Invalid("Invalid Version4 UUID")
|
||||
|
||||
return str(result)
|
||||
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema(
|
||||
{
|
||||
DOMAIN: vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_API_KEY): cv.string,
|
||||
vol.Required(CONF_API_KEY): uuid4_string,
|
||||
vol.Optional(
|
||||
CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL
|
||||
): cv.time_period,
|
||||
@@ -49,7 +65,7 @@ CONFIG_SCHEMA = vol.Schema(
|
||||
cv.positive_int, vol.Range(min=1)
|
||||
),
|
||||
vol.Optional(CONF_STATIONS, default=[]): vol.All(
|
||||
cv.ensure_list, [cv.string]
|
||||
cv.ensure_list, [uuid4_string]
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user