c# - Using FluentValidation to validate mutually exclusive fields -
i'm attempting validate 1 of 3 fields has value using fluentvalidation.
rulefor(x => x.date1) .must(x => !x.hasvalue) .when(x => x.date2.hasvalue || x.date3.hasvalue) .withmessage("select 1 of date 1, date 2 , date 3");
this repeated other 2 dates. expected, produces on message per rule matches.
there other rules involved, there way execute other rules fail on first of these three? i've seen set cascademode.stoponfirstfailure globally want other rules outside of these 3 work do.
i decide go down route. feels elegant i'll know if passes code review.
i created new property
public ienumerable<datetime?> mutuallyexclusivedates { { return new list<datetime?>() { date1, date2, date3 }; } }
then added rule
rulefor(x => x.mutuallyexclusivedates) .must(x => x.count(d => d.hasvalue) <= 1) .withmessage("select 1 of date 1, date 2 , date 3");
Comments
Post a Comment