Brendan Vogt
Brendan Vogt

Reputation: 26048

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?

Upvotes: 5

Views: 28355

Answers (3)

Rohidas Kadam
Rohidas Kadam

Reputation: 446

@Brendan Vogt
use NotNull() instead of NotEmpty()

Upvotes: 0

Pavense
Pavense

Reputation: 21

I'd suggest that you download the source code and look into the code/tests when uncertain.

StatusId is an integer. How does NotEmpty work in this case? What does it validate?

That the value of StatusId is the default value of its type. (0)

Integers or string?

The type for StatusId, int.

What would the unit test look like for this part checking that an integer is not empty?

var validator = new NewsEditViewDataValidator();
validator.ShouldHaveValidationErrorFor(x => x.StatusID, 0);

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?

Use when or unless to specify when rules should or shouldn't be tested.

 Is there a preference as to which one will be fired first?

I believe it is in the order you have specified them.

 If StatusId is 0 which rule set will fire?

Depending on what cascade option you have set the validation will fail on first error or check other rules for the property.

 If it is -1?

The second rule would fail.

I would like NotEmpty to work with the view and GreaterThanOrEqualTo when checking the id of the business object. Any suggestions?

Upvotes: 2

rsenna
rsenna

Reputation: 11972

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.

Upvotes: 20

Related Questions