Skip to content

Add a factory method for creating the CompositeValidatorSelector #1988

@uhfath

Description

@uhfath

Is your feature request related to a problem? Please describe.

When defining a RuleSet along with properties to be validated (like described here) the ValidationStrategy builds a CompositeValidatorSelector which then uses Any to combine IValidatorSelectors.

There are cases where it would be feasible to change Any to All since a RuleSet might define other rules for a property or there is no need to run validators not defined in a RuleSet using an explicit property.
Currently that is not possible.

Describe the solution you'd like

Something like this:

public enum ValidatorSelectorCombineMode
{
    All,
    Any,
}

When set to All the above described case will be possible.
Any is the currently default method to keep backward compatibility.

Describe alternatives you've considered

I'm willing to make a PR if this addition is welcome.

Additional Context

A sample which describes when to use this feature:

internal class ClientDetailsModelValidator : AbstractValidator<Models.ClientDetails>
{
    public ClientDetailsModelValidator()
    {
        RuleSet("name", () =>
        {
            RuleFor(m => m.Surname)
                .NotEmpty()
                .MaximumLength(50)
            ;

            RuleFor(m => m.Name)
                .NotEmpty()
                .MaximumLength(50)
            ;

            RuleFor(m => m.Patronymic)
                .MaximumLength(50)
            ;
        });

        RuleFor(m => m.Sex)
            .NotEmpty()
        ;

        RuleFor(m => m.Birthday)
            .NotEmpty()
            .LessThanOrEqualTo(DateTime.Today)
            .ExclusiveBetween(new DateTime(1900, 01, 01), new DateTime(2100, 01, 01))
        ;
    }
}

Currently it's not possible to validate a Surname property from name RuleSet separately.
Or at least I haven't found a way for it to work in a single Validate call.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions