Skip to content

Commit 3ea3df7

Browse files
laipz8200Yeuoly
authored andcommitted
refactor(validation): improve input validation logic (#10175)
1 parent b01e7d7 commit 3ea3df7

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

api/core/app/apps/base_app_generator.py

+26-19
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def _prepare_user_inputs(
7777

7878
def _validate_input(self, *, inputs: Mapping[str, Any], var: "VariableEntity"):
7979
user_input_value = inputs.get(var.variable)
80+
8081
if not user_input_value:
8182
if var.required:
8283
raise ValueError(f"{var.variable} is required in input form")
@@ -89,6 +90,7 @@ def _validate_input(self, *, inputs: Mapping[str, Any], var: "VariableEntity"):
8990
VariableEntityType.PARAGRAPH,
9091
} and not isinstance(user_input_value, str):
9192
raise ValueError(f"(type '{var.type}') {var.variable} in input form must be a string")
93+
9294
if var.type == VariableEntityType.NUMBER and isinstance(user_input_value, str):
9395
# may raise ValueError if user_input_value is not a valid number
9496
try:
@@ -98,25 +100,30 @@ def _validate_input(self, *, inputs: Mapping[str, Any], var: "VariableEntity"):
98100
return int(user_input_value)
99101
except ValueError:
100102
raise ValueError(f"{var.variable} in input form must be a valid number")
101-
if var.type == VariableEntityType.SELECT:
102-
options = var.options
103-
if user_input_value not in options:
104-
raise ValueError(f"{var.variable} in input form must be one of the following: {options}")
105-
elif var.type in {VariableEntityType.TEXT_INPUT, VariableEntityType.PARAGRAPH}:
106-
if var.max_length and len(user_input_value) > var.max_length:
107-
raise ValueError(f"{var.variable} in input form must be less than {var.max_length} characters")
108-
elif var.type == VariableEntityType.FILE:
109-
if not isinstance(user_input_value, dict) and not isinstance(user_input_value, File):
110-
raise ValueError(f"{var.variable} in input form must be a file")
111-
elif var.type == VariableEntityType.FILE_LIST:
112-
if not (
113-
isinstance(user_input_value, list)
114-
and (
115-
all(isinstance(item, dict) for item in user_input_value)
116-
or all(isinstance(item, File) for item in user_input_value)
117-
)
118-
):
119-
raise ValueError(f"{var.variable} in input form must be a list of files")
103+
104+
match var.type:
105+
case VariableEntityType.SELECT:
106+
if user_input_value not in var.options:
107+
raise ValueError(f"{var.variable} in input form must be one of the following: {var.options}")
108+
case VariableEntityType.TEXT_INPUT | VariableEntityType.PARAGRAPH:
109+
if var.max_length and len(user_input_value) > var.max_length:
110+
raise ValueError(f"{var.variable} in input form must be less than {var.max_length} characters")
111+
case VariableEntityType.FILE:
112+
if not isinstance(user_input_value, dict) and not isinstance(user_input_value, File):
113+
raise ValueError(f"{var.variable} in input form must be a file")
114+
case VariableEntityType.FILE_LIST:
115+
# if number of files exceeds the limit, raise ValueError
116+
if not (
117+
isinstance(user_input_value, list)
118+
and (
119+
all(isinstance(item, dict) for item in user_input_value)
120+
or all(isinstance(item, File) for item in user_input_value)
121+
)
122+
):
123+
raise ValueError(f"{var.variable} in input form must be a list of files")
124+
125+
if var.max_length and len(user_input_value) > var.max_length:
126+
raise ValueError(f"{var.variable} in input form must be less than {var.max_length} files")
120127

121128
return user_input_value
122129

0 commit comments

Comments
 (0)