Exemplo n.º 1
0
        private void ConfigureValidationRules()
        {
            Validator.AddRequiredRule(() => UserName, "User Name is required");

            Validator.AddAsyncRule(nameof(UserName),
                                   async() =>
            {
                var isAvailable = await UserRegistrationService.IsUserNameAvailable(UserName).ToTask();

                return(RuleResult.Assert(isAvailable,
                                         string.Format("User Name {0} is taken. Please choose a different one.", UserName)));
            });

            Validator.AddRequiredRule(() => FirstName, "First Name is required");

            Validator.AddRequiredRule(() => LastName, "Last Name is required");

            Validator.AddRequiredRule(() => Email, "Email is required");

            Validator.AddRule(nameof(Email),
                              () =>
            {
                const string regexPattern =
                    @"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";
                return(RuleResult.Assert(Regex.IsMatch(Email, regexPattern),
                                         "Email must by a valid email address"));
            });

            Validator.AddRequiredRule(() => Password, "Password is required");

            Validator.AddRule(nameof(Password),
                              () => RuleResult.Assert(Password.Length >= 6,
                                                      "Password must contain at least 6 characters"));

            Validator.AddRule(nameof(Password),
                              () => RuleResult.Assert((!Password.All(char.IsLower) &&
                                                       !Password.All(char.IsUpper) &&
                                                       !Password.All(char.IsDigit)),
                                                      "Password must contain both lower case and upper case letters"));

            Validator.AddRule(nameof(Password),
                              () => RuleResult.Assert(Password.Any(char.IsDigit),
                                                      "Password must contain at least one digit"));

            Validator.AddRule(nameof(PasswordConfirmation),
                              () =>
            {
                if (!string.IsNullOrEmpty(Password) && string.IsNullOrEmpty(PasswordConfirmation))
                {
                    return(RuleResult.Invalid("Please confirm password"));
                }

                return(RuleResult.Valid());
            });

            Validator.AddRule(nameof(Password),
                              nameof(PasswordConfirmation),
                              () =>
            {
                if (!string.IsNullOrEmpty(Password) && !string.IsNullOrEmpty(PasswordConfirmation))
                {
                    return(RuleResult.Assert(Password == PasswordConfirmation, "Passwords do not match"));
                }

                return(RuleResult.Valid());
            });

            Validator.AddChildValidatable(() => InterestSelectorViewModel);
        }