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.

DependentRules() method for RuleSet() within abstractValidator

See original GitHub issue

Is 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:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, Oct 26, 2021

Use case is explained in the documentation: https://docs.fluentvalidation.net/en/latest/rulesets.html

0reactions
JeremySkinnercommented, Oct 26, 2021

That’s not something I’d be interested in supporting. The existing When(lambda) is sufficient for this use case.

Read more comments on GitHub >

github_iconTop 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 >

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