【发布时间】:2020-05-21 19:48:00
【问题描述】:
我有以下流畅的验证,正在部分工作。如果我输入 3 个请求的值是有效的,但如果我输入 2 个值也是有效的。它应该不起作用,因为所有 3 个值都是强制性的。
RuleFor(x => x.Entities).Must(list => list.Count == 3)
.WithMessage("The Entities list must contain 3 items and for AmendmentParties the RelationType must be CurrentName, ChangeAddName and AuthorizingPartyName ");
RuleForEach(x => x.Entities).ChildRules(x =>
{
x.RuleFor(x => x.RelationType).Equals(EntityAmendmentRelation.CurrentName)
.Equals(EntityAmendmentRelation.ChangeAddName)
.Equals(EntityAmendmentRelation.AuthorizingPartyName);
});
请求如下:目标是实体列表必须包含 3 个项目,并且在每个项目中,RelationType 属性需要不同。
"entities": [
{
"ObjectType": "SecuredParty",
"reqSearchID": "NoSearch",
"RelationType": "CurrentName",
"altCapacity": "NoAltCapacity",
"OrganizationName": "Secured Party",
"Address": {
"StreetAddress": "12345 Main Street",
"City": "Sacramento",
"State": "CA",
"PostalCode": "95811",
"Country": "USA"
}
},
{
"ObjectType": "SecuredParty",
"reqSearchID": "NoSearch",
"RelationType": "ChangeAddName",
"altCapacity": "NoAltCapacity",
"OrganizationName": "Secured Party",
"Address": {
"StreetAddress": "12345 Main Street",
"City": "Sacramento",
"State": "CA",
"PostalCode": "95811",
"Country": "USA"
}
},
{
"ObjectType": "SecuredParty",
"reqSearchID": "NoSearch",
"RelationType": "AuthorizingPartyName",
"altCapacity": "NoAltCapacity",
"OrganizationName": "Secured Party",
"Address": {
"StreetAddress": "12345 Main Street",
"City": "Sacramento",
"State": "CA",
"PostalCode": "95811",
"Country": "USA"
}
}
【问题讨论】:
-
写一个函数来检查项目并在 Must 子句上调用它
-
工作是什么意思?它不会抛出异常,而是返回结果。如果您需要它来引发异常,请使用
validator.ValidateAndThrow
标签: c# fluentvalidation