chore: Refactor TextInput class to improve value validation
This commit is contained in:
parent
2a3f095668
commit
d62c3452ab
1 changed files with 23 additions and 23 deletions
|
|
@ -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):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue