Why does the EnumConversionValidator expect a string instead of an enum?

Topics: Building and extending application blocks, Validation Application Block
Nov 17, 2010 at 3:11 AM

Why does the EnumConversionValidator expect a string instead of an enum type for the value it is validating?

I have changed the implementation of this to check an incorreclty decalred enum property like so

enum TestEnum

public class MyClass
      TestEnum testEnum;

       [EnumConversionValidator(typeof(TestEnum), Ruleset = DEFAULT_RULESET, Tag = VALIDATION_TAG, MessageTemplate = "{0} cannot be converted to type {3}")]
        public TestEnum TestEnum
                return testEnum;
                SetValue<TestEnum>(ref testEnum, value, "TestEnum");

//Which will complile and not throw an exception even though TestEnum doesnt have a corresponding -1 value
MyClass myObject = new MyClass();
myObject.TestEnum = (TestEnum)-1;

However with my implementation, when I validate this it will tell me that the -1 cannot be converted to a TestEnum value.

But before I go ahead and implement it throughout was there a specific reason why this was not done originally?

What was the thinking behind using the string?


Nov 17, 2010 at 5:31 AM
Edited Nov 17, 2010 at 5:34 AM

I personally think that a greater number of people would expect it and it's a more common scenario to convert a string to an enumeration rather than an integer to an enumeration.  That's just my opinion.

Anyway, you could log the addition of the logic of taking into consideration of converting an integer as well in the Issue Tracker as a feature request.


Sarah Urmeneta
Global Technologies & Solutions
Avanade, Inc.

Nov 17, 2010 at 9:50 AM

The EnumConversionValidator makes sense. What's missing however is a EnumValidator that validates whether the supplied value of the enum property is a defined value of that enum. I would expect the EnumValidator to be configured without having to specify  the type:

     public TestEnum TestEnum { get; set; }

Would be a nice addition for v5.1.