Exemple #1
0
        protected override async Task <ValidationBag> Validate(UpdateCustomerRequest request, CancellationToken cancellationToken)
        {
            var bag = new ValidationBag();

            var customer = await _financialContext.Customers.FindAsync(request.Customer.Id);

            if (customer == null)
            {
                bag.AddError(ValidationErrorCode.CustomerNotFound, new { request.Customer.Id });
            }

            if (request.Customer.Name.Length > 250)
            {
                bag.AddError(ValidationErrorCode.InvalidMaxNameLength, new { request.Customer.Name });
            }
            if (request.Customer.Surname.Length > 250)
            {
                bag.AddError(ValidationErrorCode.InvalidMaxSurnameLength, new { request.Customer.Surname });
            }
            if (string.IsNullOrWhiteSpace(request.Customer.Name))
            {
                bag.AddError(ValidationErrorCode.InvalidMinNameLength, new { request.Customer.Name });
            }
            if (string.IsNullOrWhiteSpace(request.Customer.Surname))
            {
                bag.AddError(ValidationErrorCode.InvalidMinSurnameLength, new { request.Customer.Surname });
            }


            return(bag);
        }
        protected override void Establish_context()
        {
            base.Establish_context();

            _validationBag = new ValidationBag();

            _command = new CreateArticleCommandTestDataBuilder();
        }
Exemple #3
0
        public Task Validate(T instance, ValidationBag bag)
        {
            var result = base.Validate(instance);

            foreach (var error in result.Errors)
            {
                error.AddErrorToValidationBag(bag);
            }
            return(Task.CompletedTask);
        }
        protected override async Task <ValidationBag> Validate(GetCustomerRequest request, CancellationToken cancellationToken)
        {
            var bag = new ValidationBag();

            if (!await _financialContext.Customers.AnyAsync(_ => _.Id == request.CustomerId, cancellationToken))
            {
                bag.AddError(ValidationErrorCode.CustomerNotFound);
            }

            return(bag);
        }
Exemple #5
0
        protected override async Task <ValidationBag> Validate(GetUserByIdRequest request, CancellationToken cancellationToken)
        {
            var bag = new ValidationBag();

            var user = await _userRepository.Single(_ => _.Id == request.Id);

            if (user == null)
            {
                bag.AddError(ValidationErrorCode.UserDoesNotExist, $"There is no user for id {request.Id}.");
            }

            return(bag);
        }
Exemple #6
0
        public void Verify_error_when_language_is_null_with_validation_bag()
        {
            var validationBag = new ValidationBag();
            var query         = new GetErrorMessageQueryTestDataBuilder()
                                .WithLanguage(null);

            SUT.Validate(query, validationBag)
            .GetAwaiter()
            .GetResult();

            validationBag.HasErrors().ShouldBeTrue();
            validationBag.ErrorMessages.Count.ShouldBeEqualTo(1);
            validationBag.ErrorMessages.Any(x => x.Key == nameof(ErrorResources.LanguageNotEmpty)).ShouldBeTrue();
        }
        protected override async Task <ValidationBag> Validate(AddUserRequest request, CancellationToken cancellationToken)
        {
            var bag = new ValidationBag();

            if (string.IsNullOrEmpty(request.Name))
            {
                bag.AddError(ValidationErrorCode.InvalidName);
            }

            if (string.IsNullOrEmpty(request.Email))
            {
                bag.AddError(ValidationErrorCode.InvalidEmail);
            }

            return(bag);
        }
        protected override async Task <ValidationBag> Validate(AddAccountRequest request, CancellationToken cancellationToken)
        {
            var bag = new ValidationBag();

            if (!await _financialContext.Customers.AnyAsync(_ => _.Id == request.CustomerId, cancellationToken))
            {
                bag.AddError(ValidationErrorCode.CustomerNotFound);
            }

            if (decimal.Compare(request.InitialCredit, 0) < 0)
            {
                bag.AddError(ValidationErrorCode.InvalidUnderZeroInitialCredit, new { request.InitialCredit });
            }

            return(bag);
        }
        /// <summary>
        /// Maps the <see cref="ValidationBag"/> to a corresponding <see cref="ValidationResult"/>.
        /// </summary>
        /// <param name="validationBag"></param>
        /// <returns></returns>
        public static ValidationResult ToValidationResult(this ValidationBag validationBag)
        {
            var messages = new List <ValidationResultMessage>();

            foreach (var msg in validationBag.ErrorMessages)
            {
                var item = new ValidationResultMessage()
                {
                    Key     = msg.Key,
                    Message = msg.Message
                };
                messages.Add(item);
            }
            var validationResult = new ValidationResult(messages);

            return(validationResult);
        }
        public void ValidationBag()
        {
            var vb = new ValidationBag();

            Assert.False(vb.IsValid);
            vb.Add(false);
            vb.Add(true);
            Assert.False(vb.IsValid);

            vb = new ValidationBag(true);
            Assert.True(vb.IsValid);
            vb.Add(true);
            vb.Add(false);
            Assert.False(vb.IsValid);

            vb = new ValidationBag();
            vb.Add(true);
            Assert.True(vb.IsValid);
        }
