question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a factory method for creating the CompositeValidatorSelector

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
uhfathcommented, Aug 9, 2022

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.

What does the validate call look like @uhfath ? Just curious about the use case here.

Something along the lines:

private ValidationContext<object> BuildValidationContext(object model, params string[] properties) =>
    ValidationContext<object>.CreateWithOptions(model, opts =>
    {
        if (!properties.IsEmpty())
        {
            opts.IncludeProperties(properties);
        }

        if (!string.IsNullOrWhiteSpace(RuleSet))
        {
            opts.IncludeRuleSets(RuleSet);
        }

        if (!RuleSets.IsEmpty())
        {
            opts.IncludeRuleSets(RuleSets.ToArray());
        }

        if (UseNotInRuleSet)
        {
            opts.IncludeRulesNotInRuleSet();
        }

        opts.SetSelectorCombineMode(FluentValidation.Internal.ValidatorSelectorCombineMode.All);
    });

//---------------------------

    var property = eventArgs.FieldIdentifier.FieldName;
    var context = BuildValidationContext(eventArgs.FieldIdentifier.Model, property);
    var validator = (IValidator)ServiceProvider.GetRequiredService(MakeValidatorType(eventArgs.FieldIdentifier.Model.GetType()));
    var result = validator.Validate(context);
1reaction
JeremySkinnercommented, Aug 8, 2022

I’ve pushed out an 11.2 release which contains the ValidatorOptions.Global.ValidatorSelectors.CompositeValidatorSelectorFactory

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add a factory method for creating the ...
We could add another factory method for creating the CompositeValidatorSelector , so you can then swap out just the composite selector without ...
Read more >
java - Make factory method more dynamic
I've been calling this a strategy pattern... – OneCricketeer · In the first example, why is there a for loop iteration over PizzaType?...
Read more >
Factory method for designing pattern
The Factory Method pattern is used to create objects without specifying the exact class of object that will be created. This pattern is...
Read more >
Factory Method
Factory Method is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the...
Read more >
The Factory Method Pattern and Its Implementation in Python
Factory Method is a creational design pattern used to create concrete implementations of a common interface. It separates the process of creating an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found