Align valid_entity_id with new slugify (#20231)

* slug

* ensure a dot

* fix

* schema_with_slug_keys

* lint

* test
This commit is contained in:
Johann Kellerman 2019-01-21 19:45:11 +02:00 committed by Paulus Schoutsen
parent 6ca0da5c52
commit c36c708068
5 changed files with 32 additions and 17 deletions

View file

@ -319,7 +319,23 @@ def service(value):
.format(value))
def slug(value):
def schema_with_slug_keys(value_schema: Union[T, Callable]) -> Callable:
"""Ensure dicts have slugs as keys.
Replacement of vol.Schema({cv.slug: value_schema}) to prevent misleading
"Extra keys" errors from voluptuous.
"""
schema = vol.Schema({str: value_schema})
def verify(value: Dict) -> Dict:
"""Validate all keys are slugs and then the value_schema."""
for key in value.keys():
slug(key)
return schema(value)
return verify
def slug(value: Any) -> str:
"""Validate value is a valid slug."""
if value is None:
raise vol.Invalid('Slug should not be None')
@ -330,7 +346,7 @@ def slug(value):
raise vol.Invalid('invalid slug {} (try {})'.format(value, slg))
def slugify(value):
def slugify(value: Any) -> str:
"""Coerce a value to a slug."""
if value is None:
raise vol.Invalid('Slug should not be None')