Skip to content

refactor: share message text constraints between send and edit requests - #43

Merged
lesnik512 merged 2 commits into
mainfrom
refactor/shared-message-text
Oct 4, 2026
Merged

lesnik512 merged 2 commits into
mainfrom
refactor/shared-message-text

Conversation

@lesnik512

Copy link
Copy Markdown
Member

Closes #18

What changed

  • app/schemas/api.py: new module-level alias MessageText = Annotated[str, pydantic.Field(min_length=1, max_length=4000)]. SendMessageRequest.text and EditMessageRequest.text both use it, so the bounds 1 and 4000 are declared once in app/.
  • tests/test_schemas.py: tests parametrised over both request models. Each model rejects "" and a 4001-character string and accepts a 4000-character string. If the two models drift apart later, one parametrised case fails.

Why

The two request models declared the same pydantic.Field(min_length=1, max_length=4000) independently. Changing one would have left the other alone, and no test would have noticed.

A plain Annotated assignment is used instead of a PEP 695 type alias. That keeps the field inlined in the generated OpenAPI schema exactly as before.

Test-first evidence

This is a pure refactor, so a correct test cannot fail first. The new tests were written and run against the unchanged code: 7 passed. To check that the parametrisation catches drift, I temporarily changed only EditMessageRequest.text to max_length=5000 and reran: FAILED tests/test_schemas.py::test_message_request_rejects_out_of_bounds_text[over_max-EditMessageRequest] (DID NOT RAISE ValidationError), 1 failed and 6 passed. Then I reverted it and applied the refactor: 7 passed.

Verification

  • just test: 116 passed, coverage 100.00%.
  • just lint: ruff format, ruff check and ty check all clean.
  • OpenAPI: a Litestar app built with both models generates {'type': 'string', 'maxLength': 4000, 'minLength': 1} for text on both SendMessageRequest and EditMessageRequest, the same as before.
  • Existing send/edit API tests are unchanged and pass.

@lesnik512
lesnik512 merged commit 872e46a into main Oct 4, 2026
4 checks passed
@lesnik512
lesnik512 deleted the refactor/shared-message-text branch October 4, 2026 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EditMessageRequest duplicates SendMessageRequest's text constraints

1 participant