我可以使用DataAnnotations验证集合属性吗?

时间:2015-10-12 20:59:56

标签: c# asp.net-web-api data-annotations

我有一个WebAPI2控制器的模型,其中的字段包含字符串的集合(List)。有没有一种方法可以为字符串指定DataAnnotations(例如[MaxLength]),以确保通过验证列表中的所有字符串都不是> 50个长度?

    public class MyModel
    {
        //...

        [Required]
        public List<string> Identifiers { get; set; }

        // ....
    }

我宁愿不创建一个新类只是为了包装字符串。

2 个答案:

答案 0 :(得分:1)

您可以编写自己的验证属性,例如如下:

public class NoStringInListBiggerThanAttribute : ValidationAttribute
{
    private readonly int length;

    public NoStringInListBiggerThanAttribute(int length)
    {
        this.length = length;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var strings = value as IEnumerable<string>;
        if(strings == null)
            return ValidationResult.Success;

        var invalid = strings.Where(s => s.Length > length).ToArray();
        if(invalid.Length > 0)
            return new ValidationResult("The following strings exceed the value: " + string.Join(", ", invalid));

        return ValidationResult.Success;
    }
}

您可以将它直接放在您的财产上:

[Required, NoStringInListBiggerThan(50)]
public List<string> Identifiers {get; set;}

答案 1 :(得分:0)

我知道这个问题由来已久,但对于那些偶然发现它的人来说,这是我的自定义属性版本,其灵感来自于已接受的答案。它利用 StringLengthAttribute 来完成繁重的工作。

/// <summary>
///     Validation attribute to assert that the members of an IEnumerable&lt;string&gt; property, field, or parameter does not exceed a maximum length
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)]
public class EnumerableStringLengthAttribute : StringLengthAttribute
{
    /// <summary>
    ///     Constructor that accepts the maximum length of the string.
    /// </summary>
    /// <param name="maximumLength">The maximum length, inclusive.  It may not be negative.</param>
    public EnumerableStringLengthAttribute(int maximumLength) : base(maximumLength)
    {
    }
    
    /// <summary>
    ///     Override of <see cref="StringLengthAttribute.IsValid(object)" />
    /// </summary>
    /// <remarks>
    ///     This method returns <c>true</c> if the <paramref name="value" /> is null.
    ///     It is assumed the <see cref="RequiredAttribute" /> is used if the value may not be null.
    /// </remarks>
    /// <param name="value">The value to test.</param>
    /// <returns><c>true</c> if the value is null or the length of each member of the value is between the set minimum and maximum length</returns>
    /// <exception cref="InvalidOperationException"> is thrown if the current attribute is ill-formed.</exception>
    public override bool IsValid(object? value)
    {
        return value is null || ((IEnumerable<string>)value).All(s => base.IsValid(s));
    }
}

编辑:如果 base.IsValid(s) 为空,则 s 返回 true,因此如果字符串成员不能为空,请改用:

public override bool IsValid(object? value)
{
    return value is null || ((IEnumerable<string>)value).All(s => !string.IsNullOrEmpty(s) && base.IsValid(s));
}