-
Notifications
You must be signed in to change notification settings - Fork 107
Open
Description
Description
I have a struct that wants a str
input that has whitespace stripped, is in all lowercase, is below a max length, is above a minimum length, and matches a regex pattern. This repository already has an issue for stripping whitespace through msgspec.Meta
, that being #782. This is possible through __post_init__
, but I don't see why this couldn't just be on Meta
instead. Plus, using __post_init__
means more boilerplate. Here's an example of what this might look like in code:
from msgspec import Struct, Meta, json
from typing import Annotated
class Repository(Struct, kw_only=True):
owner: str
name: str
prefix: Annotated[str, Meta(min_length=3, max_length=30, to_lower=True, pattern=r"^[\w\-]+$")]
decoder = json.Decoder(Repository)
raw = '{"owner": "jcrist", "name": "msgspec", "prefix": "Msg-Spec"}'
repo = decoder.decode(raw)
print(repo.prefix)
# msg-spec
Versus what has to be done now (not too bad but still unnecessary boilerplate):
from msgspec import Struct, Meta, json
from typing import Annotated
class Repository(Struct, kw_only=True):
owner: str
name: str
prefix: Annotated[str, Meta(min_length=3, max_length=30, pattern=r"^[\w\-]+$")]
def __post_init__(self) -> None:
self.prefix = self.prefix.lower()
decoder = json.Decoder(Repository)
raw = '{"owner": "jcrist", "name": "msgspec", "prefix": "Msg-Spec"}'
repo = decoder.decode(raw)
print(repo.prefix)
# msg-spec
Metadata
Metadata
Assignees
Labels
No labels