コード例 #1
0
        public RegisterValidator(IPlayerRepository repository, BrandQueries brandQueries)
        {
            CascadeMode = CascadeMode.StopOnFirstFailure;

            RuleFor(p => p.FirstName)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(PlayerInfoValidatorResponseCodes.FirstNameRequired.ToString());

            RuleFor(p => p.LastName)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(PlayerInfoValidatorResponseCodes.LastNameRequired.ToString());

            RuleFor(p => p.Email)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(PlayerInfoValidatorResponseCodes.EmailIsRequired.ToString());

            RuleFor(p => p.PhoneNumber)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(PlayerInfoValidatorResponseCodes.PhoneNumberIsRequired.ToString());

            RuleFor(p => p.MailingAddressLine1)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(PlayerInfoValidatorResponseCodes.MailingAddressLine1IsRequired.ToString());

            RuleFor(p => p.MailingAddressCity)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(PlayerInfoValidatorResponseCodes.MailingAddressCityIsRequired.ToString());

            RuleFor(p => p.MailingAddressPostalCode)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(PlayerInfoValidatorResponseCodes.MailingAddressPostalCodeIsRequired.ToString());

            RuleFor(p => p.PhysicalAddressLine1)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(RegisterValidatorResponseCodes.PhysicalAddressLine1Required.ToString());

            RuleFor(p => p.PhysicalAddressCity)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(RegisterValidatorResponseCodes.PhysicalAddressCityRequired.ToString());

            RuleFor(p => p.PhysicalAddressPostalCode)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(RegisterValidatorResponseCodes.PhysicalAddressPostalCodeRequired.ToString());

            RuleFor(p => p.CountryCode)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(PlayerInfoValidatorResponseCodes.CountryCodeIsRequired.ToString());

            RuleFor(p => p.CurrencyCode)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(RegisterValidatorResponseCodes.CurrencyCodeRequired.ToString());

            RuleFor(p => p.CultureCode)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(RegisterValidatorResponseCodes.CultureCodeRequired.ToString());

            RuleFor(p => p.Username)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(RegisterValidatorResponseCodes.UsernameRequired.ToString());

            RuleFor(p => p.Password)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(RegisterValidatorResponseCodes.PasswordRequired.ToString());

            RuleFor(p => p.PasswordConfirm)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(RegisterValidatorResponseCodes.PasswordConfirmRequired.ToString());

            RuleFor(p => p.DateOfBirth)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(PlayerInfoValidatorResponseCodes.DateOfBirthIsMissingOrIncorrect.ToString());

            RuleFor(p => p.BrandId)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(RegisterValidatorResponseCodes.BrandIdRequired.ToString());

            RuleFor(p => p.Gender)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(PlayerInfoValidatorResponseCodes.GenderIsRequired.ToString());

            RuleFor(p => p.Title)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(PlayerInfoValidatorResponseCodes.TitleIsRequired.ToString());

            RuleFor(p => p.ContactPreference)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(RegisterValidatorResponseCodes.ContactPreferenceRequired.ToString());

            RuleFor(p => p.IdStatus)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(RegisterValidatorResponseCodes.IdStatusRequired.ToString());

            RuleFor(p => p.SecurityQuestionId)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(RegisterValidatorResponseCodes.SecurityQuestionIdRequired.ToString())
            .Must(v => repository.SecurityQuestions.Any(x => x.Id == new Guid(v)))
            .WithMessage(RegisterValidatorResponseCodes.InvalidSecurityQuestionId.ToString());

            RuleFor(p => p.SecurityAnswer)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(RegisterValidatorResponseCodes.SecurityAnswerIsMissing.ToString());

            RuleFor(p => p.MailingAddressStateProvince)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(RegisterValidatorResponseCodes.AddressStateProvinceIsMissing.ToString());

            When(p => !string.IsNullOrWhiteSpace(p.FirstName), () =>
            {
                var firstNameMinLength = CommonPlayerSettings.FirstNameMinLength;
                var firstNameMaxLength = CommonPlayerSettings.FirstNameMaxLength;

                RuleFor(p => p.FirstName)
                .Length(firstNameMinLength, firstNameMaxLength)
                .WithMessage(PlayerInfoValidatorResponseCodes.FirstNameLengthIsNotInTheAllowedRange.ToString());

                RuleFor(p => p.FirstName)
                .Matches(CommonPlayerSettings.FirstNamePattern)
                .WithMessage(PlayerInfoValidatorResponseCodes.FirstNameFormatIsWrong.ToString());
            });

            When(p => !string.IsNullOrWhiteSpace(p.LastName), () =>
            {
                var lastNameMinLength = CommonPlayerSettings.LastNameMinLength;
                var lastNameMaxLength = CommonPlayerSettings.LastNameMaxLength;

                RuleFor(p => p.LastName)
                .Length(lastNameMinLength, lastNameMaxLength)
                .WithMessage(PlayerInfoValidatorResponseCodes.LastNameLengthIsNotInTheAllowedRange.ToString());

                RuleFor(p => p.LastName)
                .Matches(CommonPlayerSettings.LastNamePattern)
                .WithMessage(PlayerInfoValidatorResponseCodes.LastNameFormatIsWrong.ToString());
            });

            When(p => !string.IsNullOrWhiteSpace(p.Email), () =>
            {
                const int emailMaxLength = 50;

                RuleFor(p => p.Email)
                .Length(0, emailMaxLength)
                .WithMessage(PlayerInfoValidatorResponseCodes.EmailLengthIsNotInAllowedRange.ToString());

                RuleFor(p => p.Email)
                .Matches(CommonPlayerSettings.EmailPattern)
                .WithMessage(PlayerInfoValidatorResponseCodes.EmailFormatIsWrong.ToString());

                RuleFor(p => p.Email)
                .Must(
                    (r, e) =>
                    !repository.Players.AsNoTracking()
                    .Any(p => p.Email == e && p.BrandId == new Guid(r.BrandId)))
                .WithMessage(PlayerInfoValidatorResponseCodes.EmailAlreadyExists.ToString());
            });

            When(p => !string.IsNullOrWhiteSpace(p.PhoneNumber), () =>
            {
                const int phoneMinLength = 8, phoneMaxLength = 15;

                RuleFor(p => p.PhoneNumber)
                .Length(phoneMinLength, phoneMaxLength)
                .WithMessage(PlayerInfoValidatorResponseCodes.PhoneNumberLengthIsWrong.ToString());

                RuleFor(p => p.PhoneNumber)
                .Matches(CommonPlayerSettings.PhonePattern)
                .WithMessage(PlayerInfoValidatorResponseCodes.PhoneNumberFormatIsWrong.ToString());
            });

            When(p => !string.IsNullOrWhiteSpace(p.MailingAddressLine1), () =>
            {
                const int addressMinLength = 1, addressMaxLength = 50;

                RuleFor(p => p.MailingAddressLine1)
                .Length(addressMinLength, addressMaxLength)
                .WithMessage(PlayerInfoValidatorResponseCodes.MailingAddressLine1LengthIsIncorrect.ToString());
            });

            When(p => !string.IsNullOrWhiteSpace(p.MailingAddressPostalCode), () =>
            {
                const int zipCodesMinLength = 1, zipCodeMaxLength = 10;

                RuleFor(p => p.MailingAddressPostalCode)
                .Length(zipCodesMinLength, zipCodeMaxLength)
                .WithMessage(PlayerInfoValidatorResponseCodes.MailingAddressPostalCodeLengthIsIncorrect.ToString());
            });

            When(p => brandQueries.DoesBrandExist(new Guid(p.BrandId)) && !string.IsNullOrWhiteSpace(p.CountryCode),
                 () => RuleFor(p => p.CountryCode)
                 .Must((r, c) => brandQueries.BrandHasCountry(new Guid(r.BrandId), c))
                 .WithMessage(PlayerInfoValidatorResponseCodes.InvalidCountryCode.ToString()));

            When(p => brandQueries.DoesBrandExist(new Guid(p.BrandId)) && !string.IsNullOrWhiteSpace(p.CurrencyCode),
                 () => RuleFor(p => p.CurrencyCode)
                 .Must((r, c) => brandQueries.BrandHasCurrency(new Guid(r.BrandId), c))
                 .WithMessage(PlayerInfoValidatorResponseCodes.InvalidCurrencyCode.ToString()));

            When(p => brandQueries.DoesBrandExist(new Guid(p.BrandId)) && !string.IsNullOrWhiteSpace(p.CultureCode),
                 () => RuleFor(p => p.CultureCode)
                 .Must((r, c) => brandQueries.BrandHasCulture(new Guid(r.BrandId), c))
                 .WithMessage(RegisterValidatorResponseCodes.InvalidCultureCode.ToString()));

            When(p => !string.IsNullOrWhiteSpace(p.Username), () =>
            {
                const int usernameMinLength = 6;
                const int usernameMaxLength = 12;

                RuleFor(p => p.Username)
                .Length(usernameMinLength, usernameMaxLength)
                .WithMessage(RegisterValidatorResponseCodes.UsernameLengthIsNotInAllowedRange.ToString());

                RuleFor(p => p.Username)
                .Matches(CommonPlayerSettings.UsernamePatter)
                .WithMessage(RegisterValidatorResponseCodes.UsernameFormatIsWrong.ToString());

                RuleFor(p => p.Username)
                .Must(
                    (r, u) =>
                    !repository.Players.AsNoTracking()
                    .Any(p => p.Username == u && p.BrandId == new Guid(r.BrandId)))
                .WithMessage(RegisterValidatorResponseCodes.UsernameAlreadyExists.ToString());
            });

            When(p => !string.IsNullOrWhiteSpace(p.Password), () =>
            {
                int passwordMinLength = CommonPlayerSettings.PasswordMinLength;
                int passwordMaxLength = CommonPlayerSettings.PasswordMaxLength;

                RuleFor(p => p.Password)
                .Length(passwordMinLength, passwordMaxLength)
                .WithMessage(RegisterValidatorResponseCodes.PasswordLengthIsNotInItsAllowedRange.ToString());
            });

            When(p => !string.IsNullOrWhiteSpace(p.Password) && !string.IsNullOrWhiteSpace(p.PasswordConfirm), () =>
                 RuleFor(p => p.PasswordConfirm)
                 .Must((r, p) => r.Password == p)
                 .WithMessage(RegisterValidatorResponseCodes.ConfirmationPasswordDoesntMatch.ToString())
                 );

            When(p => !string.IsNullOrWhiteSpace(p.DateOfBirth), () =>
            {
                RuleFor(p => p.DateOfBirth)
                .Must(p => ValidatorUtils.ToDateTime(p, "yyyy/MM/dd") != null)
                .WithMessage(PlayerInfoValidatorResponseCodes.DateOfBirthIsMissingOrIncorrect.ToString());

                RuleFor(p => p.DateOfBirth)
                .Must(p =>
                {
                    DateTime?dateTime = ValidatorUtils.ToDateTime(p, "yyyy/MM/dd");
                    if (dateTime == null)
                    {
                        return(true);
                    }
                    return(dateTime >= DateTime.UtcNow.AddYears(-100));
                })
                .WithMessage(PlayerInfoValidatorResponseCodes.DateOfBirthIsMissingOrIncorrect.ToString());

                RuleFor(p => p.DateOfBirth)
                .Must(p =>
                {
                    DateTime?dateTime = ValidatorUtils.ToDateTime(p, "yyyy/MM/dd");
                    if (dateTime == null)
                    {
                        return(true);
                    }
                    return(dateTime <= DateTime.UtcNow.AddYears(-18));
                })
                .WithMessage(PlayerInfoValidatorResponseCodes.AgeIsUnderAllowed.ToString());
            });

            When(p => !string.IsNullOrWhiteSpace(p.BrandId), () => RuleFor(p => p.BrandId)
                 .Must(brand => brandQueries.DoesBrandExist(new Guid(brand)))
                 .WithMessage(RegisterValidatorResponseCodes.BrandIsUnknown.ToString()));

            When(p => !string.IsNullOrEmpty(p.Gender), () => RuleFor(p => p.Gender)
                 .Must(g => Enum.IsDefined(typeof(Gender), g))
                 .WithMessage(PlayerInfoValidatorResponseCodes.GenderIsRequired.ToString()));

            When(p => !string.IsNullOrEmpty(p.Title), () => RuleFor(p => p.Title)
                 .Must(g => Enum.IsDefined(typeof(Title), g))
                 .WithMessage(PlayerInfoValidatorResponseCodes.TitleIsRequired.ToString()));

            When(p => !string.IsNullOrEmpty(p.ContactPreference), () => RuleFor(p => p.ContactPreference)
                 .Must(cp => Enum.IsDefined(typeof(ContactMethod), cp))
                 .WithMessage(RegisterValidatorResponseCodes.InvalidContractPreference.ToString()));
        }
