public bool IsValid(IPostalCodeModel model) { var postalCodeProperty = model.GetType().GetProperty("PostalCode"); var attribute = postalCodeProperty.GetCustomAttribute(typeof(PostalCodeAttribute)) as PostalCodeAttribute; // Model doesn't implement PostalCodeAttribute if (attribute == null) { return(true); } return(ValidatePostalCode(_regualrExpressionService, model, attribute.Country)); }
public bool IsValid(IPostalCodeModel model) { var postalCodeProperty = model.GetType().GetProperty("PostalCode"); var attribute = (PostalCodeAttribute)postalCodeProperty.GetCustomAttribute(typeof(PostalCodeAttribute)); if (attribute is USPostalCodeAttribute) { return(ValidateUsPostalCode(model)); } else if (attribute is GBPostalCodeAttribute) { return(ValidateGBPostalCode(model)); } else { throw new NotSupportedException("This country's postal code is not supported"); } }