You can take advantage of the Must
overload that gives you access to the whole class object so that you can do a property validation against other properties. See FluentValidation rule for multiple properties for more details.
public class SomeClassValidator : AbstractValidator<SomeClass>
{
private const string OneOptionalPropertyMessage = "Only one of FirstOptionalProperty, SecondOptionalProperty, or ThirdOptionalProperty can be set.";
public SomeClassValidator()
{
RuleFor(x => x.FirstOptionalProperty)
.Must(OptionalPropertiesAreValid)
.WithMessage(OneOptionalPropertyMessage);
RuleFor(x => x.SecondOptionalProperty)
.Must(OptionalPropertiesAreValid)
.WithMessage(OneOptionalPropertyMessage);
RuleFor(x => x.ThirdOptionalProperty)
.Must(OptionalPropertiesAreValid)
.WithMessage(OneOptionalPropertyMessage);
}
// this "break out" method only works because all of the optional properties
// in the class are of the same type. You'll need to move the logic back
// inline in the Must if that's not the case.
private bool OptionalPropertiesAreValid(SomeClass obj, IEnumerable<int> prop)
{
// "obj" is the important parameter here - it's the class instance.
// not going to use "prop" parameter.
// if they are all null, that's fine
if (obj.FirstOptionalProperty is null &&
obj.SecondOptionalProperty is null &&
obj.ThirdOptionalProperty is null)
{
return true;
}
// else, check that exactly 1 of them is not null
return new []
{
obj.FirstOptionalProperty is not null,
obj.SecondOptionalProperty is not null,
obj.ThirdOptionalProperty is not null
}
.Count(x => x == true) == 1;
// yes, the "== true" is not needed, I think it looks better
}
}
You can tweak the check function. As this currently stands, if you set 2 or more of the optional properties, ALL of them will throw an error. That may or may not be fine for your needs.
You could also make a RuleFor
ONLY for the first optional property, rather than all of them, as all the properties will be executing the same IsValid code and return the same message, your user might just get a bit confused if they get an error message for OptionalProperty1 but they didn't supply that one.
The downside to this approach is that you need to know at compile time what all your properties are (so you can write the code for it), and you need to maintain this validator if you add/remove optional entries. This downside may or may not be important to you.
NotBeNull
? Did I understand you correctly? – Sort