This project is read-only.


Validation Required on Enumerator


Validation of Required Rule fails on Enumerator if the first item is set.
To reproduce in the test project:
public enum Gender
in Test.Entities.Customer
public Gender Gender { get; set; }
in Test.ValidationLevelTests
    public void ValidationNotification_Enum_Required_IsValid()
        ValidationCatalog.AddSpecification<Customer>(spec =>
            spec.Check(cust => cust.Gender).Required();
        var customer = new Customer();
        customer.Gender = Gender.Male;
        var vn = ValidationCatalog.Validate(customer);
        Assert.That(vn.IsValid, Is.True);
This test will fail

file attachments

Closed Mar 27, 2011 at 6:22 AM by TechnoAg


luis_v_silva wrote Mar 18, 2011 at 3:30 AM

I have submited a patch with the fix and the unit test now passing

TechnoAg wrote Mar 18, 2011 at 5:51 PM


Thank you for the patch, I'll be applying it soon.

Just for future documentation, the Enum type has a default value of the first item in the list. The RequiredRule checks if the value is null or the default value for nonnullable types. In the case an enum marked as required, if the value is the first in the list, then it's the default value resulting in the Required rule failing. The resolution is to always let an enum pass the Required rule.

Check( cust => cust.Gender).Required() will always pass. So, with enum, you'll need to add an Equals rule to test for appropriate values:

Check(cust => cust.Gender).Required().Equals(Gender.Male);

luis_v_silva wrote Mar 19, 2011 at 2:21 AM

Hi Techno,

Thanks for the quick reply. Not sure why you need the second rule, only if you want to confirm that the vlidation is to be Male right? The required rule is quite redundant on an Enum, cause they will allways be non null, the reason that I want the rule is for the integration with webforms so it can generate a UI validator for the form input element.

Also I did some work on integrating Devexpress controls with this framework using as a base your Specexpress.Web project. Its looking pretty good. Talking about that I think your Web project is not compiling anymore, have you guys abandoned it? I am hapy to put some work on it.

At the moment the ProperyValidator has dropped the RequiredValue property which forces me to use reflection to check that property on the Generic PropertyValidator. Was this a design decision?

Thanks for the excelent project

TechnoAg wrote Mar 19, 2011 at 3:23 AM

Thanks for the bringing the web project compilation to our attention. The last big version added grouping of rules into complex And/Or trees, and the internal refactoring broke the project and we had forgotten about it since then. We're looking at adding the missing fields back. I'm hoping to have a fix before the end of next week. Sorry for the inconvenience.

luis_v_silva wrote Mar 19, 2011 at 1:18 PM

Awesome. Keep up the good work mate. Cheers

wrote Mar 27, 2011 at 6:22 AM

wrote Feb 14, 2013 at 3:51 AM

wrote May 16, 2013 at 8:41 AM