Opened 9 years ago

Closed 9 years ago

Last modified 8 years ago

#1566 closed enhancement (wontfix)

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

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

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@… 9 years ago.

Download all attachments as: .zip

Change History (4)

Changed 9 years ago by gary.wilson@…

comment:1 Changed 9 years ago by anonymous

  • Summary changed from validator that validates if a specified number of validators pass to [patch] validator that validates if a specified number of validators pass

comment:2 Changed 9 years ago by jacob

  • Resolution set to wontfix
  • Status changed from new to closed

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

comment:3 Changed 9 years ago by anonymous

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