Is there an authentication tool for Enum values ​​in the DataAnnotations namespace?

The values ​​of the C # enumeration are not limited only to the values ​​specified in this definition, and can store any value of its base type. If the base type is not defined than Int32 or just int .

I am developing a WCF series that needs to be sure that some enumeration has a value that is assigned instead of the default value for all enumerations at 0. I start with the unit test to see if [Required] work correctly here.

 using System.ComponentModel.DataAnnotations; using Xunit; public enum MyEnum { // I always start from 1 in order to distinct first value from the default value First = 1, Second, } public class Entity { [Required] public MyEnum EnumValue { get; set; } } public class EntityValidationTests { [Fact] public void TestValidEnumValue() { Entity entity = new Entity { EnumValue = MyEnum.First }; Validator.ValidateObject(entity, new ValidationContext(entity, null, null)); } [Fact] public void TestInvalidEnumValue() { Entity entity = new Entity { EnumValue = (MyEnum)(-126) }; // -126 is stored in the entity.EnumValue property Assert.Throws<ValidationException>(() => Validator.ValidateObject(entity, new ValidationContext(entity, null, null))); } } 

This is not so, the second test does not raise any exceptions.

My question is: is there a validator attribute to verify that the supplied value is in Enum.GetValues ?

Update . Be sure to use ValidateObject(Object, ValidationContext, Boolean) with the last parameter equal to True instead of ValidateObject(Object, ValidationContext) , as done in my unit test.

+8
enums c # data-annotations
source share
2 answers

There EnumDataType in .NET4 + ...

Make sure you set the third parameter, validateAllProperties=true in the call to ValidateObject

so from your example:

 public class Entity { [EnumDataType(typeof(MyEnum))] public MyEnum EnumValue { get; set; } } [Fact] public void TestInvalidEnumValue() { Entity entity = new Entity { EnumValue = (MyEnum)(-126) }; // -126 is stored in the entity.EnumValue property Assert.Throws<ValidationException>(() => Validator.ValidateObject(entity, new ValidationContext(entity, null, null), true)); } 
+14
source share

What are you looking for:

  Enum.IsDefined(typeof(MyEnum), entity.EnumValue) 

[Update + 1]

A validator that performs many checks, including this one, is called EnumDataType. Make sure you set validateAllProperties = true as ValidateObject, otherwise your test will fail.

If you just want to check if an enumeration is defined, you can use a special validator with the line above:

  [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method, AllowMultiple = false)] public sealed class EnumValidateExistsAttribute : DataTypeAttribute { public EnumValidateExistsAttribute(Type enumType) : base("Enumeration") { this.EnumType = enumType; } public override bool IsValid(object value) { if (this.EnumType == null) { throw new InvalidOperationException("Type cannot be null"); } if (!this.EnumType.IsEnum) { throw new InvalidOperationException("Type must be an enum"); } if (!Enum.IsDefined(EnumType, value)) { return false; } return true; } public Type EnumType { get; set; } } 

... but I suppose it's not out of the box, right?

+10
source share

All Articles