validation - PostSharp 参数验证 - 具有最大长度的可选字符串

标签 validation parameters postsharp

我正在使用 PostSharp 3.1 在 Web 服务上进行参数验证。

我有一个可选的字符串参数,传递时该参数必须少于 50 个字符。

我目前有[StringLength(50)],这意味着必须传递一个字符串,string.Empty可以传递,但null不能。

null对此参数有效。

我需要它工作的方式与[EmailAddress]验证相同 - 如果传递null,则不验证,否则验证是否有任何字符串通过了。

这可以做到吗?

最佳答案

我编写了一个自定义验证属性,如下所示:

using System;

using PostSharp.Aspects;
using PostSharp.Patterns.Contracts;
using PostSharp.Reflection;

public class NullOrStringLengthAttribute : LocationContractAttribute, ILocationValidationAspect<string>
{
    private readonly int maximum;

    public NullOrStringLengthAttribute(int maximum)
    {
        this.maximum = maximum;

        this.ErrorMessage = "The parameter '{0}' must be a string with a maximum length of " + maximum + ".";
    }

    public Exception ValidateValue(string value, string locationName, LocationKind locationKind)
    {
        if (value == null)
        {
            return null;
        }

        return value.Length > this.maximum ? this.CreateArgumentException(value, locationName, locationKind) : null;
    }

}

编辑:更新为包括最小字符串长度和新的 PostSharp 4.x 错误消息模式:

using System;

using PostSharp.Aspects;
using PostSharp.Patterns.Contracts;
using PostSharp.Reflection;

public class NullOrStringLengthAttribute : LocationContractAttribute, ILocationValidationAspect<string>
{
    public NullOrStringLengthAttribute(int maximumLength)
    {
        this.ErrorMessage = string.Format("The {{2}} must be null or a string with a maximum length of {0}.", maximumLength);
        this.MaximumLength = maximumLength;
        this.MinimumLength = 0;
    }

    public NullOrStringLengthAttribute(int minimumLength, int maximumLength)
    {
        if (maximumLength != int.MaxValue)
        {
            this.ErrorMessage = string.Format("The {{2}} must be null or a string with length between {0} and {1}.", minimumLength, maximumLength);
        }
        else
        {
            this.ErrorMessage = string.Format("The {{2}} must be null or a string with a minimum length of {0}.", minimumLength);
        }

        this.MaximumLength = maximumLength;
        this.MinimumLength = minimumLength;
    }

    public int MaximumLength { get; private set; }

    public int MinimumLength { get; private set; }

    public Exception ValidateValue(string value, string locationName, LocationKind locationKind)
    {
        if (value == null)
        {
            return null;
        }

        if (value.Length < this.MinimumLength || value.Length > this.MaximumLength)
        {
            return this.CreateArgumentException(value, locationName, locationKind);
        }

        return null;
    }
}

关于validation - PostSharp 参数验证 - 具有最大长度的可选字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24994554/

相关文章:

c# - 使用两个参数启动线程

java - 在Java中,byte[]与string作为方法参数有什么优缺点?

c# - 向多个目标类中的方法调用注入(inject)外部参数

python - 在 Python 2.7 中,如何将用户输入限制为特定整数并跟踪异常?

azure - 如何从AI日志中排除参数?

c# - 如何验证文件夹是否已存在

wpf - 使用 PostSharp 为嵌套属性或集合撤消重做

resharper - 使用 PostSharp 和 ReSharper 的任何经验

ruby-on-rails - Rails state_machine - 条件验证?

javascript - Jquery 下拉列表日期验证