c# - 如何使用流利的验证在一个类的另一个 IEnumerable 中验证一个类的 IEnumerable
问题描述
我有这些课程
public class CryptocurrencyDto
{
public int RequestId { get; set; }
public IEnumerable<Cryptocurrency> Cryptocurrencies { get; set; }
}
public class Cryptocurrency : BaseClass
{
public string Ticker { get; set; }
public string Name { get; set; }
public double TotalSupply { get; set; }
public double CirculatingSupply { get; set; }
public IEnumerable<Note> Notes { get; set; }
}
public class Note
{
public int NoteId { get; set; }
public string Description { get; set; }
public IEnumerable<Url> Urls { get; set; }
public byte Image { get; set; }
public int DisplayOrder { get; set; }
}
public class Url
{
public string UrlId { get; set; }
public string Link { get; set; }
public string Description { get; set; }
}
我有这个端点
[HttpPost]
public void Post([FromBody] CryptocurrencyDto cryptocurrency)
{
}
我如何通过这些课程进行验证?到目前为止,我只知道如何验证第一堂课CryptocurrencyDto
。我不知道如何到达其他班级。加密货币、注释和 URL。
解决方案
当您将 FluentValidation 添加到问题标签时,这是Collection Validation
使用 FluentValidation:
public class CryptocurrencyDtoValidator : AbstractValidator<CryptocurrencyDto> {
public CryptocurrencyDtoValidator() {
RuleForEach(x => x.Cryptocurrency).NotNull();
}
}
但是还有许多其他方法可以对关联的类使用验证(例如T
or IEnumerable<T>
):
推荐阅读
- c++ - 如何从标头中检索不记名令牌(rest sdk)
- php - 如何使用 .env.production 运行 php artisan serve?
- python - 如何在 Heroku 中使用 tesseract 和 ocr?
- java - 已清除配置文件更新上的 Keycloak 用户属性 - 如何保留并且只能由管理员编辑
- python - 我正在使用的 API 返回一个数据框 - 如何使用列表返回多个 DF,然后合并它们?
- html - 背景图片在某些移动设备上不显示
- python - 从 .txt 文件创建熊猫数据框,每行数据
- amazon-emr - 以编程方式生成“实例”定义以在 StepFunctions 中创建 EMR 集群
- maven - 如何让`mvn package`知道它已经构建了目标
- java - Crystal Report 错误 - 使用 Java 与 Oracle 连接