Ejemplo n.º 1
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var config = DependencyResolver.Current.GetService <AppConfiguration>();

            if (config.Environment.Equals("LIVE", StringComparison.InvariantCultureIgnoreCase))
            {
                var instance = validationContext.ObjectInstance;
                var type     = instance.GetType();

                var competentAuthorityPropertyValue = type.GetProperty(competentAuthorityProperty)
                                                      .GetValue(instance, null);

                if (!typeof(UKCompetentAuthority).IsAssignableFrom(competentAuthorityPropertyValue.GetType()))
                {
                    throw new InvalidOperationException("The competentAuthorityProperty must be of type " +
                                                        typeof(UKCompetentAuthority).FullName);
                }

                var competentAuthority = (UKCompetentAuthority)competentAuthorityPropertyValue;

                var validEmailAddressDomains =
                    CompetentAuthorityMetadata.GetValidEmailAddressDomains(competentAuthority).ToArray();
                if (validEmailAddressDomains.Any(domain => value.ToString().EndsWith(domain)))
                {
                    return(ValidationResult.Success);
                }
                return(new ValidationResult(string.Format(errorMessage, string.Join(" or ", validEmailAddressDomains)), new[] { validationContext.MemberName }));
            }

            return(ValidationResult.Success);
        }
        public override IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            var baseValidationResults = base.Validate(validationContext).ToList();

            var config = DependencyResolver.Current.GetService <AppConfiguration>();

            if (config.Environment.Equals("LIVE", StringComparison.InvariantCultureIgnoreCase))
            {
                var competentAuthorityEmailDomains = CompetentAuthorityMetadata.GetValidEmailAddressDomains();
                if (competentAuthorityEmailDomains.Any(domain => Email.ToString().EndsWith(domain)))
                {
                    baseValidationResults.Add(new ValidationResult(ApplicantRegistrationResources.EmailNotCompetentAuthority, new[] { "Email" }));
                }
            }

            return(baseValidationResults);
        }