Opened 18 years ago

Closed 18 years ago

Last modified 18 years ago

#1566 closed enhancement (wontfix)

[patch] validator that validates if a specified number of validators pass

Reported by: gary.wilson@… Owned by: Adrian Holovaty
Component: Validators Version: dev
Severity: normal Keywords:
Cc: gary.wilson@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Explained in the docstring:

class SomeValidators:

    """Validates if `num_required` of given validators pass.
    
    This validator tries all given validators in order.  Once the
    `num_required` number of validators pass, validation passes without
    any further testing.  If `num_required` is not met, a validation error
    is raised with the given message.  The message is rather unspecific,
    so it's best to specify one on instantiation.
    """

    def __init__(self, num_required, validator_list=[], error_message=gettext_lazy("This field is invalid.")):

    ..snip..

One use case is for validating passwords, where one might have something like...

hasLowercase = validators.MatchesRegularExpression('[a-z]', "Password must contain at least one lowercase letter.")
hasUppercase = validators.MatchesRegularExpression('[A-Z]', "Password must contain at least one uppercase letter.")
hasNumeric = validators.MatchesRegularExpression('[0-9]', "Password must contain at least one number.")
strongPassword = validators.SomeValidators(num_required=2, validator_list=[hasLowercase, hasUppercase, hasNumeric],
                                           error_message="Password must contain at least 2 of: lowercase, uppercase, and/or numeric characters.")

class AddUserForm(formfields.Manipulator):

    def __init__(self):
        self.fields = [
            formfields.PasswordField(field_name="password", length=15, maxlength=20, is_required=True, validator_list=[strongPassword]),
        ]

This also would mean that validators.AnyValidator now becomes a special case of SomeValidators.

patch coming...

Attachments (1)

validators.diff (2.6 KB ) - added by gary.wilson@… 18 years ago.

Download all attachments as: .zip

Change History (4)

by gary.wilson@…, 18 years ago

Attachment: validators.diff added

comment:1 by anonymous, 18 years ago

Summary: validator that validates if a specified number of validators pass[patch] validator that validates if a specified number of validators pass

comment:2 by Jacob, 18 years ago

Resolution: wontfix
Status: newclosed

This is actually pretty cool... but I think it's just a bit beyond what ought to be in core.

comment:3 by anonymous, 18 years ago

Cc: gary.wilson@… added
Note: See TracTickets for help on using tickets.
Back to Top