Beispiel #1
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            MobilePhoneRegex = "^(\\+44\\s?7\\d{3}|\\(?07\\d{3}\\)?)\\s?\\d{3}\\s?\\d{3}$";
            // We have to get the selection value of the property on which the logic is dependent (ContactPreference)
            var propertyContactPreference = validationContext.ObjectType.GetProperty(this.DependsOn);

            if (propertyContactPreference == null)
            {
                throw new MissingMemberException("Missing member DependsOn - Should be set to contact preference.");
            }

            var contactPref = (CommonEnums.Channel)propertyContactPreference.GetValue(validationContext.ObjectInstance);

            // Get all properties, where MobilePhoneAttribute is applied
            var propertyTelephoneNumbers = validationContext.ObjectType.GetProperties().Where(p => p.CustomAttributes.Any(a => a.AttributeType == typeof(MobilePhoneAttribute)));

            if (propertyTelephoneNumbers == null || propertyTelephoneNumbers.Count() == 0)
            {
                throw new InvalidOperationException("Cannot find properties referencing MobilePnoneAttribute - Expected telephone and alternative telephone.");
            }

            if (contactPref == CommonEnums.Channel.Text || contactPref == CommonEnums.Channel.Mobile) // The logic is applied only if selection of the ContactPreference is Text
            {
                // If ANY of the validated properties is Mobile number validation rule is satisfied. If not ErrorMessage is returned as validation result
                bool hasMobileNumber = propertyTelephoneNumbers.Any(p => ServiceFunctions.IsValidRegexValue(p.GetValue(validationContext.ObjectInstance)?.ToString(), MobilePhoneRegex));
                if (!hasMobileNumber)
                {
                    return(new ValidationResult(ErrorMessage));
                }
            }

            return(null);
        }
        public override ValidationResult IsConditionalValid(string property, object value, ValidationContext validationContext)
        {
            var dependsOnProp = validationContext.ObjectType.GetProperty(property.Trim());
            var postcode      = dependsOnProp.GetValue(validationContext.ObjectInstance)?.ToString();

            if (!string.IsNullOrEmpty(UKPostCodeRegex) && !string.IsNullOrEmpty(EnglishOrBFPOPostCodeRegex) && !string.IsNullOrEmpty(BfpoPostCodeRegex) && !string.IsNullOrEmpty(postcode?.Trim()))
            {
                if (IsNotBfpo)
                {
                    // It applies to Town
                    if (!ServiceFunctions.IsValidRegexValue(postcode, BfpoPostCodeRegex) && string.IsNullOrEmpty(value?.ToString()))
                    {
                        return(new ValidationResult(ErrorMessage));
                    }
                } // It applies to AddressLine2 & AddressLine3 (when other dependent property is valid UK BFPO postcode)
                else if (!IsNonEnglishBfpo && ServiceFunctions.IsValidDoubleRegexValue(postcode, UKPostCodeRegex, BfpoPostCodeRegex, true) && string.IsNullOrEmpty(value?.ToString()))
                {
                    return(new ValidationResult(ErrorMessage));
                }

                // It applies to AlternativePostCode
                // Here we are evaluating the value of the HomePostCode (postcode), not AlternativePostCode
                if (IsNonEnglishBfpo && !ServiceFunctions.IsValidDoubleRegexValue(postcode, UKPostCodeRegex, EnglishOrBFPOPostCodeRegex, true) && string.IsNullOrEmpty(value?.ToString()))
                {
                    return(new ValidationResult(ErrorMessage));
                }
            }

            return(null);
        }
Beispiel #3
0
        public override bool IsValid(object value)
        {
            string objectValue = Convert.ToString(value);

            return((!IsRequired && string.IsNullOrEmpty(objectValue)) || ServiceFunctions.IsValidRegexValue(objectValue, Regex));
        }
        public void When_IsRegexCalledWithValidString_ReturnTrue()
        {
            var result = ServiceFunctions.IsValidRegexValue("07867565456", "^(\\+44\\s?7\\d{3}|\\(?07\\d{3}\\)?)\\s?\\d{3}\\s?\\d{3}$");

            Assert.True(result);
        }
        public void When_IsRegexCalledWithEmptyString_ReturnFalse()
        {
            var result = ServiceFunctions.IsValidRegexValue("", "");

            Assert.False(result);
        }