I have a validator that looks like this
public class ImageValidator : AbstractValidator<Image>
{
public ImageValidator()
{
RuleFor(e => e.Name).NotEmpty().Length(1, 255).WithMessage("Name must be between 1 and 255 chars");
RuleFor(e => e.Data).NotNull().WithMessage("Image must have data").When(e => e.Id == 0);
RuleFor(e => e.Height).GreaterThan(0).WithMessage("Image must have a height");
RuleFor(e => e.Width).GreaterThan(0).WithMessage("Image must have a width");
}
}
Now my unit tests are failing because Height and Width are populated based on the value in Data.
Data holds a byte array which creates a bitmap image. If Data is not null (and Id equals 0, so its a new image), i can create a Bitmap image and obtain the Height and Width values. Upon updating, the Height and Width would already be populated and Data could be null, because it would be stored in the database already.
Is there anyway i can populate the values of Height and Width if the validation rule for Data is true within my validator?
Before i had a check like
if (myObject.Data == null) throw new ApplicationException(...
But i think this is a validation rule, and should be in the validator, or do i just need to split the rules up?