Built-in validators
There is a cornucopia of built-in validators in addition to the Equal
and NotEmpty
validators we’ve seen so far. I won’t provide a comprehensive list (see the documentation) but one of the most interesting is the PredicateValidator
validator. This passes the value of the property to a delegate, which can use custom validation logic. This is accomplished with the keyword Must
keyword, as shown here:
RuleFor(car => car.Is_Deleted).Must(isDeleted => isDeleted == "0").WithMessage("Car must have value zero");
If this validation fails, the issue is displayed in the results as shown in Figure 5.5:
Figure 5.5 – PredicateValidator error
There is a RegularExpression
validator that uses the Matches
keyword (instead of Must
), but one of my favorites is EmailValidator
, which ensures that the value submitted is a valid email. Similarly, there is a CreditCard
validator:
RuleFor(cc => cc.CreditCard...