New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide generic field validators #85

Closed
sloria opened this Issue Dec 7, 2014 · 6 comments

Comments

Projects
None yet
2 participants
@sloria
Member

sloria commented Dec 7, 2014

To continue the discussion from #84 (comment):

Before release 1.1.0, the validate module was used mainly for internal use, i.e. as helpers for the URL and Email fields. Users were not expected to use the functions directly.

In release 1.1.0, @philtay added a few user-facing validators: ranging, equal, regexp, and predicate.

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 validate module with potentially breaking changes, we need to decide whether marshmallow should include these user validators in the first place.

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.

@philtay raised his concerns about integrating with other libraries in #84 (comment):

...every library raises its own exception and, in general, does things in a slightly different way. Moral of the story: we have to provide at least a minimum set of generic essential validators.

Opening this up for suggestions, opinions, and discussion.

EDIT: Fix code example.

@philtay

This comment has been minimized.

Contributor

philtay commented Dec 7, 2014

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)
@sloria

This comment has been minimized.

Member

sloria commented Dec 7, 2014

@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).

@philtay

This comment has been minimized.

Contributor

philtay commented Dec 7, 2014

@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.
If you wanna go ahead in this class-based direction (and I strongly suggest to do so), I can implement the class version of the validators we currently have. Then you will add your Converter validator(s) and I will add 2 or 3 more basic validators. After this we will be strongly positioned as a deserialization library as well IMO.

@sloria

This comment has been minimized.

Member

sloria commented Dec 8, 2014

However this is just another class based validator.

True, a slight modification could make the converter a proper validator. See this revision: https://gist.github.com/sloria/edffc11fac7ee680e544/9cacc4836ee19fa456dcf6e902c98d74c8de753b

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.

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.

These libraries are not just "validation libraries", but have a broader scope. Validation is just a fraction of what they do.

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:

  • Rewrite the current validators as class-based validators. Mark the backward-compatible functions for PendingDeprecation.
  • Put a hold on adding new validators until there is evidence that they meet an unmet need.
@philtay

This comment has been minimized.

Contributor

philtay commented Dec 8, 2014

Rewrite the current validators as class-based validators. Mark the backward-compatible functions for PendingDeprecation.

Sure. I'll open a new pull request later today.

Put a hold on adding new validators until there is evidence that they meet an unmet need.

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.

@sloria sloria referenced this issue Dec 10, 2014

Closed

Class validators #92

@sloria sloria added this to the 1.2.0 milestone Dec 21, 2014

@sloria

This comment has been minimized.

Member

sloria commented Dec 21, 2014

The class-based validators have been added for 1.2, so closing this issue.

@sloria sloria closed this Dec 21, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment