コード例 #1
0
        public AcceptInviteValidator(IInviteRepository inviteRepository,
                                     IMemberRepository memberRepository,
                                     IGuildRepository guildRepository)
        {
            RuleFor(x => x.Id)
            .NotEmpty()
            .MustAsync(async(id, _) => await inviteRepository.ExistsWithIdAsync(id))
            .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Invite), x.Invite.Id));

            RuleFor(x => x.Invite)
            .NotEmpty().NotEqual(new NullInvite())
            .WithMessage("Invite was null or empty.");

            RuleFor(x => x.Invite.Status).IsInEnum().Equal(InviteStatuses.Pending);

            RuleFor(x => x.Invite.MemberId)
            .NotEmpty()
            .MustAsync(async(memberId, _) => await memberRepository.ExistsWithIdAsync(memberId))
            .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Member), x.Invite.MemberId));

            RuleFor(x => x.Invite.GuildId)
            .NotEmpty()
            .MustAsync(async(guildId, _) => await guildRepository.ExistsWithIdAsync(guildId))
            .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Guild), x.Invite.GuildId));
        }
コード例 #2
0
        public UpdateGuildValidator(IGuildRepository guildRepository, IMemberRepository memberRepository)
        {
            RuleFor(x => x.Id)
            .NotEmpty()
            .MustAsync(async(id, cancellationToken) => await guildRepository.ExistsWithIdAsync(id, cancellationToken))
            .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Guild), x.Id));

            RuleFor(x => x.Name)
            .NotEmpty()
            .MustAsync(async(name, cancellationToken) =>
                       !await memberRepository.ExistsWithNameAsync(name, cancellationToken))
            .WithMessage(x => CommonValidationMessages.ForConflictWithKey(nameof(Guild), x.Name))
            .UnlessAsync(async(x, cancellationToken) =>
            {
                var member = await memberRepository.GetByNameAsync(x.Name, true, cancellationToken);
                return(x.Id.Equals(member.Id));
            });

            RuleFor(x => x.MasterId)
            .NotEmpty()
            .MustAsync(async(masterId, cancellationToken) =>
                       await memberRepository.ExistsWithIdAsync(masterId, cancellationToken))
            .WithMessage("Member chosen for Guild Master not found.");

            RuleFor(x => x)
            .MustAsync(async(x, cancellationToken) =>
                       (await memberRepository.GetByIdAsync(x.MasterId, true, cancellationToken))
                       .GuildId.Equals(x.Id))
            .WithMessage("Member chosen for Guild Master must be a Member of target Guild.");
        }
コード例 #3
0
        public MemberValidator(IGuildRepository guildRepository)
        {
            RuleFor(x => x.Data.Name).NotEmpty();

            RuleFor(x => x.Data)
            .Must(x => x.Memberships.All(m => m.MemberId.Equals(x.Id)))
            .WithMessage(x =>
                         $"Not all {nameof(Membership)}s with '{nameof(Membership.MemberId)}' matching '{x.Data.Id}'.");

            RuleFor(x => x.Data.GuildId).NotEmpty().Unless(x => x.Data.Guild is null);

            RuleFor(x => x.Data.GuildId)
            .MustAsync(async(guildId, _) => await guildRepository.ExistsWithIdAsync(guildId.Value))
            .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Guild), x.Data.GuildId.Value))
            .When(x => x.Data.GuildId.HasValue);

            RuleFor(x => x.Data.Guild.Id)
            .Equal(x => x.Data.GuildId.Value)
            .WithMessage(x =>
                         $"{nameof(Member.GuildId)} and {nameof(Guild)}.{nameof(Member.Guild.Id)} not matching.")
            .Unless(x => x.Data.Guild is null);

            RuleFor(x => x)
            .Must(x => x.Data.Memberships.Any(ms
                                              => ms.MemberId == x.Data.Id &&
                                              ms.GuildId == x.Data.GuildId &&
                                              ms.Until == null &&
                                              !ms.Disabled))
            .WithMessage(x =>
                         $"{nameof(Member)} missing active {nameof(Membership)} for {nameof(Member.GuildId)} '{x.Data.GuildId}'.")
            .When(x => x.Data.Memberships.Any() && x.Data.GuildId.HasValue &&
                  !x.Data.GuildId.Equals(Guid.Empty));
        }
コード例 #4
0
        public void ItShouldNotAcceptNullValues()
        {
            // Act
            var exception = Assert.Throws <ArgumentNullException>(() => new EmailAddress(null));

            // Assert
            Assert.Equal(CommonValidationMessages.ArgumentNull("value"), exception.Message);
        }
コード例 #5
0
 public CreateMemberValidator(IMemberRepository memberRepository)
 {
     RuleFor(x => x.Name)
     .NotEmpty()
     .MustAsync(async(name, cancellationToken) =>
                !await memberRepository.ExistsWithNameAsync(name, cancellationToken))
     .WithMessage(x => CommonValidationMessages.ForConflictWithKey(nameof(Member), x.Name));
 }
コード例 #6
0
        public CreateGuildValidator(IGuildRepository guildRepository, IMemberRepository memberRepository)
        {
            RuleFor(x => x.Name)
            .NotEmpty()
            .MustAsync(async(name, cancellationToken) =>
                       !await guildRepository.ExistsWithNameAsync(name, cancellationToken))
            .WithMessage(x => CommonValidationMessages.ForConflictWithKey(nameof(Guild), x.Name));

            RuleFor(x => x.MasterId)
            .NotEmpty()
            .WithMessage("No Member key reference was used to created this the Guild as guild master.")
            .MustAsync(async(masterId, cancellationToken) =>
                       await memberRepository.ExistsWithIdAsync(masterId, cancellationToken))
            .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Member), x.MasterId));
        }
