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):
input_types: list[str] = ["Data", "Message", "Text"]
# @field_validator("value")
# @classmethod
# def validate_value(cls, v: Any, _info):
# value = None
# if isinstance(v, str):
# value = v
# elif isinstance(v, Message):
# value = v.text
# elif isinstance(v, Data):
# if v.text_key in v.data:
# value = v.data[v.text_key]
# else:
# keys = ", ".join(v.data.keys())
# input_name = _info.data["name"]
# raise ValueError(
# 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."
# )
# else:
# raise ValueError(f"Invalid input type {type(v)}")
# if isinstance(value, str):
# return value
# raise ValueError(f"Invalid value type {type(value)}")
@field_validator("value")
@classmethod
def validate_value(cls, v: Any, _info):
value = None
if isinstance(v, str):
value = v
elif isinstance(v, Message):
value = v.text
elif isinstance(v, Data):
if v.text_key in v.data:
value = v.data[v.text_key]
else:
keys = ", ".join(v.data.keys())
input_name = _info.data["name"]
raise ValueError(
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."
)
else:
raise ValueError(f"Invalid input type {type(v)}")
if isinstance(value, str):
return value
raise ValueError(f"Invalid value type {type(value)}")
class MultilineInput(BaseInputMixin):