If I have something like this:
When(x => x.SendMail.Equals("Y"), () =>
{
RuleFor(x => x.To).NotEmpty();
RuleFor(x => x.From).NotEmpty();
RuleFor(x => x.EmailAddress).NotEmpty();
});
and SendMail does not have a value, I will get a NullReferenceException. However, if I surround the When() like so:
When(x => x.SendMail != null, () =>
{
When(x => x.SendMail.Equals("Y"), () =>
{
RuleFor(x => x.To).NotEmpty();
RuleFor(x => x.From).NotEmpty();
RuleFor(x => x.EmailAddress).NotEmpty();
});
});
it works as I would expect and I do not get a NRE when SendMail does not have a value. I'm new to FluentValidaton and C# in general. Is this the proper way to go about handling validations like this? Do I need to wrap all logic like this with null checks?
When(x => x.SendMail != null && x.SendMail.Equals("Y"),...);