DataAnnotations:递归验证整个对象图
我有一个对象图,上面散布着 DataAnnotation 属性,其中对象的某些属性是本身具有验证属性的类,等等。
在以下场景中:
public class Employee
{
[Required]
public string Name { get; set; }
[Required]
public Address Address { get; set; }
}
public class Address
{
[Required]
public string Line1 { get; set; }
public string Line2 { get; set; }
[Required]
public string Town { get; set; }
[Required]
public string PostalCode { get; set; }
}
如果我尝试验证没有 PostalCode
值的 Employee
的 Address
,那么我希望(并期望)一个例外,但我没有得到。我的做法如下:
var employee = new Employee
{
Name = "Neil Barnwell",
Address = new Address
{
Line1 = "My Road",
Town = "My Town",
PostalCode = "" // <- INVALID!
}
};
Validator.ValidateObject(employee, new ValidationContext(employee, null, null));
Validator
还有哪些其他选项可以确保所有属性都得到递归验证?
I have an object graph sprinkled with DataAnnotation attributes, where some properties of objects are classes which themselves have validation attributes, and so on.
In the following scenario:
public class Employee
{
[Required]
public string Name { get; set; }
[Required]
public Address Address { get; set; }
}
public class Address
{
[Required]
public string Line1 { get; set; }
public string Line2 { get; set; }
[Required]
public string Town { get; set; }
[Required]
public string PostalCode { get; set; }
}
If I try to validate an Employee
's Address
with no value for PostalCode
, then I would like (and expect) an exception, but I get none. Here's how I'm doing it:
var employee = new Employee
{
Name = "Neil Barnwell",
Address = new Address
{
Line1 = "My Road",
Town = "My Town",
PostalCode = "" // <- INVALID!
}
};
Validator.ValidateObject(employee, new ValidationContext(employee, null, null));
What other options do I have with Validator
that would ensure all properties are validated recursively?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是选择加入属性方法的替代方法。我相信这将正确遍历对象图并验证一切。
最新的代码:
https://github.com/reustmd/DataAnnotationsValidatorRecursive
包:
https://www.nuget.org/packages/DataAnnotationsValidator/
此外,我还更新了此内容处理循环对象图的解决方案。感谢您的反馈。
Here's an alternative to the opt-in attribute approach. I believe this will traverse the object-graph properly and validate everything.
Most up-to-date code:
https://github.com/reustmd/DataAnnotationsValidatorRecursive
Package:
https://www.nuget.org/packages/DataAnnotationsValidator/
Also, I have updated this solution to handle cyclical object graphs. Thanks for the feedback.
您可以扩展默认验证行为,使要验证的类实现 IValidatableObject 接口
,并通过以下方式之一使用 Validator 类对其进行验证
或
You can extend the default validation behavior, making the class you want to validate implement the
IValidatableObject
interfaceAnd validate it using the
Validator
class in one of these waysor
我在搜索 Blazor 遇到的类似问题时发现了这个问题。随着 Blazor 变得越来越流行,我认为这是一个很好的地方来提及我如何解决这个问题。
首先,使用包管理器控制台安装以下包:
Install-Package Microsoft.AspNetCore.Components.DataAnnotations.Validation -Version 3.2.0-rc1.20223.4
或者,您也可以将其手动添加到您的 . csproj 文件:
添加并安装此包后,我们可以简单地将以下数据注释添加到任何对象以表明它是一种复杂类型。使用提供的示例 OP:
记下
Address
引用上方的[ValidateComplexType]
注释。对于在使用 Blazor 时也发现这篇文章的人:
确保您的 EditForm 使用此 AnnotationValidator 而不是普通的 AnnotationValidator:
来源: https://learn.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-3.1#blazor-data-annotations-validation-package
I found this issue while searching for a similar problem I had with Blazor. Seeing as Blazor is becoming increasingly more popular I figured this would be a good place to mention how I solved this problem.
Firstly, install the following package using your package manager console:
Install-Package Microsoft.AspNetCore.Components.DataAnnotations.Validation -Version 3.2.0-rc1.20223.4
Alternatively you can also add it manually in your .csproj file:
Having added and installed this package one can simply add the following data annotation to any object to indicate that it is a complex type. Using the example OP provided:
Take note of the
[ValidateComplexType]
annotation above theAddress
reference.For the ones that also found this post when using Blazor:
make sure your EditForm uses this AnnotationValidator instead of the normal one:
Source: https://learn.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-3.1#blazor-data-annotations-validation-package
MiniValidation 库可能是最好的最新解决方案。
验证将是一行:
MiniValidation library is probably the best up to date solution.
The validation will be one liner:
代码:
Code:
我稍微清理了 j_freyre 的代码。如果您没有“this.serviceProvider”,则可以将其替换为“null”。
I cleaned the code from j_freyre a little. The "this.serviceProvider" can be replaced with "null" if you dont have one.
Fluent Validation 库为验证复杂嵌套对象提供了出色的支持。因此,假设在您的应用程序中您有客户,并且客户可以拥有订单集合,如下所示:
现在,要验证客户,您所需要做的就是为每个订单设置订单验证器:
Fluent Validation library provides excellent support for validating complex nested objects. So, say in your application you have Customer's, and Customer's can have collections of Orders, like this:
Now, to validate Customer's, all you need is to set orders validator for each Order: