diff --git a/templates/api/models.py b/templates/api/models.py index 5a2d8f8..39b121f 100644 --- a/templates/api/models.py +++ b/templates/api/models.py @@ -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.""" diff --git a/tests/api/test_handler.py b/tests/api/test_handler.py index 4626930..225fe41 100644 --- a/tests/api/test_handler.py +++ b/tests/api/test_handler.py @@ -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