private void ConfigureValidationRules()
        {
            Validator.AddRule(nameof(FirstName),
                              () => RuleResult.Assert(!string.IsNullOrEmpty(FirstName) && FirstName.Length <= 25, "First Name is required and cannot be more than 25 characters"));

            Validator.AddRule(nameof(MiddleName),
                              () => RuleResult.Assert(string.IsNullOrEmpty(MiddleName) || MiddleName.Length <= 25, "Middle Name cannot be more than 25 characters"));

            Validator.AddRule(nameof(LastName),
                              () => RuleResult.Assert(!string.IsNullOrEmpty(LastName) && LastName.Length <= 25, "Last Name is required and cannot be more than 25 characters"));

            Validator.AddRule(nameof(NickName),
                              () => RuleResult.Assert(string.IsNullOrEmpty(NickName) || NickName.Length <= 40, "Alias cannot be more than 40 characters"));

            Validator.AddRequiredRule(() => FollowUpSettings.PreferredContactMethod, "Contact Preference is required");

            Validator.AddAsyncRule(() => Email,
                                   async() =>
            {
                var verifyViaService = (!Email.EmailVerified || (Email.EmailAddress != _originalProspect.Email.EmailAddress)) &&
                                       !String.IsNullOrEmpty(Email.EmailAddress);
                var result = !verifyViaService
                                           ? new EmailValidationResult()
                {
                    IsValid = true
                }
                                           : await _emailValidationService.Validate(Email);

                return(RuleResult.Assert(result.IsValid,
                                         string.Format("Valid email required" + (result.DidYouMean != null ? ". Did you mean " + result.DidYouMean : ""))));
            });

            Validator.AddAsyncRule(() => MobilePhone,
                                   async() =>
            {
                var verifyViaService = (!MobilePhone.PhoneVerified || (MobilePhone.Phone != _originalProspect.MobilePhoneNumber.Phone)) &&
                                       !String.IsNullOrEmpty(MobilePhone.Phone);
                var result = !verifyViaService ? true :
                             await _phoneNumberValidationService.Validate(MobilePhone);

                return(RuleResult.Assert(result,
                                         string.Format("Mobile Phone number is invalid")));
            });

            Validator.AddAsyncRule(() => HomePhone,
                                   async() =>
            {
                var verifyViaService = (!HomePhone.PhoneVerified || (HomePhone.Phone != _originalProspect.HomePhoneNumber.Phone)) &&
                                       !String.IsNullOrEmpty(HomePhone.Phone);
                var result = !verifyViaService ? true :
                             await _phoneNumberValidationService.Validate(HomePhone);

                return(RuleResult.Assert(result,
                                         string.Format("Home Phone number is invalid")));
            });

            Validator.AddAsyncRule(() => WorkPhone,
                                   async() =>
            {
                var verifyViaService = (!WorkPhone.PhoneVerified || (WorkPhone.Phone != _originalProspect.WorkPhoneNumber.Phone)) &&
                                       !String.IsNullOrEmpty(WorkPhone.Phone);
                var result = !verifyViaService ? true :
                             await _phoneNumberValidationService.Validate(WorkPhone);

                return(RuleResult.Assert(result,
                                         string.Format("Work Phone number is invalid")));
            });

            Validator.AddAsyncRule(() => StreetAddress,
                                   async() =>
            {
                var verifyViaService = (!StreetAddress.StreetAddressVerified ||
                                        (StreetAddress.AddressLine1 != _originalProspect.StreetAddress.AddressLine1) ||
                                        (StreetAddress.AddressLine2 != _originalProspect.StreetAddress.AddressLine2) ||
                                        (StreetAddress.City != _originalProspect.StreetAddress.City) ||
                                        (StreetAddress.State != _originalProspect.StreetAddress.State) ||
                                        (StreetAddress.PostalCode != _originalProspect.StreetAddress.PostalCode)) &&
                                       (!String.IsNullOrEmpty(StreetAddress.AddressLine1) ||
                                        !String.IsNullOrEmpty(StreetAddress.AddressLine2) ||
                                        !String.IsNullOrEmpty(StreetAddress.City) ||
                                        !String.IsNullOrEmpty(StreetAddress.State) ||
                                        !String.IsNullOrEmpty(StreetAddress.PostalCode));

                var result = !verifyViaService
                                           ? true :
                             await _streetValidationService.Validate(StreetAddress);

                return(RuleResult.Assert(result,
                                         string.Format("Street Address is invalid")));
            });

            Validator.AddRule(() => ActiveTrafficSourceDetail,
                              () =>
            {
                var result = !(Prospect.ProspectCommunity.LeadId == 0 && (ActiveTrafficSourceDetail == null || ActiveTrafficSourceDetail.CodeId == 0));

                return(RuleResult.Assert(result, string.Format("Traffic Detail required")));
            });

            Validator.AddRule(() => Prospect,
                              () =>
            {
                var result = !(String.IsNullOrEmpty(Email.EmailAddress) &&
                               String.IsNullOrEmpty(MobilePhone.Phone) &&
                               String.IsNullOrEmpty(HomePhone.Phone) &&
                               String.IsNullOrEmpty(WorkPhone.Phone) &&
                               String.IsNullOrEmpty(StreetAddress.AddressLine1) &&
                               String.IsNullOrEmpty(StreetAddress.AddressLine2) &&
                               String.IsNullOrEmpty(StreetAddress.City) &&
                               String.IsNullOrEmpty(StreetAddress.State) &&
                               String.IsNullOrEmpty(StreetAddress.PostalCode));

                return(RuleResult.Assert(result, string.Format("Enter Full Street Address or Phone Number or Email")));
            });

            Validator.AddRule(() => FollowUpSettings,
                              () =>
            {
                var result = !(!FollowUpSettings.ConsentToEmail &&
                               !FollowUpSettings.ConsentToPhone &&
                               !FollowUpSettings.ConsentToMail &&
                               !FollowUpSettings.ConsentToText && !FollowUpSettings.ExcludeFromFollowup);

                return(RuleResult.Assert(result, string.Format("Must Consent or Exclude From Follow Up")));
            });

            Validator.AddRule(() => FollowUpSettings,
                              () =>
            {
                var result = !((FollowUpSettings.ConsentToEmail ||
                                FollowUpSettings.ConsentToPhone ||
                                FollowUpSettings.ConsentToMail ||
                                FollowUpSettings.ConsentToText) && FollowUpSettings.ExcludeFromFollowup);

                return(RuleResult.Assert(result, string.Format("Cannot Consent and Exclude From Follow Up")));
            });

            Validator.AddRule(() => FollowUpSettings,
                              () =>
            {
                var result = !(!FollowUpSettings.ConsentToEmail &&
                               FollowUpSettings.PreferredContactMethod.Equals("Email") && !FollowUpSettings.ExcludeFromFollowup);

                return(RuleResult.Assert(result, string.Format("Must Consent To Email when Contact Preference is Email")));
            });

            Validator.AddRule(() => FollowUpSettings,
                              () =>
            {
                var result = !(!FollowUpSettings.ConsentToPhone &&
                               FollowUpSettings.PreferredContactMethod.Equals("Phone") && !FollowUpSettings.ExcludeFromFollowup);

                return(RuleResult.Assert(result, string.Format("Must Consent To Phone when Contact Preference is Phone")));
            });

            Validator.AddRule(() => FollowUpSettings,
                              () =>
            {
                var result = !(!FollowUpSettings.ConsentToMail &&
                               FollowUpSettings.PreferredContactMethod.Equals("Mail") && !FollowUpSettings.ExcludeFromFollowup);

                return(RuleResult.Assert(result, string.Format("Must Consent To Mail when Contact Preference is Mail")));
            });

            Validator.AddRule(() => FollowUpSettings,
                              () =>
            {
                var result = !(!FollowUpSettings.ConsentToText &&
                               FollowUpSettings.PreferredContactMethod.Equals("Text") && !FollowUpSettings.ExcludeFromFollowup);

                return(RuleResult.Assert(result, string.Format("Must Consent To Text when Contact Preference is Text")));
            });

            Validator.AddRule(() => FollowUpSettings,
                              () =>
            {
                var result = !(String.IsNullOrEmpty(Email.EmailAddress) &&
                               (FollowUpSettings.PreferredContactMethod.Equals("Email") || FollowUpSettings.ConsentToEmail));

                return(RuleResult.Assert(result, string.Format("Email required when Consenting To Email or Contact Preference is Email")));
            });

            Validator.AddRule(() => FollowUpSettings,
                              () =>
            {
                var result = !(String.IsNullOrEmpty(MobilePhone.Phone) &&
                               String.IsNullOrEmpty(HomePhone.Phone) &&
                               String.IsNullOrEmpty(WorkPhone.Phone) &&
                               (FollowUpSettings.PreferredContactMethod.Equals("Phone") || FollowUpSettings.ConsentToPhone));

                return(RuleResult.Assert(result, string.Format("Phone required when Consenting To Phone or Contact Preference is Phone")));
            });

            Validator.AddRule(() => FollowUpSettings,
                              () =>
            {
                var result = !(String.IsNullOrEmpty(StreetAddress.AddressLine1) &&
                               String.IsNullOrEmpty(StreetAddress.AddressLine2) &&
                               String.IsNullOrEmpty(StreetAddress.City) &&
                               String.IsNullOrEmpty(StreetAddress.State) &&
                               String.IsNullOrEmpty(StreetAddress.PostalCode) &&
                               (FollowUpSettings.PreferredContactMethod.Equals("Mail") || FollowUpSettings.ConsentToMail));

                return(RuleResult.Assert(result, string.Format("Street Address required when Consenting To Mail or Contact Preference is Mail")));
            });

            Validator.AddRule(() => FollowUpSettings,
                              () =>
            {
                var result = !(String.IsNullOrEmpty(MobilePhone.Phone) &&
                               (FollowUpSettings.PreferredContactMethod.Equals("Text") || FollowUpSettings.ConsentToText));

                return(RuleResult.Assert(result, string.Format("Mobile Phone required when Consenting To Text or Contact Preference is Text")));
            });

            Validator.AddRule(() => ActiveExcludeReason,
                              () =>
            {
                var result = !(FollowUpSettings.ExcludeFromFollowup && String.IsNullOrEmpty(FollowUpSettings.ExcludeReason));

                return(RuleResult.Assert(result, string.Format("Exclude Reason required")));
            });
        }
