form: add reserved "id" validator

This commit is contained in:
axolotle 2023-04-17 20:03:35 +02:00
parent 774b11cbbe
commit bec34b92b0
2 changed files with 26 additions and 26 deletions

View file

@ -455,31 +455,6 @@ class ConfigPanel:
"config_unknown_filter_key", filter_key=self.filter_key "config_unknown_filter_key", filter_key=self.filter_key
) )
# List forbidden keywords from helpers and sections toml (to avoid conflict)
forbidden_keywords = [
"old",
"app",
"changed",
"file_hash",
"binds",
"types",
"formats",
"getter",
"setter",
"short_setting",
"type",
"bind",
"nothing_changed",
"changes_validated",
"result",
"max_progression",
]
forbidden_keywords += format_description["sections"]
for _, _, option in self._iterate():
if option["id"] in forbidden_keywords:
raise YunohostError("config_forbidden_keyword", keyword=option["id"])
return self.config return self.config
def _get_default_values(self): def _get_default_values(self):

View file

@ -265,7 +265,26 @@ FORBIDDEN_READONLY_TYPES = {
OptionType.user, OptionType.user,
OptionType.group, OptionType.group,
} }
FORBIDDEN_KEYWORDS = {
"old",
"app",
"changed",
"file_hash",
"binds",
"types",
"formats",
"getter",
"setter",
"short_setting",
"type",
"bind",
"nothing_changed",
"changes_validated",
"result",
"max_progression",
"properties",
"defaults",
}
Context = dict[str, Any] Context = dict[str, Any]
Translation = Union[dict[str, str], str] Translation = Union[dict[str, str], str]
@ -298,6 +317,12 @@ class BaseOption(BaseModel):
del schema["description"] del schema["description"]
schema["additionalProperties"] = False schema["additionalProperties"] = False
@validator("id", pre=True)
def check_id_is_not_forbidden(cls, value: str) -> str:
if value in FORBIDDEN_KEYWORDS:
raise ValueError(m18n.n("config_forbidden_keyword", keyword=value))
return value
# FIXME Legacy, is `name` still needed? # FIXME Legacy, is `name` still needed?
@validator("name", pre=True, always=True) @validator("name", pre=True, always=True)
def apply_legacy_name(cls, value: Union[str, None], values: Values) -> str: def apply_legacy_name(cls, value: Union[str, None], values: Values) -> str: