私は次の流暢な検証を行っていますが、部分的に機能しています。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"
}
}
バリデーターに何を期待するかによります。
あなたが書く場合、デフォルトで
var results = validator.Validate(new TestData
{
Entities = new List<string> { "", "" }
});
例外はスローされませんが、後で確認できる結果が返されます。
bool success = results.IsValid;
例外をスローするために必要な場合は、次の方法で使用してください。
validator.ValidateAndThrow(new TestData
{
Entities = new List<string> { "", "" }
});
したがって、私にとっては、両方のメソッドの検証は次の構成で機能します
public class TestDataValidator : AbstractValidator<TestData>
{
public TestDataValidator()
{
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 ");
}
}
最初のアプローチを使用することresult.IsValid
は誤りです。使用ValidateAndThrow
すると例外がスローされます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加