DependentRules() method for RuleSet() within abstractValidator
See original GitHub issueIs your feature request related to a problem? Please describe.
I’ve got multiple rules that should be met before I would like to validate some code. There’s When()
method but I need to repeat my rules in conditional.
Describe the solution you’d like
DependentRules() method for RuleSet() within abstractValidator. Example:
RuleSet(()=>{
RuleFor(d => d.NotificationType).NotEmpty().WithMessage("Required");
RuleFor(<some-other-rule-in-other-property>);
}).DependentRules(()=> {
RuleFor(d => d.NotificationEmail).EmailAddress().WithMessage("Invalid Email Address");
RuleFor(d => d.NotificationEmail).NotEmpty().WithMessage("Required");
});
Describe alternatives you’ve considered
There is alternative like this:
RuleFor(d => d.NotificationType).NotEmpty().WithMessage("Required");
When(d => !string.IsNullOrEmpty(d.NotificationType) && d.NotificationType.ToUpper() == "EMAIL", () =>
{
RuleFor(d => d.NotificationEmail).EmailAddress().WithMessage("Invalid Email Address");
RuleFor(d => d.NotificationEmail).NotEmpty().WithMessage("Required");
});
but I don’t like this because of violating of DRY principle.
Additional Context
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Dependent Rules — FluentValidation documentation
This method accepts a lambda expression inside which you can define other rules that will be executed only if the first rule passes:...
Read more >Understanding dependent rules · Issue #153
Trying to understand as why my BaseValidation() method will not fire off in the code below assuming first name is just an empty...
Read more >RuleSets — FluentValidation documentation
RuleSets ¶. RuleSets allow you to group validation rules together which can be executed together as a group whilst ignoring other rules:.
Read more >FluentValidation Call RuleSet and Common Rules
The using directive is required to bring the Validate() extension method from DefaultValidatorExtensions into scope, which has the ruleSet ...
Read more >Creating Complex Validation Rules Using Fluent ...
The RuleFor method takes a lambda expression as an argument that allows you to specify the property the rule is to be applied...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Use case is explained in the documentation: https://docs.fluentvalidation.net/en/latest/rulesets.html
That’s not something I’d be interested in supporting. The existing
When(lambda)
is sufficient for this use case.