RequiredEnumRule in validation.xml

Mar 14, 2008 at 6:09 AM
Edited Mar 14, 2008 at 7:06 AM
Hi Simon,

I have a method need two parameters, Get(string ID, Enum operations).

my validation.xml file had under section:
       <method name="Get">
            <parameter name="ID">
                <rule typeName="LengthStringRule" minimum="1" maximum="255" errorMessage="aaa/>
            <parameter name="operation">
                <rule typeName="RequiredEnumRule" errorMessage="bbb" />
when my app calls ConfigurationService.AddAssembly(assembly), I got the message "Type provided must be an Enum".

the stack trace message is like this:
at System.Enum.GetValues(Type enumType)
at ValidationFramework.RequiredEnumRuleCreator.ReadConfig(String initialValue, String errorMessage, String ruleSet, Boolean useErrorMessageProvider, RuntimeTypeHandle runtimeTypeHandle) in D:\Documents\Code\CodePlex\Validation\Validation\ValidationFramework\Validators\RequiredValidators\RequiredEnumRuleCreator.cs:line 46
at ValidationFramework.Configuration.RequiredEnumRuleConfigReader.ReadConfig(RuleData ruleData, RuntimeTypeHandle runtimeTypeHandle) in D:\Documents\Code\CodePlex\Validation\Validation\ValidationFramework\Configuration\ConfigReaders\RequiredEnumRuleConfigReader.cs:line 54
at ValidationFramework.Configuration.ConfigurationService.GetRule(RuleData ruleData, RuntimeTypeHandle runtimeTypeHandle) in D:\Documents\Code\CodePlex\Validation\Validation\ValidationFramework\Configuration\ConfigurationService.cs:line 416
at ValidationFramework.Configuration.ConfigurationService.AddRuleToInfoDescriptor(RuleData[] list, InfoDescriptor infoDescriptor) in D:\Documents\Code\CodePlex\Validation\Validation\ValidationFramework\Configuration\ConfigurationService.cs:line 352
at ValidationFramework.Configuration.ConfigurationService.ProcessMethods(ClassData classData, Type classType) in D:\Documents\Code\CodePlex\Validation\Validation\ValidationFramework\Configuration\ConfigurationService.cs:line 319
at ValidationFramework.Configuration.ConfigurationService.AddXmlReader(XmlTextReader xmlTextReader) in D:\Documents\Code\CodePlex\Validation\Validation\ValidationFramework\Configuration\ConfigurationService.cs:line 232
at ValidationFramework.Configuration.ConfigurationService.AddInputStream(Stream xmlInputStream) in D:\Documents\Code\CodePlex\Validation\Validation\ValidationFramework\Configuration\ConfigurationService.cs:line 433
at ValidationFramework.Configuration.ConfigurationService.AddResource(String path, Assembly assembly) in D:\Documents\Code\CodePlex\Validation\Validation\ValidationFramework\Configuration\ConfigurationService.cs:line 456
at ValidationFramework.Configuration.ConfigurationService.AddAssembly(Assembly assembly) in D:\Documents\Code\CodePlex\Validation\Validation\ValidationFramework\Configuration\ConfigurationService.cs:line 518
and I got another problem with the same method, when I only add a rule for ID, I got another error message says:

System.IndexOutOfRangeException : Index was outside the bounds of the array.
at ValidationFramework.ParameterValidationManager.InternalThrowException(RuntimeMethodHandle runtimeMethodHandle, String ruleSet, Object[] parameters, Object target, Object context, Boolean throwException) in D:\Documents\Code\CodePlex\Validation\Validation\ValidationFramework\ParameterValidationManager.cs:line 132

It seems the MethodCache.GetMethod(runtimeMethodHandle) got two parameters, but since I only need to validate ID, I didn't pass two parameters for validation.

I hope you can understand what I said.
thanks for your help.

Mar 14, 2008 at 11:44 AM
The enum one first...
Unfortunately the RequiredEnumRule only validates “enum”s not “Enum”s.
By this i mean the parameter you pass in would have to be strongly typed to the enum you are passing in. For example if your enum look like this
public enum Operation
and your method looked like this
public void Get(string ID, Operation operation)
Then you can use RequiredEnumRule.

If your method looks like this
public void Get(string ID, Enum operation)
Then you can should use the RequiredObjectRule.

Mar 14, 2008 at 1:10 PM
The "System.IndexOutOfRangeException : Index was outside the bounds of the array." problem is because you need to pass in all the parameters that the method takes.
So even if you are only validating one parameter you need to pass in the rest in the same order that they exists in the method.
The reason for this is that the validation relies on the index of each parameter to work out which rules apply to it.
Mar 15, 2008 at 6:57 AM
Thank you so much.

That's my bad, it should be Get(string ID, Operation operations).