3 ms·
In our startup we use marshmallow [1] to validate and make REST API type aware. So marshmallow models validates the data types based on validation rules for req
by dragonsh 7y ago
In our startup we use marshmallow [1] to validate and make REST API type aware. So marshmallow models validates the data types based on validation rules for request/response.
In place where we need client to be able to define the validation the request/response will consist of data, validation json schema. In this case use jsonschema validator [2] [3].
So for every json request/request data, it's passed through a marshmallow model which validates the json. In some cases we use jsonschema validator when we have dynamic json data with schema definition for validations.
For response the function return value pass through a marshmallow model. We are moving towards making all of internal functions/methods with type annotations to generate documentation using Sphinx [4] plugin.
We are not Instagram but are very happy with it and can replace flask library with bottle or other wsgi compatible framework and it will still work.
[1] https://marshmallow.readthedocs.io/en/stable/ https://marshmallow.readthedocs.io/en/stable/
[2] https://python-jsonschema.readthedocs.io/ https://python-jsonschema.readthedocs.io/
[3] https://pydantic-docs.helpmanual.io/ https://pydantic-docs.helpmanual.io/
[4] https://pypi.org/project/sphinx-autodoc-typehints/#data https://pypi.org/project/sphinx-autodoc-typehints/#data
- henryfjordan 7y agoMarshmallow is good but it can be slow. We also use it over a Flask API for input/output serialization and in the worse cases it can take a significant amount of time if the objects are large enough (maybe a hundred milliseconds). We also use the Marshmallow models in conjunction with a project called `flask-apispec` by the same authors to generate Swagger docs. I've wanted to explore using the Typing module to replace Marshmallow since it started making the rounds to see if it results in better performance, but haven't had a chance. I would have liked to see Instagram release a library to go with this blog post so I don't have to do as much legwork.
- j88439h84 7y ago> Toasted Marshmallow implements a JIT for marshmallow that speeds up dumping objects 10-25X (depending on your schema). https://github.com/lyft/toasted-marshmallow https://github.com/lyft/toasted-marshmallow
- dragonsh 7y agoWe are exploring using Python data classes and typehints directly. This way can remove all dependencies and rely on standard library. As first step we are exploring marshmallow with data classes like the way we are dealing with jsonschema using pydantic. In our product we prefer to use as little 3rd party packages as possible and rely on standard library. When we want to use 3rd party package we look at the code and if my team can support and enhance it then only we use, except in some case where package is better than standard library like requests.