コード例 #7
0
        public CancelInviteValidator(IInviteRepository inviteRepository)
        {
            RuleFor(x => x.Id)
            .NotEmpty()
            .MustAsync(async(id, _) => await inviteRepository.ExistsWithIdAsync(id))
            .WithMessage(x => CommonValidationMessages.ForConflictWithKey(nameof(Invite), x.Id));

            RuleFor(x => x.Invite)
            .NotEmpty().NotEqual(new NullInvite())
            .WithMessage("Invite was null or empty.");

            RuleFor(x => x.Invite.Status).IsInEnum().Equal(InviteStatuses.Pending);

            RuleFor(x => x.Invite.MemberId).NotEmpty();

            RuleFor(x => x.Invite.GuildId).NotEmpty();
        }
コード例 #8
0
        public UpdateMemberValidator(IMemberRepository memberRepository, IGuildRepository guildRepository)
        {
            RuleFor(x => x.Id)
            .NotEmpty()
            .MustAsync(async(id, _) => await memberRepository.ExistsWithIdAsync(id))
            .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Member), x.Id));

            RuleFor(x => x.Name)
            .NotEmpty()
            .MustAsync(async(name, _) => !await memberRepository.ExistsWithNameAsync(name))
            .WithMessage(x => CommonValidationMessages.ForConflictWithKey(nameof(Member), x.Name))
            .Unless(x => x.Id.Equals(memberRepository.Query().SingleOrDefault(y => y.Name.Equals(x.Name)).Id));

            RuleFor(x => x.GuildId)
            .MustAsync(async(guildId, _) => await guildRepository.ExistsWithIdAsync(guildId))
            .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Guild), x.GuildId))
            .When(x => x.GuildId != Guid.Empty && x.GuildId != null);
        }
コード例 #9
0
        public LeaveGuildValidator(IMemberRepository memberRepository, IGuildRepository guildRepository)
        {
            RuleFor(x => x.Id)
            .NotEmpty()
            .MustAsync(async(id, _) => await memberRepository.ExistsWithIdAsync(id))
            .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Member), x.Id));

            RuleFor(x => x.Member)
            .NotEmpty().NotEqual(new NullMember())
            .WithMessage("Member was null or empty.");

            RuleFor(x => x.Member.GuildId)
            .NotEmpty().WithMessage("Missing a guild key reference.");

            var guildNotEmptyMessage = "Members out of a guild do not have one to leave from.";

            RuleFor(x => x.Member.Guild)
            .NotEmpty().WithMessage(guildNotEmptyMessage)
            .NotEqual(new NullGuild()).WithMessage(guildNotEmptyMessage);
        }
コード例 #10
0
        public InviteMemberValidator(IMemberRepository memberRepository, IGuildRepository guildRepository)
        {
            RuleFor(x => x.MemberId)
            .NotEmpty()
            .MustAsync(async(memberId, _) => await memberRepository.ExistsWithIdAsync(memberId))
            .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Member), x.MemberId));

            RuleFor(x => x.GuildId)
            .NotEmpty()
            .MustAsync(async(guildId, _) => await guildRepository.ExistsWithIdAsync(guildId))
            .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Guild), x.GuildId));

            RuleFor(x => x)
            .MustAsync(async(x, _) => !await guildRepository.ExistsAsync(y =>
                                                                         y.Id.Equals(x.GuildId) &&
                                                                         y.Members.Any(m => m.Id.Equals(x.MemberId))
                                                                         ))
            .WithMessage(x => string.Format(
                             "{0} already in target {1} with key {2}", nameof(Member), nameof(Guild), x.GuildId));
        }
コード例 #11
0
        public MembershipValidator(IGuildRepository guildRepository, IMemberRepository memberRepository)
        {
            RuleFor(x => x.Since)
            .NotEmpty()
            .WithErrorCode(CommonValidationMessages.ConflictCodeString)
            .WithMessage($"{nameof(Membership)} must have a {nameof(Membership.Since)} start date.");

            RuleFor(x => x.MemberId)
            .NotEmpty()
            .MustAsync(async(x, _) => await memberRepository.ExistsAsync(y => y.Id.Equals(x)))
            .WithErrorCode(CommonValidationMessages.ConflictCodeString)
            .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Member), x.MemberId));

            RuleFor(x => x.GuildId)
            .NotEmpty()
            .MustAsync(async(x, _) => await guildRepository.ExistsAsync(y => y.Id.Equals(x)))
            .WithErrorCode(CommonValidationMessages.ConflictCodeString)
            .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Guild), x.GuildId));

            RuleFor(x => x.Member.Id).Equal(x => x.MemberId).Unless(x => x.Member is null);

            RuleFor(x => x.Guild.Id).Equal(x => x.GuildId).Unless(x => x.Guild is null);
        }
コード例 #12
0
        public DemoteMemberValidator(IMemberRepository memberRepository, IGuildRepository guildRepository)
        {
            RuleFor(x => x.Id)
            .NotEmpty()
            .MustAsync(async(id, _) => await memberRepository.ExistsWithIdAsync(id))
            .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Member), x.Id));

            RuleFor(x => x.Member.IsGuildMaster)
            .Equal(true)
            .WithMessage("Member are not a Guild Master and cannot be demoted.");

            RuleFor(x => x.Member)
            .NotEmpty().NotEqual(new NullMember())
            .WithMessage("Member was null or empty.");

            RuleFor(x => x.Member.GuildId)
            .NotEmpty().WithMessage("Missing a guild key reference.");

            var guildNotEmptyMessage = "Members out of a guild cannot be demoted.";

            RuleFor(x => x.Member.Guild)
            .NotEmpty().WithMessage(guildNotEmptyMessage)
            .NotEqual(new NullGuild()).WithMessage(guildNotEmptyMessage);
        }