Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

New Post: Chaining Rules with multiple When

$
0
0
I have a property that has multiple rules that need to be applied. But each rule has a condition for when to apply the rule to the property.

I've defined it as
RuleFor(x=>x.Value).BetweenInclusive(0,14).When(x=>x.Code == 12)
    .WithMessage("pH out of range.")
    .BetweenInclusive(6.9,9).When(x=>x.Code == 12).WithState(x=> ValidationStates.Warning)
    .WithMessage("pH is outside of normal range.")
    .Must(BeDefinedToThousands).When(x=>codes.Contains(x.Code))
    .WithMessage("Must be reported to nearest thousandths");
But is this the right way to do it? Or Should I instead use top level When and define each rule separately?

Viewing all articles
Browse latest Browse all 1917

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>