问题描述
我可以添加多个验证一个对象?例如:
Can I add more than one validator to an object? For example:
public interface IFoo
{
int Id { get; set; }
string Name { get; set; }
}
public interface IBar
{
string Stuff { get; set; }
}
public class FooValidator : AbstractValidator<IFoo>
{
public FooValidator ()
{
RuleFor(x => x.Id).NotEmpty().GreaterThan(0);
}
}
public class BarValidator : AbstractValidator<IBar>
{
public BarValidator()
{
RuleFor(x => x.Stuff).Length(5, 30);
}
}
public class FooBar : IFoo, IBar
{
public int Id { get; set; }
public string Name { get; set; }
public string Stuff { get; set; }
}
public class FooBarValidator : AbstractValidator<FooBar>
{
public FooBarValidator()
{
RuleFor(x => x)
.SetValidator(new FooValidator())
.SetValidator(new BarValidator());
}
}
运行测试。
FooBarValidator validator = new FooBarValidator();
validator.ShouldHaveValidationErrorFor(x => x.Id, 0);
我得到一个 InvalidOperationException异常
消息:属性名称不能为前pression X =&GT自动确定;的X.通过调用WithName请指定一个自定义属性名称。
有什么办法来实现这个还是我尝试使用FluentValidation的方式,这并不意味着要使用?
Is there any way to implement this or am I trying to use FluentValidation in a way that it's not meant to be used?
推荐答案
RuleFor试图创建一个属性级别的规则。你还可以使用AddRule函数来添加一个通用的规则
RuleFor is trying to create a property-level rule. You can additionally use the AddRule function to add a general-purpose rule.
使用这个,我创建了一个概念复合规则的证明。这需要在集中的其他验证,并运行它们。该产量突破
code直接来自 FluentValidator
的 DelegateValidator
。我不知道该怎么办,所以我抓起,从源头。我没有跟踪其全部目的,但一切似乎工作为的是:)
Using this, I created a composite rule proof of concept. It takes in a set of other validators and runs them. The yield break
code came straight from FluentValidator
's DelegateValidator
. I wasn't sure what to do with it so I grabbed that from the source. I didn't trace its full purpose, but everything seems to work as is :)
public interface IFoo
{
int Id { get; set; }
string Name { get; set; }
}
public interface IBar
{
string Stuff { get; set; }
}
public class FooValidator : AbstractValidator<IFoo>
{
public FooValidator()
{
RuleFor(x => x.Id).NotEmpty().GreaterThan(0);
}
}
public class BarValidator : AbstractValidator<IBar>
{
public BarValidator()
{
RuleFor(x => x.Stuff).Length(5, 30);
}
}
public class FooBar : IFoo, IBar
{
public int Id { get; set; }
public string Name { get; set; }
public string Stuff { get; set; }
}
public class CompositeValidatorRule : IValidationRule
{
private IValidator[] _validators;
public CompositeValidatorRule(params IValidator[] validators)
{
_validators = validators;
}
#region IValidationRule Members
public string RuleSet
{
get; set;
}
public IEnumerable<ServiceStack.FluentValidation.Results.ValidationFailure> Validate(ValidationContext context)
{
var ret = new List<ServiceStack.FluentValidation.Results.ValidationFailure>();
foreach(var v in _validators)
{
ret.AddRange(v.Validate(context).Errors);
}
return ret;
}
public IEnumerable<ServiceStack.FluentValidation.Validators.IPropertyValidator> Validators
{
get { yield break; }
}
#endregion
}
public class FooBarValidator : AbstractValidator<FooBar>
{
public FooBarValidator()
{
AddRule(new CompositeValidatorRule(new FooValidator(), new BarValidator()));
}
}
基础测试案例:
[TestMethod]
public void TestValidator()
{
FooBarValidator validator = new FooBarValidator();
var result = validator.Validate(new FooBar());
}
我希望这有助于。
I hope this helps.
这篇关于fluentvalidation多重校验器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!