feat: add choices to strings to enable literal types by MaximSrour · Pull Request #1580 · ormar-orm/ormar
Summary
Enables ormar.String to preserve string literal typing when choices are provided, while keeping the underlying database column as sqlalchemy.String.
Closes #1579.
Changes
- update
ormar.Stringto project atyping.Literal[...]pydantic type when stringchoicesare provided - preserve nullable behavior for
String(..., choices=..., nullable=True)by wrapping the projected type inOptional[...] - keep normal
Stringbehavior unchanged whenchoicesare not provided - add/adjust tests for:
- runtime validation of
String(..., choices=...) - nullable string choices
- generated JSON schema for string choices fields
- runtime validation of
- update field documentation to describe
StringandString(..., choices=...)in a single section
Testing
Built and ran targetted tests for the new changes, and ran tests against the entire suite. All passing.
Installed a local version of ORMAR into a personal repository that uses it, and tested against existing type errors.

