mirror of
https://github.com/home-assistant/core.git
synced 2026-05-08 17:49:37 +01:00
Hassfest check for invalid localization placeholders (#155216)
This commit is contained in:
@@ -5,6 +5,7 @@ from __future__ import annotations
|
||||
from functools import partial
|
||||
import json
|
||||
import re
|
||||
import string
|
||||
from typing import Any
|
||||
|
||||
import voluptuous as vol
|
||||
@@ -131,10 +132,12 @@ def translation_value_validator(value: Any) -> str:
|
||||
|
||||
- prevents string with HTML
|
||||
- prevents strings with single quoted placeholders
|
||||
- prevents strings with placeholders using invalid identifiers
|
||||
- prevents combined translations
|
||||
"""
|
||||
string_value = cv.string_with_no_html(value)
|
||||
string_value = string_no_single_quoted_placeholders(string_value)
|
||||
string_value = validate_placeholders(string_value)
|
||||
if RE_COMBINED_REFERENCE.search(string_value):
|
||||
raise vol.Invalid("the string should not contain combined translations")
|
||||
if string_value != string_value.strip():
|
||||
@@ -151,6 +154,19 @@ def string_no_single_quoted_placeholders(value: str) -> str:
|
||||
return value
|
||||
|
||||
|
||||
def validate_placeholders(value: str) -> str:
|
||||
"""Validate that placeholders in translations use valid identifiers."""
|
||||
formatter = string.Formatter()
|
||||
|
||||
for _, field_name, _, _ in formatter.parse(value):
|
||||
if field_name: # skip literal text segments
|
||||
if not field_name.isidentifier():
|
||||
raise vol.Invalid(
|
||||
"placeholders must be valid identifiers ([a-zA-Z_][a-zA-Z0-9_]*)"
|
||||
)
|
||||
return value
|
||||
|
||||
|
||||
def gen_data_entry_schema(
|
||||
*,
|
||||
config: Config,
|
||||
|
||||
Reference in New Issue
Block a user