mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Make translations keys check hassfest more strict (#85221)
This commit is contained in:
parent
209c47383d
commit
3cb56211f8
@ -21,6 +21,7 @@ REQUIRED = 1
|
|||||||
REMOVED = 2
|
REMOVED = 2
|
||||||
|
|
||||||
RE_REFERENCE = r"\[\%key:(.+)\%\]"
|
RE_REFERENCE = r"\[\%key:(.+)\%\]"
|
||||||
|
RE_TRANSLATION_KEY = re.compile(r"^(?!.+[_-]{2})(?![_-])[a-z0-9-_]+(?<![_-])$")
|
||||||
|
|
||||||
# Only allow translation of integration names if they contain non-brand names
|
# Only allow translation of integration names if they contain non-brand names
|
||||||
ALLOW_NAME_TRANSLATION = {
|
ALLOW_NAME_TRANSLATION = {
|
||||||
@ -86,9 +87,7 @@ def find_references(
|
|||||||
find_references(value, f"{prefix}::{key}", found)
|
find_references(value, f"{prefix}::{key}", found)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
match = re.match(RE_REFERENCE, value)
|
if match := re.match(RE_REFERENCE, value):
|
||||||
|
|
||||||
if match:
|
|
||||||
found.append({"source": f"{prefix}::{key}", "ref": match.groups()[0]})
|
found.append({"source": f"{prefix}::{key}", "ref": match.groups()[0]})
|
||||||
|
|
||||||
|
|
||||||
@ -106,10 +105,13 @@ def removed_title_validator(
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
def lowercase_validator(value: str) -> str:
|
def translation_key_validator(value: str) -> str:
|
||||||
"""Validate value is lowercase."""
|
"""Validate value is valid translation key."""
|
||||||
if value.lower() != value:
|
if RE_TRANSLATION_KEY.match(value) is None:
|
||||||
raise vol.Invalid("Needs to be lowercase")
|
raise vol.Invalid(
|
||||||
|
f"Invalid translation key '{value}', need to be [a-z0-9-_]+ and"
|
||||||
|
" cannot start or end with a hyphen or underscore."
|
||||||
|
)
|
||||||
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
@ -221,7 +223,9 @@ def gen_strings_schema(config: Config, integration: Integration) -> vol.Schema:
|
|||||||
vol.Optional("trigger_subtype"): {str: cv.string_with_no_html},
|
vol.Optional("trigger_subtype"): {str: cv.string_with_no_html},
|
||||||
},
|
},
|
||||||
vol.Optional("state"): cv.schema_with_slug_keys(
|
vol.Optional("state"): cv.schema_with_slug_keys(
|
||||||
cv.schema_with_slug_keys(str, slug_validator=lowercase_validator),
|
cv.schema_with_slug_keys(
|
||||||
|
cv.string_with_no_html, slug_validator=translation_key_validator
|
||||||
|
),
|
||||||
slug_validator=vol.Any("_", cv.slug),
|
slug_validator=vol.Any("_", cv.slug),
|
||||||
),
|
),
|
||||||
vol.Optional("state_attributes"): cv.schema_with_slug_keys(
|
vol.Optional("state_attributes"): cv.schema_with_slug_keys(
|
||||||
@ -229,19 +233,22 @@ def gen_strings_schema(config: Config, integration: Integration) -> vol.Schema:
|
|||||||
{
|
{
|
||||||
vol.Optional("name"): str,
|
vol.Optional("name"): str,
|
||||||
vol.Optional("state"): cv.schema_with_slug_keys(
|
vol.Optional("state"): cv.schema_with_slug_keys(
|
||||||
str, slug_validator=lowercase_validator
|
cv.string_with_no_html,
|
||||||
|
slug_validator=translation_key_validator,
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
slug_validator=lowercase_validator,
|
slug_validator=translation_key_validator,
|
||||||
),
|
),
|
||||||
slug_validator=vol.Any("_", cv.slug),
|
slug_validator=vol.Any("_", cv.slug),
|
||||||
),
|
),
|
||||||
vol.Optional("system_health"): {
|
vol.Optional("system_health"): {
|
||||||
vol.Optional("info"): {str: cv.string_with_no_html}
|
vol.Optional("info"): cv.schema_with_slug_keys(
|
||||||
|
cv.string_with_no_html, slug_validator=translation_key_validator
|
||||||
|
),
|
||||||
},
|
},
|
||||||
vol.Optional("config_panel"): cv.schema_with_slug_keys(
|
vol.Optional("config_panel"): cv.schema_with_slug_keys(
|
||||||
cv.schema_with_slug_keys(
|
cv.schema_with_slug_keys(
|
||||||
cv.string_with_no_html, slug_validator=lowercase_validator
|
cv.string_with_no_html, slug_validator=translation_key_validator
|
||||||
),
|
),
|
||||||
slug_validator=vol.Any("_", cv.slug),
|
slug_validator=vol.Any("_", cv.slug),
|
||||||
),
|
),
|
||||||
@ -273,10 +280,16 @@ def gen_strings_schema(config: Config, integration: Integration) -> vol.Schema:
|
|||||||
vol.Optional("state_attributes"): {
|
vol.Optional("state_attributes"): {
|
||||||
str: {
|
str: {
|
||||||
vol.Optional("name"): cv.string_with_no_html,
|
vol.Optional("name"): cv.string_with_no_html,
|
||||||
vol.Optional("state"): {str: cv.string_with_no_html},
|
vol.Optional("state"): cv.schema_with_slug_keys(
|
||||||
|
cv.string_with_no_html,
|
||||||
|
slug_validator=translation_key_validator,
|
||||||
|
),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
vol.Optional("state"): {str: cv.string_with_no_html},
|
vol.Optional("state"): cv.schema_with_slug_keys(
|
||||||
|
cv.string_with_no_html,
|
||||||
|
slug_validator=translation_key_validator,
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -352,7 +365,7 @@ def gen_platform_strings_schema(config: Config, integration: Integration) -> vol
|
|||||||
return vol.Schema(
|
return vol.Schema(
|
||||||
{
|
{
|
||||||
vol.Optional("state"): cv.schema_with_slug_keys(
|
vol.Optional("state"): cv.schema_with_slug_keys(
|
||||||
cv.schema_with_slug_keys(str, slug_validator=lowercase_validator),
|
cv.schema_with_slug_keys(str, slug_validator=translation_key_validator),
|
||||||
slug_validator=device_class_validator,
|
slug_validator=device_class_validator,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user