Fix range_spec step type validation
This commit is contained in:
parent
ff3bf645fd
commit
666248ee40
3 changed files with 19 additions and 3 deletions
|
|
@ -52,7 +52,7 @@ class CreateRecordComponent(CustomComponent):
|
|||
"display_name": "Number of Fields",
|
||||
"info": "Number of fields to be added to the record.",
|
||||
"real_time_refresh": True,
|
||||
"rangeSpec": RangeSpec(min=1, max=15, step=1),
|
||||
"rangeSpec": RangeSpec(min=1, max=15, step=1, step_type="int"),
|
||||
},
|
||||
"text_key": {
|
||||
"display_name": "Text Key",
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
from typing import Literal
|
||||
|
||||
from pydantic import BaseModel, field_validator
|
||||
|
||||
|
||||
|
|
@ -5,6 +7,7 @@ class RangeSpec(BaseModel):
|
|||
min: float = -1.0
|
||||
max: float = 1.0
|
||||
step: float = 0.1
|
||||
step_type: Literal["int", "float"] = "float"
|
||||
|
||||
@field_validator("max")
|
||||
@classmethod
|
||||
|
|
@ -15,7 +18,13 @@ class RangeSpec(BaseModel):
|
|||
|
||||
@field_validator("step")
|
||||
@classmethod
|
||||
def step_must_be_positive(cls, v):
|
||||
def step_must_be_positive(cls, v, values, **kwargs):
|
||||
if v <= 0:
|
||||
raise ValueError("Step must be positive")
|
||||
if values.data["step_type"] == "int" and isinstance(v, float) and not v.is_integer():
|
||||
raise ValueError("When step_type is int, step must be an integer")
|
||||
return v
|
||||
|
||||
@classmethod
|
||||
def set_step_type(cls, step_type: Literal["int", "float"], range_spec: "RangeSpec") -> "RangeSpec":
|
||||
return cls(min=range_spec.min, max=range_spec.max, step=range_spec.step, step_type=step_type)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
from typing import Any, Callable, Optional, Union
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_serializer, field_validator, model_serializer
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_serializer, field_validator, model_serializer, model_validator
|
||||
|
||||
from langflow.field_typing.range_spec import RangeSpec
|
||||
|
||||
|
|
@ -91,6 +91,13 @@ class TemplateField(BaseModel):
|
|||
result["type"] = self.field_type
|
||||
return result
|
||||
|
||||
@model_validator(mode="after")
|
||||
def validate_model(self):
|
||||
# if field_type is int, we need to set the range_spec
|
||||
if self.field_type == "int" and self.range_spec is not None:
|
||||
self.range_spec = RangeSpec.set_step_type("int", self.range_spec)
|
||||
return self
|
||||
|
||||
@field_serializer("file_path")
|
||||
def serialize_file_path(self, value):
|
||||
return value if self.field_type == "file" else ""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue