Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions templates/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@


class Item(BaseModel, populate_by_name=True, alias_generator=to_camel):
id: str = Field(description="Unique item identifier", default_factory=lambda: str(uuid4()))
name: str = Field(description="Human-readable item name")
id: str = Field(
description="Unique item identifier", default_factory=lambda: str(uuid4()), min_length=1, max_length=50
)
name: str = Field(description="Human-readable item name", min_length=1, max_length=100)

def dump(self, **kwargs: Any) -> str:
"""Dump the model to a JSON string with default settings for API responses."""
Expand Down
14 changes: 14 additions & 0 deletions tests/api/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ def test_post_item_invalid_body(mock_repo, lambda_context):
assert "errors" in body


def test_post_item_name_too_long(mock_repo, lambda_context):
"""POST /items returns 422 when the name exceeds the maximum length."""
from templates.api.handler import main

long_name = "a" * 101
event = _apigw_event("POST", "/items", body={"id": "xyz", "name": long_name})
response = main(event, lambda_context)

assert response["statusCode"] == 422
body = loads(response["body"])
assert "errors" in body
assert any(err["type"] == "string_too_long" for err in body["errors"])


def test_get_item_dynamodb_error(mock_repo, lambda_context):
"""GET /items/{id} returns 500 when the repository raises an exception."""
import templates.api.handler as handler_module
Expand Down