5

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?

3 Answers 3

21

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Look at my question again. If I pass 0 to the ID, which one of the rules will fire first seeing that both will fire if there is a 0. How do I know the order of preference?
I'm going to accept this answer, but in the future try and answer a question and not half a question.
@Brendan Vogt: 1) In fact your "question" is made of 9 distinct questions - and at least 7 of them are answered just by the above quote. 2) I am not aware of the resolution order, but I suppose it will be the same order you have used at the declaration of the rules (and you know you can debug it, right?). 3) You seem to be upset by my answer, which is strange, since I did not mean any kind of offense. 4) And if my answer did not please you, then do not accept it. That's why SO exists, after all - we should only accept the answers we are satisfied with.
2

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?

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.