Exemple #11
0
        protected override async Task <ValidationBag> Validate(AddCustomerRequest request, CancellationToken cancellationToken)
        {
            var bag = new ValidationBag();

            if (request.Customer.Name.Length > 250)
            {
                bag.AddError(ValidationErrorCode.InvalidMaxNameLength, new { request.Customer.Name });
            }
            if (request.Customer.Surname.Length > 250)
            {
                bag.AddError(ValidationErrorCode.InvalidMaxSurnameLength, new { request.Customer.Surname });
            }
            if (string.IsNullOrWhiteSpace(request.Customer.Name))
            {
                bag.AddError(ValidationErrorCode.InvalidMinNameLength, new { request.Customer.Name });
            }
            if (string.IsNullOrWhiteSpace(request.Customer.Surname))
            {
                bag.AddError(ValidationErrorCode.InvalidMinSurnameLength, new { request.Customer.Surname });
            }

            return(bag);
        }
Exemple #12
0
        protected override void Establish_context()
        {
            base.Establish_context();

            _validationBag = new ValidationBag();
        }
 public static ValidationBag ExpectNumberOfErrors(this ValidationBag entity, int count)
 {
     entity.ErrorMessages.Count.ShouldBeEqualTo(count);
     return(entity);
 }
 public static ValidationBag ExpectNoErrorWithCode(this ValidationBag entity, String code)
 {
     entity.ErrorMessages.FirstOrDefault(x => x.Key == code).ShouldBeNull();
     return(entity);
 }
Exemple #15
0
 public ErrorMessagesCqrsService(IMediator mediator, IMapper mapper, ValidationBag validationBag)
     : base(mediator, mapper, validationBag)
 {
 }
 public static ValidationBag ExpectAnError(this ValidationBag entity)
 {
     entity.HasErrors().ShouldBeTrue("At least on error was expected in the ValidationBag");
     return(entity);
 }
 public GenericPipelineBehavior(IServiceProvider serviceProvider, ValidationBag validationBag)
 {
     _serviceProvider = serviceProvider;
     _validationBag   = validationBag;
 }
Exemple #18
0
 public CqrsService(IMediator mediator, IMapper mapper, ValidationBag validationBag)
 {
     _mediator      = mediator;
     _mapper        = mapper;
     _validationBag = validationBag;
 }
Exemple #19
0
 public HealthCheckCqrsService(IMediator mediator, IMapper mapper, ValidationBag validationBag)
     : base(mediator, mapper, validationBag)
 {
 }
Exemple #20
0
 public CrashCqrsService(IMediator mediator, IMapper mapper, ValidationBag validationBag) : base(mediator, mapper, validationBag)
 {
 }
 public static void ExpectNoErrors(this ValidationBag entity)
 {
     entity.HasErrors().ShouldBeFalse();
 }