I have a generic object with particular rules setup in my database. I would like to execute particular rules setup in the database, depending on a value within the object.
For example, lets say i have an object like this
public class MyObject {
public int Type { get; set; }
public string Name { get; set; }
public decimal? Value { get; set; }
}
Now if the value of Type is 0, then i need to make sure then Name is populated. If Type is 1, then i need to make sure Name is populated and over 50 chars, and also need Value to be populated.
This is a basic example, and there are more rules as well. So far i have
public class MyObjectValidator : AbstractValidator<MyObject>
{
public MyObjectValidator()
{
// here i would like to check what the value of type is, something like
if (Type == 1) {
RuleFor(e => e.Name).NotEmpty().WithMessage("Please enter a name");
}
if (Type == 2) {
RuleFor(....);
}
}
}
But i do not know how to get the instance that is being validated.