I have the following code:
public class NewsEditViewDataValidator : AbstractValidator<NewsEditViewData>
{
public NewsEditViewDataValidator()
{
// Status unique identifier cannot be empty
// Status unique identifier must be greater or equal to 1
RuleFor(x => x.StatusId)
.NotEmpty()
.WithMessage("Status is required")
.GreaterThanOrEqualTo(1)
.WithMessage("Status unique identifier must be greater or equal to 1");
// Other rule sets
}
}
StatusId is an integer. How does NotEmpty work in this case? What does it validate? Integers or string? What would the unit test look like for this part checking that an integer is not empty?
This is used to validate a dropdown list in my MVC 3 application. The validation works well on the view. The GreaterThanOrEqualTo part is that the status unique identifier can never less than 1. This I want to trigger to validate my object. When do it this way will NotEmpty also not fire? Is there a preference as to which one will be fired first? If StatusId is 0 which rule set will fire? If it is -1? I would like NotEmpty to work with the view and GreaterThanOrEqualTo when checking the id of the business object. Any suggestions?
Look at the documentation:
NotEmpty Validator
Description: Ensures that the specified property is not null, an empty string or whitespace (or the default value for value types, eg 0 for int).
So NotEmpty()
will avoid only the default value (0) for that property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With