17

I am developing a small RESTful web application in python and using JSON as interface. The application accepts JSON data and needs to validate them against a schema. The schema may be very strict or flexible depending on the request. Also, the application may need to validate user objects at a later improvement.

I have found some suggestions in stack overflow as well as at other websites. Those include

But could not find a proper comparison.

In terms of simplicity, flexibility and extend-ability, which one do you think should I choose?

Cerberus seems to be very flexible and extend-ability seems very good from their documentation and examples. But I don't know about the other libraries.

EDIT 1: For now the application's needs are pretty simple and a basic schema validation with scope for adding custom types and custom validation rules will suffice. So if all these libraries offer the basic requirements, I would go for the one that is simplest to use. Also I am open to other suggestions if any.

Hoping to find some help.

bergercookie
  • 2,542
  • 1
  • 30
  • 38
Abhishek Dey
  • 175
  • 1
  • 6
  • I think some more information is required. There are too many data validation packages to count and I think the choice depends on their advanced features, not their basic schema validation features (https://github.com/keleshev/schema, http://docs.pylonsproject.org/projects/colander/en/latest/index.html are also valid choices). I use jsonschema a lot and it's very simple. What exactly it is you're looking for that will not just allow you to try one of them with a good enough abstraction that it will be easy to replace when needed? – nir0s Mar 07 '17 at 06:53
  • @nir0s For now the basic schema validation will suffice with a scope of adding custom types and validation rules (for example validating valid urls, IPv4 addresses or IPv6 addresses etc. Also, validation rule may depend on the value of another key in the same input, for example {'user': 'some user', 'sex': 'male', 'age': age} in this schema, the valid age may change depending on the sex). And data structures can be nested as well. So if all the libraries offer these, i can opt for the simplest. – Abhishek Dey Mar 07 '17 at 07:05
  • This question has an issue thread on github cerberus **See:** https://github.com/pyeve/cerberus/issues/254 – dreftymac Dec 11 '18 at 00:58

1 Answers1

9

I would take Cerberus then if only for its wonderful documentation. It allows to set dependencies according to certain conditions (http://docs.python-cerberus.org/en/stable/validation-rules.html#dependencies) and is certainly as simple for the basic schema validation needs you're referring to as jsonschema, voluptuous, etc..

Nested fields are supported also (in all libraries i'm familiar with) and their customization docs are amazing: http://docs.python-cerberus.org/en/stable/customize.html

That being said, I'm not basing this answer on using all three proposed packages but rather on using jsonschema extensively and reading enough documentation and recommendations on the other two.

nir0s
  • 1,151
  • 7
  • 17
  • Yes, to me also, cerberus documentation is wonderful and they have all my needs covered. Looks like going with this one for now. Thanks for taking time to answer the question. – Abhishek Dey Mar 07 '17 at 09:47
  • each package has its own advantages, for example voluptuous doesn't allow extra fields (in case you don't allow it explicitly) – Eli Borodach Jan 20 '20 at 13:27