如何使用流利验证在类的另一个IEnumerable中验证类的IEnumeraable



我有这些类

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添加到问题标签时,这是使用FluentValidation:的Collection Validation

public class CryptocurrencyDtoValidator : AbstractValidator<CryptocurrencyDto> {
public CryptocurrencyDtoValidator() {
RuleForEach(x => x.Cryptocurrency).NotNull();
}
}

更多信息

但是,还有许多其他方法可以对相关类使用验证(例如TIEnumerable<T>(:

  1. 创建并使用ValidationAttribute:更多信息

  2. 实现IValidatableObject:更多信息

  3. 检查Controller级别中的任何关联类。

最新更新