5 ms·
Hands down, my favorite new library is schema: https://pypi.python.org/pypi/schema https://pypi.python.org/pypi/schema Here's a schema I use in production, se
by StavrosK 11y ago
Hands down, my favorite new library is schema:
https://pypi.python.org/pypi/schema https://pypi.python.org/pypi/schema
Here's a schema I use in production, see how readable it makes the parameters of the API and how quick all the validation and normalization is:
https://www.pastery.net/mhwwnv/ https://www.pastery.net/mhwwnv/
At the end, you get an object called data, and you can do data.title, data.language, etc, and be sure that everything is as you expect.
- deleted 11y ago[deleted]
- lsh 11y agoAny relation to the Clojure Schema? https://github.com/Prismatic/schema https://github.com/Prismatic/schema
- StavrosK 11y agoPossibly? It certainly looks similar.
- bpicolo 11y agoOptional being an attribute of the key rather than the value is pretty bizarre to me, I've seen that patten a couple times. Feels like the wrong way to go about it
- StavrosK 11y agoI see what you mean, but it's the key that's optional, not the value.
- bpicolo 11y agoAhh. That makes sense then. The value of the key being optional is indeed a different concept. Fwiw, this is one huge downside of Swagger APIs. Swagger supports the concept of optional keys but not optional values >.> I have yet to find a validation library that supports all the sorts of things I expect it to. One place they tend to fail is the ways they can fulfill default values. Say a field has an error and I want to just give it a default value when it's broken? That particular feature doesn't exist in any library i've found so far (for python) =/ Granted, this sort of blends into the usual "It's a validation library not a serialization library". But they all make a half-assed attempt at the other side in my experience.
- HeyImAlex 11y agoThink about it in terms of composability of validators; "having a key" is a property of the dict/object, not the value that is stored there. I feel your intuition, but experience says otherwise.
- fidget 11y agoFor those of us more dictionary oriented, there is https://pypi.python.org/pypi/voluptuous https://pypi.python.org/pypi/voluptuous (which is OK for the most part, as long as you are only trying to do validation, and nothing too crazy)
- StavrosK 11y agoHuh, voluptuous looks pretty much exactly the same as schema. Or at least a subset of it, as it doesn't have And/Or/Use, as far as I can see.
- mixmastamyk 11y agoI believe it may, perhaps analogous to its Any and All, etc.
- odonnellryan 11y agoSimilar, I've used this library with a lot of success: https://marshmallow.readthedocs.org/en/latest/ https://marshmallow.readthedocs.org/en/latest/
- aidos 11y agoI've tried truckloads of Python serialisation libs over the last few years and marshmallow is the one that finally makes me feel like I don't need to look for another one.
- odonnellryan 11y agoIt's very easy and does everything I've needed it to!
- Walkman 11y agoMarshmallow is also my favourite lib of the year. Pyramid guys love it too! The developer is responsive, and the library moves really fast!
- 11y ago
- deleted 11y ago[deleted]
- shinryuu 11y agoThere is also JSON-Schema protocol: http://json-schema.org/implementations.html http://json-schema.org/implementations.html curious how schema may be superior :).
- lqdc13 11y agoYup, this one is way easier to use and very cross-language compatible.
- deleted 11y ago[deleted]
- pbowyer 11y ago> Here's a schema I use in production, see how readable it makes the parameters of the API and how quick all the validation and normalization is: https://www.pastery.net/mhwwnv/ https://www.pastery.net/mhwwnv/ Thank you so much for providing this example. I couldn't grok what schema did, and your code made it make sense. If people want to provide examples for the other libraries in this thread, you'll be popular :)
- noir_lord 11y agoI like Schema and Voluptuous enough that as a polyglot who mostly works in PHP I'm working on a PHP version at the moment.
- tenfingers 11y agoAnyone that have been using schema after using 'rx' (https://pypi.python.org/pypi/Rx/ https://pypi.python.org/pypi/Rx/)? I generally like the simplicity of Rx and the fact that's language agnostic (I've used it with both json and yaml and other serialization libraries), with schemas themselves being written both systems. However, the lacking documentation has always been a problem.
- reinhardt 11y agoIn similar vein is valideer: https://pypi.python.org/pypi/valideer https://pypi.python.org/pypi/valideer
- jgalt212 11y agoWhat's the difference between these sort of libraries and an ORM? Don't ORM's provide built in validation and serialization?
- taha_jahangir 11y agoThey are useful when you want backend-independent orm (say you want to save your objects as JSON in ceph in deployment, and in sqlite in development)
- jgalt212 11y agoI thought most ORMs were back-end independent. Isn't that 1/2 of their value proposition? The other half being accessing a persistence layer in native code.