chore: Refactor TextInput class to improve value validation

This commit is contained in:
ogabrielluiz 2024-06-17 10:35:24 -03:00
commit d62c3452ab

View file

@ -43,29 +43,29 @@ class StrInput(BaseInputMixin, ListableInputMixin, DatabaseLoadMixin): # noqa:
class TextInput(StrInput): class TextInput(StrInput):
input_types: list[str] = ["Data", "Message", "Text"] input_types: list[str] = ["Data", "Message", "Text"]
# @field_validator("value") @field_validator("value")
# @classmethod @classmethod
# def validate_value(cls, v: Any, _info): def validate_value(cls, v: Any, _info):
# value = None value = None
# if isinstance(v, str): if isinstance(v, str):
# value = v value = v
# elif isinstance(v, Message): elif isinstance(v, Message):
# value = v.text value = v.text
# elif isinstance(v, Data): elif isinstance(v, Data):
# if v.text_key in v.data: if v.text_key in v.data:
# value = v.data[v.text_key] value = v.data[v.text_key]
# else: else:
# keys = ", ".join(v.data.keys()) keys = ", ".join(v.data.keys())
# input_name = _info.data["name"] input_name = _info.data["name"]
# raise ValueError( raise ValueError(
# f"The input to '{input_name}' must contain the key '{v.text_key}'." f"The input to '{input_name}' must contain the key '{v.text_key}'."
# f"You can set `text_key` to one of the following keys: {keys} or set the value using another Component." f"You can set `text_key` to one of the following keys: {keys} or set the value using another Component."
# ) )
# else: else:
# raise ValueError(f"Invalid input type {type(v)}") raise ValueError(f"Invalid input type {type(v)}")
# if isinstance(value, str): if isinstance(value, str):
# return value return value
# raise ValueError(f"Invalid value type {type(value)}") raise ValueError(f"Invalid value type {type(value)}")
class MultilineInput(BaseInputMixin): class MultilineInput(BaseInputMixin):