DataAnnotations命名空间中的枚举值是否有现成的验证器? [英] Is there out-of-the box validator for Enum values in DataAnnotations namespace?

查看:205
本文介绍了DataAnnotations命名空间中的枚举值是否有现成的验证器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#枚举值不仅限于其定义中列出的值,还可以存储其基本类型的任何值。如果基本类型没有定义为 Int32 或简单地 int



我正在开发一个WCF serivice,它需要确定一些枚举的值分配给所有枚举值的默认值。我从单元测试开始,以确定是否 [必需] 将在这里做正确的工作。

  using System.ComponentModel.DataAnnotations; 
使用Xunit;

public enum MyEnum
{
//我始终从1开始,以将默认值的第一个值与
First = 1,
Second ,
}

public class Entity
{
[必需]
public MyEnum EnumValue {get;组;


public class EntityValidationTests
{
[Fact]
public void TestValidEnumValue()
{
实体实体=新实体{EnumValue = MyEnum.First};

Validator.ValidateObject(entity,new ValidationContext(entity,null,null));
}

[事实]
public void TestInvalidEnumValue()
{
实体实体=新实体{EnumValue =(MyEnum)( - 126)} ;
// -126存储在entity.EnumValue属性

Assert.Throws< ValidationException>(()=>
Validator.ValidateObject(entity,new ValidationContext ,null,null)));
}
}

没有,第二个测试不会抛出任何我的问题是:有一个验证器属性来检查提供的值是否在 Enum.GetValues 中?



更新
确保使用最后一个参数等于 True ValidateObject(Object,ValidationContext,Boolean)

解决方案

p>有 EnumDataType 在.NET4 + ...



确保您设置第三个参数 validateAllProperties = true 从您的示例中调用 ValidateObject



  public class Entity 
{
[EnumDataType(typeof(MyEnum))]
public MyEnum EnumValue {get;组;


[事实]
public void TestInvalidEnumValue()
{
实体实体=新实体{EnumValue =(MyEnum)( - 126) };
// -126存储在entity.EnumValue属性

Assert.Throws< ValidationException>(()=>
Validator.ValidateObject(entity,new ValidationContext ,null,null),true));
}


C# enum values are not limited to only values listed in it's definition and may store any value of it's base type. If base type is not defined than Int32 or simply int is used.

I am developing a WCF serivice which needs to be confident that some enum has a value assigned as opposed to the default value for all enums of 0. I start with a unit test to find out whether [Required] would do right job 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)));
    }
}

It does not, the second test does not throw any exception.

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

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

解决方案

There's EnumDataType in .NET4+ ...

Make sure you set the 3rd 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));
}

这篇关于DataAnnotations命名空间中的枚举值是否有现成的验证器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