refactor: Add MultilineMixin to StrInput for multiline input support and adapt validation
This commit is contained in:
parent
01027cb7a3
commit
599ef9cc73
2 changed files with 12 additions and 4 deletions
|
|
@ -4,6 +4,8 @@ from typing import Annotated, Any, Optional
|
||||||
from pydantic import BaseModel, ConfigDict, Field, PlainSerializer, field_validator, model_serializer
|
from pydantic import BaseModel, ConfigDict, Field, PlainSerializer, field_validator, model_serializer
|
||||||
|
|
||||||
from langflow.field_typing.range_spec import RangeSpec
|
from langflow.field_typing.range_spec import RangeSpec
|
||||||
|
from langflow.inputs.validators import CoalesceBool
|
||||||
|
from langflow.inputs.validators import CoalesceBool
|
||||||
|
|
||||||
|
|
||||||
class FieldTypes(str, Enum):
|
class FieldTypes(str, Enum):
|
||||||
|
|
@ -123,3 +125,7 @@ class RangeMixin(BaseModel):
|
||||||
class DropDownMixin(BaseModel):
|
class DropDownMixin(BaseModel):
|
||||||
options: Optional[list[str]] = None
|
options: Optional[list[str]] = None
|
||||||
"""List of options for the field. Only used when is_list=True. Default is an empty list."""
|
"""List of options for the field. Only used when is_list=True. Default is an empty list."""
|
||||||
|
|
||||||
|
|
||||||
|
class MultilineMixin(BaseModel):
|
||||||
|
multiline: CoalesceBool = True
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
from typing import Any, AsyncIterator, Callable, Iterator, Optional, Union
|
from typing import Any, AsyncIterator, Callable, Iterator, Optional, Union
|
||||||
|
|
||||||
|
from loguru import logger
|
||||||
from pydantic import Field, field_validator
|
from pydantic import Field, field_validator
|
||||||
|
|
||||||
from langflow.inputs.validators import CoalesceBool
|
from langflow.inputs.validators import CoalesceBool
|
||||||
|
|
@ -13,6 +14,7 @@ from .input_mixin import (
|
||||||
FieldTypes,
|
FieldTypes,
|
||||||
FileMixin,
|
FileMixin,
|
||||||
ListableInputMixin,
|
ListableInputMixin,
|
||||||
|
MultilineMixin,
|
||||||
RangeMixin,
|
RangeMixin,
|
||||||
SerializableFieldTypes,
|
SerializableFieldTypes,
|
||||||
)
|
)
|
||||||
|
|
@ -49,12 +51,11 @@ class PromptInput(BaseInputMixin, ListableInputMixin):
|
||||||
|
|
||||||
|
|
||||||
# Applying mixins to a specific input type
|
# Applying mixins to a specific input type
|
||||||
class StrInput(BaseInputMixin, ListableInputMixin, DatabaseLoadMixin): # noqa: F821
|
class StrInput(BaseInputMixin, ListableInputMixin, DatabaseLoadMixin, MultilineMixin):
|
||||||
field_type: Optional[SerializableFieldTypes] = FieldTypes.TEXT
|
field_type: Optional[SerializableFieldTypes] = FieldTypes.TEXT
|
||||||
load_from_db: CoalesceBool = False
|
load_from_db: CoalesceBool = False
|
||||||
"""Defines if the field will allow the user to open a text editor. Default is False."""
|
"""Defines if the field will allow the user to open a text editor. Default is False."""
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _validate_value(v: Any, _info):
|
def _validate_value(v: Any, _info):
|
||||||
"""
|
"""
|
||||||
Validates the given value and returns the processed value.
|
Validates the given value and returns the processed value.
|
||||||
|
|
@ -70,7 +71,8 @@ class StrInput(BaseInputMixin, ListableInputMixin, DatabaseLoadMixin): # noqa:
|
||||||
ValueError: If the value is not of a valid type or if the input is missing a required key.
|
ValueError: If the value is not of a valid type or if the input is missing a required key.
|
||||||
"""
|
"""
|
||||||
if not isinstance(v, str) and v is not None:
|
if not isinstance(v, str) and v is not None:
|
||||||
raise ValueError(f"Invalid value type {type(v)}")
|
if _info.data.get("input_types") and v.__class__.__name__ not in _info.data.get("input_types"):
|
||||||
|
logger.warning(f"Invalid value type {type(v)}")
|
||||||
return v
|
return v
|
||||||
|
|
||||||
@field_validator("value")
|
@field_validator("value")
|
||||||
|
|
@ -160,7 +162,7 @@ class TextInput(StrInput):
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
class MultilineInput(BaseInputMixin):
|
class MultilineInput(BaseInputMixin, MultilineMixin):
|
||||||
"""
|
"""
|
||||||
Represents a multiline input field.
|
Represents a multiline input field.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue