Add the validation for empty texts.

This commit is contained in:
2025-11-16 12:58:05 +02:00
parent 59caa6a0ab
commit 0f9546462e
4 changed files with 186 additions and 1 deletions
+28 -1
View File
@@ -27,18 +27,45 @@ def validate_single_or_batch(text: Optional[str], texts: Optional[List[str]]):
}
)
if text == "" and texts is None:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail={
"error": {
"code": "INVALID_INPUT",
"message": "'text' cannot be empty or only whitespace",
"details": {}
}
}
)
if texts is not None and len(texts) == 0:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail={
"error": {
"code": "INVALID_INPUT",
"message": "texts list cannot be empty",
"message": "Texts list cannot be empty",
"details": {}
}
}
)
if texts is not None and len(texts) != 0:
# Check for empty strings within the list
for i, t in enumerate(texts):
if not isinstance(t, str) or not t.strip():
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail={
"error": {
"code": "INVALID_INPUT",
"message": f"Text item at index {i} in 'texts' cannot be empty or only whitespace",
"details": {}
}
}
)
return text if text is not None else texts