我一直在与这个问题进行斗争,我原本认为这可能与影响我的验证的多态/继承有关,但我已将其缩小到这个......
这是班级结构..
public class Employee {
[ObjectValidator(Ruleset = "A")]
public EmployeeName Name { get; set; }
public Employee()
{
Name = new EmployeeName();
}
}
public class EmployeeName
{
[StringLengthValidator(1,20,Ruleset = "A")]
public string First { get; set; }
public string Last { get; set; }
public EmployeeName()
{
First = string.Empty;
Last = string.Empty;
}
}
测试:
[TestFixture]
public class ObjectValidationWithRulesets
{
[Test]
public void wont_validate_with_a_ruleset()
{
var person = new Employee()
{
Name = new EmployeeName()
{
First = string.Empty,
Last = string.Empty
}
};
var ruleSetValidator =
ValidationFactory.CreateValidator<Employee>("A");
var validationResults = ruleSetValidator.Validate(person);
Assert.That(!validationResults.IsValid,
"Validation with rulsets failed");
}
}
如果我删除删除规则集内容,则此测试通过。但是一旦应用了规则集,我就无法让对象正确验证。
任何人都可以对此有所了解吗?
干杯,
答案 0 :(得分:2)
我也有这个问题,但我没有在配置文件中定义targetRuleSet。我通过纠正我声明ObjectValidator属性的方式来解决问题。对我有用的正确语法如下
// Correct
[ObjectValidator("RuleSetA", Ruleset = "RuleSetA")]
在我的代码中,我错误地将其声明如下
// Wrong syntax
[ObjectValidator(Ruleset = "RuleSetA")]