使用.Net的Fluent验证库验证单个属性

你能用 Fluent Validation库验证一个属性,如果是,如何?我以为 this discussion thread from January of 2009通过以下语法向我展示了如何做到这一点:

validator.Validate(new Person(),x => x.Surname);

不幸的是,它似乎不适用于当前版本的库.另一件让我相信可能有可能验证单个属性的事情是来自Jeremy Skinners’ blog post的以下引用:

“Finally,I added the ability to be able to execute some of FluentValidation’s Property Validators without needing to validate the entire object. This means it is Now possible to stop the default “A value was required” message from being added to ModelState. “

但是,我不知道这是否必然意味着它支持仅验证单个属性,或者您可以告诉验证库在第一个验证错误后停止验证.

解决方法

根据CodePlex上的 this discussion,该功能是通过扩展方法添加的.您需要导入FluentValidation命名空间以使其显示在IntelliSense中.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....