コード例 #2
0
        public EditPlayerValidator(IPlayerRepository repository, BrandQueries brandQueries)
        {
            CascadeMode = CascadeMode.StopOnFirstFailure;

            RuleFor(p => p.PlayerId)
            .Must((r, u) => repository.Players.Any(p => p.Id == u))
            .WithMessage(EditPlayerValidatorResponseCodes.InvalidPlayerId.ToString());

            RuleFor(p => p.Title)
            .Must(v => v.HasValue)
            .WithMessage(PlayerInfoValidatorResponseCodes.TitleIsRequired.ToString());

            RuleFor(p => p.FirstName)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(PlayerInfoValidatorResponseCodes.FirstNameRequired.ToString());

            RuleFor(p => p.LastName)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(PlayerInfoValidatorResponseCodes.LastNameRequired.ToString());

            RuleFor(p => p.Email)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(PlayerInfoValidatorResponseCodes.EmailIsRequired.ToString());

            RuleFor(p => p.Gender)
            .Must(v => v.HasValue)
            .WithMessage(PlayerInfoValidatorResponseCodes.GenderIsRequired.ToString());

            RuleFor(p => p.MailingAddressLine1)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(PlayerInfoValidatorResponseCodes.MailingAddressLine1IsRequired.ToString());

            RuleFor(p => p.MailingAddressCity)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(PlayerInfoValidatorResponseCodes.MailingAddressCityIsRequired.ToString());

            RuleFor(p => p.MailingAddressPostalCode)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(PlayerInfoValidatorResponseCodes.MailingAddressPostalCodeIsRequired.ToString());

            RuleFor(p => p.MailingAddressStateProvince)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(PlayerInfoValidatorResponseCodes.MailingAddressStateProvince.ToString());

            RuleFor(p => p.CountryCode)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(PlayerInfoValidatorResponseCodes.CountryCodeIsRequired.ToString());

            RuleFor(p => p.PhoneNumber)
            .Must(v => !string.IsNullOrWhiteSpace(v))
            .WithMessage(PlayerInfoValidatorResponseCodes.PhoneNumberIsRequired.ToString());

            When(p => !p.AccountAlertEmail, () =>
            {
                RuleFor(p => p.AccountAlertSms)
                .Must(v => v)
                .WithMessage(PlayerInfoValidatorResponseCodes.AccountAlertIsRequired.ToString());
            });

            When(p => !p.AccountAlertSms, () =>
            {
                RuleFor(p => p.AccountAlertEmail)
                .Must(v => v)
                .WithMessage(PlayerInfoValidatorResponseCodes.AccountAlertIsRequired.ToString());
            });

            When(p => !p.MarketingAlertEmail && !p.MarketingAlertPhone, () =>
            {
                RuleFor(p => p.MarketingAlertSms)
                .Must(v => v)
                .WithMessage(PlayerInfoValidatorResponseCodes.MarketingAlertIsRequired.ToString());
            });

            When(p => !p.MarketingAlertSms && !p.MarketingAlertPhone, () =>
            {
                RuleFor(p => p.MarketingAlertEmail)
                .Must(v => v)
                .WithMessage(PlayerInfoValidatorResponseCodes.MarketingAlertIsRequired.ToString());
            });

            When(p => !p.MarketingAlertSms && !p.MarketingAlertEmail, () =>
            {
                RuleFor(p => p.MarketingAlertPhone)
                .Must(v => v)
                .WithMessage(PlayerInfoValidatorResponseCodes.MarketingAlertIsRequired.ToString());
            });

            When(p => !string.IsNullOrWhiteSpace(p.FirstName), () =>
            {
                int firstNameMinLength = CommonPlayerSettings.FirstNameMinLength;
                int firstNameMaxLength = CommonPlayerSettings.FirstNameMaxLength;

                RuleFor(p => p.FirstName)
                .Length(firstNameMinLength, firstNameMaxLength)
                .WithMessage(PlayerInfoValidatorResponseCodes.FirstNameLengthIsNotInTheAllowedRange.ToString());

                RuleFor(p => p.FirstName)
                .Matches(CommonPlayerSettings.FirstNamePattern)
                .WithMessage(PlayerInfoValidatorResponseCodes.FirstNameFormatIsWrong.ToString());
            });

            When(p => !string.IsNullOrWhiteSpace(p.LastName), () =>
            {
                int lastNameMinLength = CommonPlayerSettings.LastNameMinLength;
                int lastNameMaxLength = CommonPlayerSettings.LastNameMaxLength;

                RuleFor(p => p.LastName)
                .Length(lastNameMinLength, lastNameMaxLength)
                .WithMessage(PlayerInfoValidatorResponseCodes.LastNameLengthIsNotInTheAllowedRange.ToString());

                RuleFor(p => p.LastName)
                .Matches(CommonPlayerSettings.LastNamePattern)
                .WithMessage(PlayerInfoValidatorResponseCodes.LastNameFormatIsWrong.ToString());
            });

            When(p => !string.IsNullOrWhiteSpace(p.DateOfBirth), () =>
            {
                RuleFor(p => p.DateOfBirth)
                .Must(p => ValidatorUtils.ToDateTime(p, "yyyy/MM/dd") != null)
                .WithMessage(PlayerInfoValidatorResponseCodes.DateOfBirthIsMissingOrIncorrect.ToString());

                RuleFor(p => p.DateOfBirth)
                .Must(p =>
                {
                    DateTime?dateTime = ValidatorUtils.ToDateTime(p, "yyyy/MM/dd");
                    if (dateTime == null)
                    {
                        return(true);
                    }
                    return(dateTime <= DateTime.UtcNow.AddYears(-18));
                })
                .WithMessage(PlayerInfoValidatorResponseCodes.AgeIsUnderAllowed.ToString());
            });

            When(p => !string.IsNullOrWhiteSpace(p.Email), () =>
            {
                const int emailMaxLength = 50;

                RuleFor(p => p.Email)
                .Length(0, emailMaxLength)
                .WithMessage(PlayerInfoValidatorResponseCodes.EmailLengthIsNotInAllowedRange.ToString());

                RuleFor(p => p.Email)
                .EmailAddress()
                .WithMessage(PlayerInfoValidatorResponseCodes.EmailFormatIsWrong.ToString());

                RuleFor(p => p.Email)
                .Must((r, e) => !repository.Players.AsNoTracking().Any(p => p.Email == e &&
                                                                       p.BrandId == repository.Players.FirstOrDefault(player => player.Id == r.PlayerId).BrandId &&
                                                                       p.Id != r.PlayerId))
                .WithMessage(PlayerInfoValidatorResponseCodes.EmailAlreadyExists.ToString());
            });

            When(p => !string.IsNullOrWhiteSpace(p.PhoneNumber), () =>
            {
                const int phoneMinLength = 8;
                const int phoneMaxLength = 15;

                RuleFor(p => p.PhoneNumber)
                .Length(phoneMinLength, phoneMaxLength)
                .WithMessage(PlayerInfoValidatorResponseCodes.PhoneNumberLengthIsWrong.ToString());

                RuleFor(p => p.PhoneNumber)
                .Matches(@"^((\\+)|(00)|(\\*)|())[0-9]{3,14}((\\#)|())$")
                .WithMessage(PlayerInfoValidatorResponseCodes.PhoneNumberFormatIsWrong.ToString());
            });

            When(p => !string.IsNullOrWhiteSpace(p.MailingAddressLine1), () =>
            {
                const int addressMinLength = 1;
                const int addressMaxLength = 50;

                RuleFor(p => p.MailingAddressLine1)
                .Length(addressMinLength, addressMaxLength)
                .WithMessage(PlayerInfoValidatorResponseCodes.MailingAddressLine1LengthIsIncorrect.ToString());
            });

            When(p => !string.IsNullOrWhiteSpace(p.MailingAddressPostalCode), () =>
            {
                const int zipCodesMinLength = 1;
                const int zipCodeMaxLength  = 10;

                RuleFor(p => p.MailingAddressPostalCode)
                .Length(zipCodesMinLength, zipCodeMaxLength)
                .WithMessage(PlayerInfoValidatorResponseCodes.MailingAddressPostalCodeLengthIsIncorrect.ToString());
            });

            When(p => !string.IsNullOrWhiteSpace(p.CountryCode) && brandQueries.DoesBrandExist(repository.Players.FirstOrDefault(player => player.Id == p.PlayerId).BrandId), () =>
                 RuleFor(p => p.CountryCode)
                 .Must((r, c) => brandQueries.BrandHasCountry(repository.Players.FirstOrDefault(player => player.Id == r.PlayerId).BrandId, c))
                 .WithMessage(PlayerInfoValidatorResponseCodes.InvalidCountryCode.ToString()));

            When(p => !string.IsNullOrWhiteSpace(p.CurrencyCode) && brandQueries.DoesBrandExist(repository.Players.FirstOrDefault(player => player.Id == p.PlayerId).BrandId), () =>
                 RuleFor(p => p.CurrencyCode)
                 .Must((r, c) => brandQueries.BrandHasCurrency(repository.Players.FirstOrDefault(player => player.Id == r.PlayerId).BrandId, c))
                 .WithMessage(PlayerInfoValidatorResponseCodes.InvalidCurrencyCode.ToString()));
        }