Reduce overhead to construct and validate entity service schema (#113920)

pull/113922/head
J. Nick Koston 2024-03-20 16:07:17 -10:00 committed by GitHub
parent e015fd2440
commit aebc95b1d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 4 deletions

View File

@ -161,15 +161,15 @@ def path(value: Any) -> str:
# https://github.com/alecthomas/voluptuous/issues/115#issuecomment-144464666
def has_at_least_one_key(*keys: Any) -> Callable[[dict], dict]:
"""Validate that at least one key exists."""
key_set = set(keys)
def validate(obj: dict) -> dict:
"""Test keys exist in dict."""
if not isinstance(obj, dict):
raise vol.Invalid("expected dictionary")
for k in obj:
if k in keys:
return obj
if not key_set.isdisjoint(obj):
return obj
expected = ", ".join(str(k) for k in keys)
raise vol.Invalid(f"must contain at least one of {expected}.")
@ -1250,6 +1250,9 @@ TARGET_SERVICE_FIELDS = {
}
_HAS_ENTITY_SERVICE_FIELD = has_at_least_one_key(*ENTITY_SERVICE_FIELDS)
def _make_entity_service_schema(schema: dict, extra: int) -> vol.Schema:
"""Create an entity service schema."""
return vol.Schema(
@ -1263,7 +1266,7 @@ def _make_entity_service_schema(schema: dict, extra: int) -> vol.Schema:
},
extra=extra,
),
has_at_least_one_key(*ENTITY_SERVICE_FIELDS),
_HAS_ENTITY_SERVICE_FIELD,
)
)