Beispiel #2
0
        private void ConfigureValidationRules()
        {
            Validator.AddRule(nameof(FirstName),
                              () => RuleResult.Assert(!string.IsNullOrEmpty(FirstName) && FirstName.Length <= 25, "First Name is required and cannot be more than 25 characters"));

            Validator.AddRule(nameof(MiddleName),
                              () => RuleResult.Assert(string.IsNullOrEmpty(MiddleName) || MiddleName.Length <= 25, "Middle Name cannot be more than 25 characters"));

            Validator.AddRule(nameof(LastName),
                              () => RuleResult.Assert(!string.IsNullOrEmpty(LastName) && LastName.Length <= 25, "Last Name is required and cannot be more than 25 characters"));

            Validator.AddRule(nameof(NickName),
                              () => RuleResult.Assert(string.IsNullOrEmpty(NickName) || NickName.Length <= 40, "Alias cannot be more than 40 characters"));


            Validator.AddAsyncRule(() => Email,
                                   async() =>
            {
                var verifyViaService = (!Email.EmailVerified || (Email.EmailAddress != _originalCobuyer.Email.EmailAddress)) &&
                                       !String.IsNullOrEmpty(Email.EmailAddress);
                var result = !verifyViaService
                                           ? new EmailValidationResult()
                {
                    IsValid = true
                }
                                           : await _emailValidationService.Validate(Email);

                return(RuleResult.Assert(result.IsValid,
                                         string.Format("Valid email required" + (result.DidYouMean != null ? ". Did you mean " + result.DidYouMean : ""))));
            });

            Validator.AddAsyncRule(() => MobilePhone,
                                   async() =>
            {
                var verifyViaService = (!MobilePhone.PhoneVerified || (MobilePhone.Phone != _originalCobuyer.MobilePhoneNumber.Phone)) &&
                                       !String.IsNullOrEmpty(MobilePhone.Phone);
                var result = !verifyViaService ? true :
                             await _phoneNumberValidationService.Validate(MobilePhone);

                return(RuleResult.Assert(result,
                                         string.Format("Mobile Phone number is invalid")));
            });

            Validator.AddAsyncRule(() => HomePhone,
                                   async() =>
            {
                var verifyViaService = (!HomePhone.PhoneVerified || (HomePhone.Phone != _originalCobuyer.HomePhoneNumber.Phone)) &&
                                       !String.IsNullOrEmpty(HomePhone.Phone);
                var result = !verifyViaService ? true :
                             await _phoneNumberValidationService.Validate(HomePhone);

                return(RuleResult.Assert(result,
                                         string.Format("Home Phone number is invalid")));
            });

            Validator.AddAsyncRule(() => WorkPhone,
                                   async() =>
            {
                var verifyViaService = (!WorkPhone.PhoneVerified || (WorkPhone.Phone != _originalCobuyer.WorkPhoneNumber.Phone)) &&
                                       !String.IsNullOrEmpty(WorkPhone.Phone);
                var result = !verifyViaService ? true :
                             await _phoneNumberValidationService.Validate(WorkPhone);

                return(RuleResult.Assert(result,
                                         string.Format("Work Phone number is invalid")));
            });

            Validator.AddAsyncRule(() => StreetAddress,
                                   async() =>
            {
                var verifyViaService = (!StreetAddress.StreetAddressVerified ||
                                        (StreetAddress.AddressLine1 != _originalCobuyer.StreetAddress.AddressLine1) ||
                                        (StreetAddress.AddressLine2 != _originalCobuyer.StreetAddress.AddressLine2) ||
                                        (StreetAddress.City != _originalCobuyer.StreetAddress.City) ||
                                        (StreetAddress.State != _originalCobuyer.StreetAddress.State) ||
                                        (StreetAddress.PostalCode != _originalCobuyer.StreetAddress.PostalCode)) &&
                                       (!String.IsNullOrEmpty(StreetAddress.AddressLine1) ||
                                        !String.IsNullOrEmpty(StreetAddress.AddressLine2) ||
                                        !String.IsNullOrEmpty(StreetAddress.City) ||
                                        !String.IsNullOrEmpty(StreetAddress.State) ||
                                        !String.IsNullOrEmpty(StreetAddress.PostalCode));

                var result = !verifyViaService
                                           ? true :
                             await _streetValidationService.Validate(StreetAddress);

                return(RuleResult.Assert(result,
                                         string.Format("Street Address is invalid")));
            });


            Validator.AddRule(() => Cobuyer,
                              () =>
            {
                var result = !(String.IsNullOrEmpty(Email.EmailAddress) &&
                               String.IsNullOrEmpty(MobilePhone.Phone) &&
                               String.IsNullOrEmpty(HomePhone.Phone) &&
                               String.IsNullOrEmpty(WorkPhone.Phone) &&
                               String.IsNullOrEmpty(StreetAddress.AddressLine1) &&
                               String.IsNullOrEmpty(StreetAddress.AddressLine2) &&
                               String.IsNullOrEmpty(StreetAddress.City) &&
                               String.IsNullOrEmpty(StreetAddress.State) &&
                               String.IsNullOrEmpty(StreetAddress.PostalCode));

                return(RuleResult.Assert(result, string.Format("Enter Full Street Address or Phone Number or Email")));
            });
        }