(views) be a bit more permissive for indexable document content

In indexation view, raise a validation error only when both the
title & content of a document are empty.

Signed-off-by: Fabre Florian <ffabre@hybird.org>
This commit is contained in:
Fabre Florian
2025-08-13 13:52:52 +02:00
committed by Florian Fabre
parent c9b04e0281
commit f93515d70b
3 changed files with 58 additions and 3 deletions
+9 -3
View File
@@ -23,11 +23,11 @@ class DocumentSchema(BaseModel):
"""Schema for validating the documents submitted to our API for indexing"""
id: UUID4
title: Annotated[str, Field(max_length=300)]
title: Annotated[str, Field(max_length=300, min_length=0)]
depth: Annotated[int, Field(ge=0)]
path: Annotated[str, Field(max_length=300)]
numchild: Annotated[int, Field(ge=0)]
content: str
content: Annotated[str, Field(min_length=0)]
created_at: AwareDatetime
updated_at: AwareDatetime
size: Annotated[int, Field(ge=0, le=100 * 1024**3)] # File size limited to 100GB
@@ -56,6 +56,12 @@ class DocumentSchema(BaseModel):
raise ValueError(f"{info.field_name} must be earlier than now")
return value
@model_validator(mode="after")
def check_empty_content(self):
if not self.title and not self.content:
raise ValueError('Either title or content should have at least 1 character')
return self
@model_validator(mode="after")
def check_update_at_after_created_at(self):
"""Date and time of last modification should be later than date and time of creation"""
@@ -83,7 +89,7 @@ class SearchQueryParametersSchema(BaseModel):
q: str
services: Union[str, List[str], None] = Field(default_factory=list)
visited: List[str] = Field(default_factory=list)
visited: Union[List[str], None] = Field(default_factory=list)
reach: Optional[enums.ReachEnum] = None
order_by: Optional[Literal[enums.ORDER_BY_OPTIONS]] = Field(default=enums.RELEVANCE)
order_direction: Optional[Literal["asc", "desc"]] = Field(default="desc")
@@ -334,3 +334,32 @@ def test_api_documents_index_bulk_datetime_future(field):
for i in [1, 2]:
assert responses[i]["status"] == "valid"
def test_api_documents_index_empty_content_check():
"""Test bulkk document indexing with both empty title & content."""
service = factories.ServiceFactory(name="test-service")
documents = factories.DocumentSchemaFactory.build_batch(3)
documents[0]['content'] = ''
documents[0]['title'] = ''
response = APIClient().post(
"/api/v1.0/documents/index/",
documents,
HTTP_AUTHORIZATION=f"Bearer {service.token:s}",
format="json",
)
assert response.status_code == 400
responses = response.json()
assert responses[0]["status"] == "error"
assert len(responses[0]["errors"]) == 1
assert (
responses[0]["errors"][0]["msg"]
== "Value error, Either title or content should have at least 1 character"
)
assert responses[0]["errors"][0]["type"] == "value_error"
for i in [1, 2]:
assert responses[i]["status"] == "valid"
@@ -298,3 +298,23 @@ def test_api_documents_index_single_datetime_future(field):
assert response.status_code == 400
assert response.data[0]["msg"] == f"Value error, {field:s} must be earlier than now"
assert response.data[0]["type"] == "value_error"
def test_api_documents_index_empty_content_check():
"""Test document indexing with both empty title & content."""
service = factories.ServiceFactory(name="test-service")
document = factories.DocumentSchemaFactory.build()
document['content'] = ''
document['title'] = ''
response = APIClient().post(
"/api/v1.0/documents/index/",
document,
HTTP_AUTHORIZATION=f"Bearer {service.token:s}",
format="json",
)
assert response.status_code == 400
assert response.data[0]["msg"] == "Value error, Either title or content should have at least 1 character"
assert response.data[0]["type"] == "value_error"