You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thanks for opening this, this is something I'd like to support.
I think we'd want to spell things a bit different to allow for custom error messages - if the callback returns false it's not clear what info the error message should contain. Instead I think we'd do something like:
defnot_in_future(x: datetime.date):
ifx>=datetime.date.today():
raiseValueError("date must be <= today")
returnxBirthday=Annotated[datetime.date, msgspec.Meta(validator=not_in_future)]
Reacted by Simon Wienhöfer, Michael, Dave Hirschfeld, Matthieu, kedod, Téva KRIEF, ♥ ck, narigama, Chris Moyer, Steve Loria and 2 more
Reacted by 叶子, Dave Hirschfeld, kedod, Eugene Gubenkov and Faolain
@jcrist Can I still assist with specifying the use cases or is this clear to you? This would be super useful to us. We're currently using __post_init__, but these validators would really help because we could skip them when initialising the class directly (needed for some tests). I hope this doesn't sound too pushy, just want to make sure this is on your radar.
Seeing as the ticket is still open, I'll take the opportunity to share a few thoughts that I've been having recently. When I opened this, I think I was enthusiastic to play with constraints and didn't give it much thought. While I was busy asking myself how to do validation with msgspec, I did not ask myself whether msgspec should do any validation at all. A recent experience has made me reconsider this.
Between a single library that did serialisation, validation and conversion poorly, and three libraries that each excelled at one of those things, I think I'd prefer the latter. This isn't a critique at msgspec. It's a reminder that extra features and extended functionality come with their own costs and challenges. So far I found myself using msgspec for serialisation alone. To be honest, I've been pretty satisfied with that. The simplicity is what caught my eye about this library in the first place. The world doesn't need another pydantic.
Between a single library that did serialisation, validation and conversion poorly, and three libraries that each excelled at one of those things, I think I'd prefer the latter.
So which two libraries that excel at validation and conversion respectively do you use?
Btw, that's literally the first sentence of the README:
msgspec is a fast serialization and validation library,
@alexei I don't see your point. Sounds a bit like the linux command line philosophy, except it just doesn't make sense to apply it here. If you need validation without dealing with JSON, just use pydantic. If you don't need the validation, just parse your objects with msgspec into plain dictionaries, without giving any struct. Otherwise, the validation - especially without adding much of a time penalty - is one of the unique selling points of this library. And if validation is already defined as being an integral part of this library, also adding customization options like presented in this issue make perfect sense.
Heyo, this would be an instant reason for me to switch to msgspec from pydantic, just wondering what the status is on something like this being implemented?
Reacted by Dave Hirschfeld, Philipp Schoneville, narigama, Aleksey, Gena Jidkiy, Eugene Gubenkov, Faolain and Oliver Batchelor
Activity
jcrist commentedon Jul 13, 2023
Thanks for opening this, this is something I'd like to support.
I think we'd want to spell things a bit different to allow for custom error messages - if the callback returns false it's not clear what info the error message should contain. Instead I think we'd do something like:
jcrist commentedon Jul 13, 2023
Also, note that your first example would currently be better expressed using a Literal or Enum type.
alexei commentedon Jul 13, 2023
I like the idea of raising
ValueError👍[-]Allow `callback` constraints[/-][+]Support custom validator functions[/+]aedify-swi commentedon Aug 18, 2023
@jcrist Can I still assist with specifying the use cases or is this clear to you? This would be super useful to us. We're currently using
__post_init__, but these validators would really help because we could skip them when initialising the class directly (needed for some tests). I hope this doesn't sound too pushy, just want to make sure this is on your radar.ml31415 commentedon Nov 4, 2023
Would be great to have that. This would also solve #425
alexei commentedon Nov 6, 2023
Seeing as the ticket is still open, I'll take the opportunity to share a few thoughts that I've been having recently. When I opened this, I think I was enthusiastic to play with constraints and didn't give it much thought. While I was busy asking myself how to do validation with
msgspec, I did not ask myself whethermsgspecshould do any validation at all. A recent experience has made me reconsider this.Between a single library that did serialisation, validation and conversion poorly, and three libraries that each excelled at one of those things, I think I'd prefer the latter. This isn't a critique at
msgspec. It's a reminder that extra features and extended functionality come with their own costs and challenges. So far I found myself usingmsgspecfor serialisation alone. To be honest, I've been pretty satisfied with that. The simplicity is what caught my eye about this library in the first place. The world doesn't need anotherpydantic.gsakkis commentedon Nov 6, 2023
So which two libraries that excel at validation and conversion respectively do you use?
Btw, that's literally the first sentence of the README:
ml31415 commentedon Nov 7, 2023
@alexei I don't see your point. Sounds a bit like the linux command line philosophy, except it just doesn't make sense to apply it here. If you need validation without dealing with JSON, just use pydantic. If you don't need the validation, just parse your objects with
msgspecinto plain dictionaries, without giving any struct. Otherwise, the validation - especially without adding much of a time penalty - is one of the unique selling points of this library. And if validation is already defined as being an integral part of this library, also adding customization options like presented in this issue make perfect sense.alexei commentedon Nov 18, 2023
@gsakkis @ml31415 thanks for your input
nathanielarking commentedon Aug 3, 2024
Heyo, this would be an instant reason for me to switch to msgspec from pydantic, just wondering what the status is on something like this being implemented?
RoTorEx commentedon Jan 21, 2025
Looking forward to this feature too!
gubenkoved commentedon Apr 1, 2025
This feature would be greatly appreciated! Looks like an obvious blindspot actually...
Faolain commentedon May 15, 2025
Any updates on this?
hermeschen1116 commentedon Sep 5, 2025
Is there any updates since 2023?
provinzkraut commentedon Apr 20, 2026
Yes! I'm currently looking into this :)
hermeschen1116 commentedon Apr 21, 2026
Thanks, looking forward for the update.