Add a factory method for creating the CompositeValidatorSelector
See original GitHub issueIs 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 IValidatorSelector
s.
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:
- Created a year ago
- Comments:10 (4 by maintainers)
Top GitHub Comments
Something along the lines:
I’ve pushed out an 11.2 release which contains the
ValidatorOptions.Global.ValidatorSelectors.CompositeValidatorSelectorFactory