【问题标题】:Fluent validation: set custom message on custom validation流畅的验证:在自定义验证上设置自定义消息
【发布时间】:2014-09-27 22:17:44
【问题描述】:

场景

我有一个自定义规则来验证订单的运费:

public class OrderValidator : BaseValidator<Order>
{

    private string CustomInfo { get; set; }

    public OrderValidator()
    {
        //here I call the custom validation method and I try to add the CustomInfo string in the message
        RuleFor(order => order.ShippingCost).Cascade(CascadeMode.StopOnFirstFailure).NotNull().Must(
            (order, shippingCost) => CheckOrderShippingCost(order, shippingCost)
        ).WithMessage("{PropertyName} not set or not correct: {PropertyValue}." + (String.IsNullOrEmpty(CustomInfo) ? "" : " " + CustomInfo));
    }

    //this is the custom validation method
    private bool CheckOrderShippingCost(Order o, decimal shippingCost)
    {
        bool res = false;

        try
        {
            /*
             * check the actual shippingCost and set the res value
             */
        }
        catch (Exception ex)
        {
            CustomInfo = ex.ToString();
            res = false;
        }

        return res;
    }
}

如果出现异常,我将异常信息存储到 CustomInfo 私有成员中,并将其添加到验证消息中。

然后我运行验证器:

OrderValidator oVal = new OrderValidator();
oVal.Results = oVal.Validate(order);
if (!oVal.Results.IsValid)
    oVal.Results.Errors.ForEach(delegate(ValidationFailure error) {
        Console.WriteLine(error.ErrorMessage);
    });

问题

一切正常,万一发生异常,CustomInfo 被正确设置为 ex.ToString() 值。但最终在控制台中显示的错误消息并没有显示 CustomInfo,而只显示了消息的第一部分:

      "Shipping Cost not set or not correct: 5.9"

问题

为什么自定义消息不包含 CustomInfo 字符串? 是否可以通过其他方式在自定义消息中添加异常信息?

【问题讨论】:

    标签: c# fluentvalidation custom-errors


    【解决方案1】:

    据此https://fluentvalidation.codeplex.com/wikipage?title=Customising&referringTitle=Documentation&ANCHOR#CustomError

    你应该使用

    .WithMessage("{PropertyName} not set or not correct: {PropertyValue}. {0}", order => order.CustomInfo);
    

    这将要求您的 CustomInfo 在 Order 类的级别上,而不是您的验证器类

    编辑

    你可以使用:

    public static class OrderExtensions
    {
        private static IDictionary<Order,string> customErrorMessages;
    
        public static void SetError(this Order order, string message) {
            if (customErrorMessages == null) {
                customErrorMessages = new Dictionary<Order,string>();
            }
            if (customErrorMessages.ContainsKey(order)) {
                customErrorMessages[order] = message;
                return;
            }
            customErrorMessages.Add(order, message);
        }
    
        public static string GetError(this Order order) {
            if (customErrorMessages == null || !customErrorMessages.ContainsKey(order)) {
                return string.Empty;
            }
            return customErrorMessages[order];
        }
    }
    

    对您的验证器进行一些小改动

    public class OrderValidator : BaseValidator<Order>
    {
        public OrderValidator()
        {
            //here I call the custom validation method and I try to add the CustomInfo string in the message
            RuleFor(order =>     order.ShippingCost).Cascade(CascadeMode.StopOnFirstFailure).NotNull().Must(
                (order, shippingCost) => CheckOrderShippingCost(order, shippingCost)
            ).WithMessage("{PropertyName} not set or not correct: {PropertyValue}. {0}", order => order.GetError()));
        }
    
        //this is the custom validation method
        private bool CheckOrderShippingCost(Order o, decimal shippingCost)
        {
            bool res = false;
    
            try
            {
                /*
                 * check the actual shippingCost and set the res value
                 */
            }
            catch (Exception ex)
            {
                order.SetError(ex.ToString());
                res = false;
            }
            return res;
        }
    }
    

    【讨论】:

    • 好点。虽然我不允许修改订单结构。此外,我认为任何验证信息都应该与验证器相关,而不是与验证对象相关。
    • 您可以创建一个完全继承您的订单类的 OrderWithInfo 类,并在您设置或从该类获取信息时进行转换?由于规则是在构造函数中定义的,除了你正在验证的对象之外,似乎很难使用其他任何东西
    • 在一个侧节点上,如果您可以添加一个扩展方法,该方法会获取错误并设置错误,您不会对您的订单类进行任何更改,并且您可以使用 @ 得到错误987654325@
    猜你喜欢
    • 1970-01-01
    • 2012-12-12
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    相关资源
    最近更新 更多