Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Provide generic field validators #85
To continue the discussion from #84 (comment):
Before release 1.1.0, the
In release 1.1.0, @philtay added a few user-facing validators:
Currently, using these validators is bit awkward because you either have to define an outer function to pass them to a field.
password = fields.Str(validate=[lambda v: validate.length(v, min=6)])
This can be improved by writing the validators as classes or function factories:
class Length(object): def __init__(self, min=None, max=None, error=None): #... def __call__(self): #...
However, before we refactor the
Should marshmallow provide a set of generic user validators?
I'm not completely opposed to the idea, but my concern is that these validators have been implemented many times in other libraries. I wonder if marshmallow should integrate with existing solutions rather than reimplement them.
Opening this up for suggestions, opinions, and discussion.
EDIT: Fix code example.
I'd like to add just one thing. It's very easy to maintain backward compatibility if we switch to class based validators.
class Url(ValidatorABC): """New url validator.""" def __init__(self, relative=False, error=None): ... def __call__(self, value): ... def url(value, relative=False, error=None): """Old function still works. Keep this for 2 versions then deprecate.""" return Url(relative, error)(value)
@philtay Yes, you are correct re: backwards-compatibility.
I've drafted a quick proof-of-concept to show how 3rd-party validators (WTForms and colander as examples) could be integrated with marshmallow: https://gist.github.com/sloria/edffc11fac7ee680e544.
I could see something like this going into our marshmallow utility library (restkit).
@sloria Sure, why not. However this is just another class based validator. The structure of the 'Converter' class is the same as I proposed. It's a good idea, a very nice addon, that fits well in the general scope of the library. In my opinion we should add it. However, it doesn't fulfill the need of having our own basic validators as well. We can't rely on third parties for this, validation is too important. It's not by chance that each library (e.g. WTForms, Colander, FormEncode, etc. etc.) implements them. I don't want to cope with their changes or bring them as a dependency of my project when the only thing I really want is to check the length of a simple string. These libraries are not just "validation libraries", but have a broader scope. Validation is just a fraction of what they do.
referenced this issue
Dec 7, 2014
True, a slight modification could make the converter a proper validator. See this revision: https://gist.github.com/sloria/edffc11fac7ee680e544/9cacc4836ee19fa456dcf6e902c98d74c8de753b
Nobody is forcing you to add any dependencies. If you only need to check the length of a string, then just write the simple callable. Once your needs grow past that, then you can choose the right tool for the job, e.g. WTForms if you need il8n, Django's built-in validators if you're using Django, etc.
Same could be said for marshmallow, which is why I am inclined to outsource implementation of validation logic. Marshmallow provides the facility for validating data, but the user can choose their batteries.
How about the following course of action for release 1.2.0:
Sure. I'll open a new pull request later today.
No. The way to go is to add your Converter validator as well. And let me add a Function validator and a Unique validator. After that you can put a hold. In this way we cover almost every possible use case. We are talking about no more than 50 lines of additional code.