mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-14 01:15:56 +08:00
refactor(validation): improve input validation logic (#10175)
This commit is contained in:
parent
1432c268a8
commit
61da0f08dd
@ -76,6 +76,7 @@ class BaseAppGenerator:
|
|||||||
|
|
||||||
def _validate_input(self, *, inputs: Mapping[str, Any], var: "VariableEntity"):
|
def _validate_input(self, *, inputs: Mapping[str, Any], var: "VariableEntity"):
|
||||||
user_input_value = inputs.get(var.variable)
|
user_input_value = inputs.get(var.variable)
|
||||||
|
|
||||||
if not user_input_value:
|
if not user_input_value:
|
||||||
if var.required:
|
if var.required:
|
||||||
raise ValueError(f"{var.variable} is required in input form")
|
raise ValueError(f"{var.variable} is required in input form")
|
||||||
@ -88,6 +89,7 @@ class BaseAppGenerator:
|
|||||||
VariableEntityType.PARAGRAPH,
|
VariableEntityType.PARAGRAPH,
|
||||||
} and not isinstance(user_input_value, str):
|
} and not isinstance(user_input_value, str):
|
||||||
raise ValueError(f"(type '{var.type}') {var.variable} in input form must be a string")
|
raise ValueError(f"(type '{var.type}') {var.variable} in input form must be a string")
|
||||||
|
|
||||||
if var.type == VariableEntityType.NUMBER and isinstance(user_input_value, str):
|
if var.type == VariableEntityType.NUMBER and isinstance(user_input_value, str):
|
||||||
# may raise ValueError if user_input_value is not a valid number
|
# may raise ValueError if user_input_value is not a valid number
|
||||||
try:
|
try:
|
||||||
@ -97,25 +99,30 @@ class BaseAppGenerator:
|
|||||||
return int(user_input_value)
|
return int(user_input_value)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise ValueError(f"{var.variable} in input form must be a valid number")
|
raise ValueError(f"{var.variable} in input form must be a valid number")
|
||||||
if var.type == VariableEntityType.SELECT:
|
|
||||||
options = var.options
|
match var.type:
|
||||||
if user_input_value not in options:
|
case VariableEntityType.SELECT:
|
||||||
raise ValueError(f"{var.variable} in input form must be one of the following: {options}")
|
if user_input_value not in var.options:
|
||||||
elif var.type in {VariableEntityType.TEXT_INPUT, VariableEntityType.PARAGRAPH}:
|
raise ValueError(f"{var.variable} in input form must be one of the following: {var.options}")
|
||||||
if var.max_length and len(user_input_value) > var.max_length:
|
case VariableEntityType.TEXT_INPUT | VariableEntityType.PARAGRAPH:
|
||||||
raise ValueError(f"{var.variable} in input form must be less than {var.max_length} characters")
|
if var.max_length and len(user_input_value) > var.max_length:
|
||||||
elif var.type == VariableEntityType.FILE:
|
raise ValueError(f"{var.variable} in input form must be less than {var.max_length} characters")
|
||||||
if not isinstance(user_input_value, dict) and not isinstance(user_input_value, File):
|
case VariableEntityType.FILE:
|
||||||
raise ValueError(f"{var.variable} in input form must be a file")
|
if not isinstance(user_input_value, dict) and not isinstance(user_input_value, File):
|
||||||
elif var.type == VariableEntityType.FILE_LIST:
|
raise ValueError(f"{var.variable} in input form must be a file")
|
||||||
if not (
|
case VariableEntityType.FILE_LIST:
|
||||||
isinstance(user_input_value, list)
|
# if number of files exceeds the limit, raise ValueError
|
||||||
and (
|
if not (
|
||||||
all(isinstance(item, dict) for item in user_input_value)
|
isinstance(user_input_value, list)
|
||||||
or all(isinstance(item, File) for item in user_input_value)
|
and (
|
||||||
)
|
all(isinstance(item, dict) for item in user_input_value)
|
||||||
):
|
or all(isinstance(item, File) for item in user_input_value)
|
||||||
raise ValueError(f"{var.variable} in input form must be a list of files")
|
)
|
||||||
|
):
|
||||||
|
raise ValueError(f"{var.variable} in input form must be a list of files")
|
||||||
|
|
||||||
|
if var.max_length and len(user_input_value) > var.max_length:
|
||||||
|
raise ValueError(f"{var.variable} in input form must be less than {var.max_length} files")
|
||||||
|
|
||||||
return user_input_value
|
return user_input_value
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user