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
Add a factory method for creating the CompositeValidatorSelector #1988
Comments
Hi, thanks for the suggestion. This isn't something that we'll be including in the core library, but if you want to implement this in your own project you can do so by writing a custom The design philosophy behind many of FluentValidation's features is generally to provide default behaviour that covers common use cases, and then provide extensibility points for users to plug in their own logic if they want non-default behaviour, rather than trying to include lots of different options in the core library. Thanks for wanting to be involved though! |
Thanks for the explanation. The The extension in this case requires creation of validator selector using the same code that is part of the core since there are already multiple cases like |
Yeah I see where you're coming from. How about a compromise: we currently have global factory methods for creating the Ruleset and MemberName selectors. We could add another factory method for creating the |
Seems quite reasonable. |
Yes, so you can currently do it with the MemberNameValidatorSelector and the RulesetValidatorSelector, eg: // Replace RulesetValidatorSelector with a custom version:
ValidatorOptions.Global.ValidatorSelectors.RulesetValidatorSelectorFactory = (rulesets) => new MyRulesetValidatorSelector(rulesets); ...and would add something similar for the composite selector. |
Makes sense indeed. |
I've pushed out an 11.2 release which contains the |
What does the validate call look like @uhfath ? Just curious about the use case here. |
That was fast. |
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); |
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 aCompositeValidatorSelector
which then usesAny
to combineIValidatorSelector
s.There are cases where it would be feasible to change
Any
toAll
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:
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:
Currently it's not possible to validate a
Surname
property fromname
RuleSet separately.Or at least I haven't found a way for it to work in a single
Validate
call.The text was updated successfully, but these errors were encountered: