Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent Validation using NotEmpty on an integer property

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?

like image 416
Brendan Vogt Avatar asked Feb 02 '11 13:02

Brendan Vogt


1 Answers

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.

like image 100
rsenna Avatar answered Oct 09 '22 16:10

rsenna