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)); }
public CreateGuildValidator(IGuildRepository guildRepository, IMemberRepository memberRepository) { RuleFor(x => x.Name).NotEmpty(); RuleFor(x => x.LeaderId).NotEmpty(); When(x => x.Name != string.Empty && x.LeaderId != Guid.Empty, () => { RuleFor(x => x) .MustAsync(async(x, ct) => !await guildRepository.ExistsWithNameAsync(x.Name, ct)) .WithMessage(x => $"Record already exists for guild with given name {x.Name}.") .WithName(nameof(Member.Name)) .WithErrorCode(nameof(HttpStatusCode.Conflict)) .MustAsync((x, ct) => memberRepository.ExistsWithIdAsync(x.LeaderId, ct)) .WithMessage(x => $"Record not found for leader with given id {x.LeaderId}.") .WithName(x => nameof(x.LeaderId)) .WithErrorCode(nameof(HttpStatusCode.NotFound)); }); }