5 ms·
You could have a look at Marshmallow perhaps? I use it to great effect with marshmallow-dataclass so I don't have to define the boiler-plate data classes separa
by zo1 5y ago
You could have a look at Marshmallow perhaps? I use it to great effect with marshmallow-dataclass so I don't have to define the boiler-plate data classes separately to the validation/schema. Essentially the dataclass (with type-hints) becomes the data-validation specification.
Links:
https://pypi.org/project/marshmallow-dataclass/ https://pypi.org/project/marshmallow-dataclass/
https://pypi.org/project/marshmallow/ https://pypi.org/project/marshmallow/
- andrewingram 5y agoThanks, I've had a look at Marshmallow in the past, but it's still doing the extra bits of dealing with the extra bits of serialization, it never really felt all that dissimilar to Pydantic. What I've really been looking for something that I can just use to check a data structure i'm passing in to a function call is valid. One use case is related to state machines, where I'm providing some data along with a transition name, and want to make sure the combination of transition + data is valid.
- nerdponx 5y agoDesign-by-contract, perhaps? There are a couple libraries like this currently, and you could pretty easily write your own framework for it. Also, Attrs and Pydantic both support arbitrary additional validation on fields.
- radus 5y agoPedantic has a validate_arguments decorator that while in beta, I think does what you want: https://pydantic-docs.helpmanual.io/usage/validation_decorator/ https://pydantic-docs.helpmanual.io/usage/validation_decorat...
- bilboa 5y agoSounds like you want jsonschema.
- nkconway 5y agoI have used a combo of attrs and fastjsonschema to do dataclass validation on instance construction. It works great for a python stack and you can generate some clean looking code if you have a reusable data class package
- nerdponx 5y agoWorth pointing out Desert, which generates a Marshmallow schema from an Attrs class. You could then integrate this with Apispec or Marshmallow-Jsonschema. https://pypi.org/project/desert/ https://pypi.org/project/desert/ https://pypi.org/project/marshmallow-jsonschema/ https://pypi.org/project/marshmallow-jsonschema/ https://pypi.org/project/apispec/ https://pypi.org/project/apispec/
- JPKab 5y agoI use marshmallow-dataclass in an API client lib I've been continuously working on for a while for work. It's great stuff, but I'm always looking for alternatives.