首次失败时停止流利验证

时间:2014-02-06 14:24:00

标签: validation servicestack fluentvalidation

我正在为我的Request对象定义验证。 我希望验证器在第一次失败时停止,而不仅仅是同一链上的验证器。 在下面的示例中,如果我的TechnicalHeader对象为null,则在验证到达TechnicalHeader.MCUserid的规则时,我会收到NullReference异常。

可怜的话,根据第一条规则的结果,我想对下面代码中的最后三条规则进行条件验证

using System;
using ServiceStack.FluentValidation;
using MyProj.Services.Models;

namespace MyProj.Services.BaseService.Validators
{
    public class BaseValidator<T> : AbstractValidator<T>
        where T : RequestBase
    {
        public BaseValidator()
        {
            RuleSet(ServiceStack.ApplyTo.Put | ServiceStack.ApplyTo.Post, 
                () =>
                {
                    this.CascadeMode = CascadeMode.StopOnFirstFailure;
                    RuleFor(x => x.TechnicalHeader).Cascade(CascadeMode.StopOnFirstFailure).NotNull().WithMessage("Header cannot be null");
                    RuleFor(x => x.TechnicalHeader).NotEmpty().WithMessage("Header cannot be null");
                    RuleFor(x => x.TechnicalHeader.Userid).NotEmpty().WithMessage("Userid cannot be null or an empty string");
                    RuleFor(x => x.TechnicalHeader.CabCode).GreaterThan(0).WithMessage("CabCode cannot be or less than 0");
                    RuleFor(x => x.TechnicalHeader.Ndg).NotEmpty().WithMessage("Ndg cannot be null or an empty string");
                }
            );
        }
    }
}

3 个答案:

答案 0 :(得分:26)

在使用null条件运行依赖于它们的规则之前,只需检查When

this.CascadeMode = CascadeMode.StopOnFirstFailure;
RuleFor(x => x.TechnicalHeader).NotNull().WithMessage("Header cannot be null");

// Ensure TechnicalHeader is provided
When(x => x.TechnicalHeader != null, () => {
    RuleFor(x => x.TechnicalHeader.Userid).NotEmpty().WithMessage("Userid cannot be null or an empty string");
    RuleFor(x => x.TechnicalHeader.CabCode).GreaterThan(0).WithMessage("CabCode cannot be or less than 0");
    RuleFor(x => x.TechnicalHeader.Ndg).NotEmpty().WithMessage("Ndg cannot be null or an empty string");
});

答案 1 :(得分:1)

只需使用SetValidator条件在运行依赖于它们的规则之前添加Custom

//if InpatOrderImportValidatorBllNode fail ,the custom method cannot be executed
RuleFor(x => x).Cascade(CascadeMode.StopOnFirstFailure)
               .SetValidator(new InpatOrderImportValidatorBllNode())
               .Custom((input, context) => {          
                  context.AddFailure(new ValidationFailure("DrugTypeName", "fail"));
               });

答案 2 :(得分:1)

您可以使用DependentRules。

RuleFor(object => object.String)
    .NotNull()
    .DependentRules(() =>
    {
        RuleFor(object => object.String)
            .NotEmpty()
            .Matches("^[A-Z]{3}$");
    });

那么您就不会重复验证代码。

您甚至可以添加扩展方法,而不必复制RuleFor。

    public static IRuleBuilderOptions<T, TProperty> DependentRules<T, TProperty>(
        this IRuleBuilderOptions<T, TProperty> currentRule, 
        Action<IRuleBuilderOptions<T, TProperty>> action)
    {
        return currentRule.DependentRules(() => action(currentRule));
    }

那么定义代码:

RuleFor(object => object.String)
    .NotNull()
    .DependentRules(currentRule =>
    {
        currentRule
            .NotEmpty()
            .Matches("^[A-Z]{3}$");
    });