msgspec
msgspec copied to clipboard
Cannot set `min_length` for optional string filed
Description
Error while creating an object with optional filed with min_length meta.
from typing import Annotated
import msgspec
class Author(msgspec.Struct):
name: Annotated[str, msgspec.Meta(min_length=1)]
biography: Annotated[None | str, msgspec.Meta(min_length=10)]
element = {"name": "Me", "biography": None}
author = msgspec.convert(element, type=Author)
Traceback (most recent call last):
File "min_length on a str, bytes, or collection type - type typing.Annotated[None | str, msgspec.Meta(min_length=10)] is invalid
msgspec version 0.19.0
None does not have a length, is that an issue? Try to use Annonated[str, msgspec.Meta(min_length=10)] | None