简体   繁体   English

FluentValidation验证不同集合的长度

[英]FluentValidation to validate the length of a disparate collection

Given the following seven data elements, I must create a validation rule that the collective length of the elements does not exceed 315 characters. 给定以下七个数据元素,我必须创建一个验证规则,以确保元素的总长度不超过315个字符。 AssetType is an enumerator of types and if one of those types does not accurately describe the asset, AssetTypeOtherDescription is used. AssetType是类型的枚举器,如果其中一种类型不能准确描述资产,则使用AssetTypeOtherDescription。 Any ideas on how I could implement this validation? 关于如何实施此验证的任何想法?

COLLATERAL.PLEDGED_ASSET.OWNED_PROPERTY.PROPERTY.ADDRESS.AddressLineText
COLLATERAL.PLEDGED_ASSET.OWNED_PROPERTY.PROPERTY.ADDRESS.CityName
COLLATERAL.PLEDGED_ASSET.OWNED_PROPERTY.PROPERTY.ADDRESS.PostalCode
COLLATERAL.PLEDGED_ASSET.OWNED_PROPERTY.PROPERTY.ADDRESS.StateCode
COLLATERAL.PLEDGED_ASSET.ASSET_DETAIL.AssetType
COLLATERAL.PLEDGED_ASSET.ASSET_DETAIL.AssetTypeOtherDescription
COLLATERAL.PLEDGED_ASSET.OWNED_PROPERTY.PROPERTY.LEGAL_DESCRIPTIONS.LEGAL_DESCRIPTION

How about this: 这个怎么样:

RuleFor(x => x).Must(YourRequest)

private bool YourRequest(COLLATERAL coll)
{
   var result = false;

   //your Logic

   return result;
}

you can finde a full example here 您可以在此处找到完整的示例


private bool YourRequest(COLLATERAL coll)
{
   var result = false;

   if(coll != null 
   && coll.PLEDGED_ASSET_PROPERTY != null
   && coll.PLEDGED_ASSET_PROPERTY.PROPERTY != null
   && coll.PLEDGED_ASSET_PROPERTY.PROPERTY.ADDRESS != null
   && coll.PLEDGED_ASSET.OWNED_PROPERTY != null
   && coll.PLEDGED_ASSET.OWNED_PROPERTY.PROPERTY != null
   && coll.PLEDGED_ASSET.ASSET_DETAIL != null)
   { 
        var charcount = 0;
        charcount += coll.PLEDGED_ASSET.OWNED_PROPERTY.PROPERTY.ADDRESS.AddressLineText.Count()
        charcount += coll.PLEDGED_ASSET.OWNED_PROPERTY.PROPERTY.ADDRESS.CityName.Count()
        charcount += coll.PLEDGED_ASSET.OWNED_PROPERTY.PROPERTY.ADDRESS.PostalCode.Count()
        charcount += coll.PLEDGED_ASSET.OWNED_PROPERTY.PROPERTY.ADDRESS.StateCode.Count()

        charcount += coll.PLEDGED_ASSET.OWNED_PROPERTY.PROPERTY.LEGAL_DESCRIPTIONS.LEGAL_DESCRIPTION


        //one way to check your enumerator 
        bool isNotAccurately =false;
        foreach(var item in coll.PLEDGED_ASSET.ASSET_DETAIL.AssetType)
        {    
            if(item  == //your Logic for "does not accurately describe"
                isNotAccurately = true; 
        }
        if(isNotAccurately )
            charcount += coll.PLEDGED_ASSET.ASSET_DETAIL.AssetTypeOtherDescription
        else
            foreach(var item in coll.PLEDGED_ASSET.ASSET_DETAIL.AssetType)
            {    
                charcount += item.Count();
            }

        if(charcount < 315)
            result = true;
   }

   return result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM