I have a validation which checks that a string is a valid asset number. There are currently three formats it could be in, like: 001-123456
, WSH002M52B
, or CTR0025MLD
. The way my code is set up now is like:
public bool IsValidAssetNum(string assetNum)
{
if (this.IsNorthAmericanAssetFormat(assetNum)) return true;
if (this.IsGermanAssetFormat(assetNum)) return true;
if (this.IsAustralianAssetFormat(assetNum)) return true;
return false;
}
With each of those checks being like:
private bool IsNorthAmericanAssetFormat(string assetNum)
{
var regex = new Regex(ComponentsResources.NorthAmericaAssetNumValidationRegex);
var match = regex.Match(assetNum);
return match.Success;
}
In the future, if more formats come up, we'll have to add a new specific format check as well as add it to the IsValidAssetNum
function. Is there a pattern out there to solve this issue? I know a lot of if
statement refactorings come from the command pattern or strategy pattern, but this seems like a different type of issue.