FluentValidation to validate the length of a disparate collection

1.5k views Asked by At

Given the following seven data elements, I must create a validation rule that the collective length of the elements does not exceed 315 characters. AssetType is an enumerator of types and if one of those types does not accurately describe the asset, AssetTypeOtherDescription is used. 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
1

There are 1 answers

1
WiiMaxx On

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;
}