Example #1
0
        private void Apply(ValidationFailed evt)
        {
            var key = $"{evt.ControlId}-{evt.Validator}";

            if (_controlStatus.ContainsKey(key))
            {
                var item = _controlStatus[key];
                item.Values    = evt.Values;
                item.State     = false;
                item.Message   = evt.Message;
                item.Timestamp = evt.Timestamp;
            }
            else
            {
                var item = new ControlValidatorStatus
                {
                    ControlId = evt.ControlId,
                    Message   = evt.Message,
                    State     = false,
                    Timestamp = evt.Timestamp,
                    Values    = evt.Values,
                    Validator = evt.Validator
                };
                _controlStatus.Add(key, item);
            }
        }
Example #2
0
        /// <summary>
        /// Runs the validation process.
        /// </summary>
        public ValidationStatus Validate()
        {
            var handler = Validator;

            if (handler != null)
            {
                Validating?.Invoke();

                var args = new ValidatorEventArgs(value);

                handler(args);

                if (Status != args.Status)
                {
                    Status = args.Status;

                    if (args.Status == ValidationStatus.Success)
                    {
                        ValidationSucceeded?.Invoke(new ValidationSucceededEventArgs());
                    }
                    else if (args.Status == ValidationStatus.Error)
                    {
                        ValidationFailed?.Invoke(new ValidationFailedEventArgs(args.ErrorText));
                    }

                    Validated?.Invoke(new ValidatedEventArgs(Status, args.ErrorText));

                    StateHasChanged();
                }
            }

            return(Status);
        }
Example #3
0
        /// <summary>
        /// Runs the validation process.
        /// </summary>
        public ValidationStatus Validate()
        {
            if (UsePattern && patternRegex != null)
            {
                var matchStatus = patternRegex.IsMatch(value?.ToString() ?? string.Empty)
                    ? ValidationStatus.Success
                    : ValidationStatus.Error;

                if (Status != matchStatus)
                {
                    Status = matchStatus;

                    if (matchStatus == ValidationStatus.Success)
                    {
                        ValidationSucceeded?.Invoke(new ValidationSucceededEventArgs());
                    }
                    else if (matchStatus == ValidationStatus.Error)
                    {
                        ValidationFailed?.Invoke(new ValidationFailedEventArgs(null));
                    }

                    Validated?.Invoke(new ValidatedEventArgs(Status, null));

                    StateHasChanged();
                }
            }
            else
            {
                var handler = Validator;

                if (handler != null)
                {
                    Validating?.Invoke();

                    var args = new ValidatorEventArgs(value);

                    handler(args);

                    if (Status != args.Status)
                    {
                        Status = args.Status;

                        if (args.Status == ValidationStatus.Success)
                        {
                            ValidationSucceeded?.Invoke(new ValidationSucceededEventArgs());
                        }
                        else if (args.Status == ValidationStatus.Error)
                        {
                            ValidationFailed?.Invoke(new ValidationFailedEventArgs(args.ErrorText));
                        }

                        Validated?.Invoke(new ValidatedEventArgs(Status, args.ErrorText));

                        StateHasChanged();
                    }
                }
            }

            return(Status);
        }
Example #4
0
 private static string GetExceptionMessageByType(ValidationFailed reason)
 {
     return(reason switch
     {
         ValidationFailed.Time => "Time is incorrect. Must be between 9:00 and 17:00",
         ValidationFailed.DayOfWeek => "Day of week is incorrect. Must be between Monday and Friday",
         ValidationFailed.FinishedEarlierThanStarted => "Course cannot be finished before it's started",
         _ => "Unknown"
     });
Example #5
0
        public static ValidationFailed GetFake_Validation()
        {
            var validationErrorResponseList = new List <ValidationError>
            {
                new ValidationError("Code", "FieldName", "Validation description")
            };
            var validationResponse = new ValidationFailed(nameof(Messages.ErrorMessage.ValidationFailed), Messages.ErrorMessage.ValidationFailed, validationErrorResponseList);

            return(validationResponse);
        }
Example #6
0
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!filterContext.ModelState.IsValid)
            {
                // 400 Bad Request
                foreach (var value in filterContext.ModelState.Values)
                {
                    foreach (var error in value.Errors)
                    {
                        if (error.Exception != null || error.ErrorMessage.Contains("line ") && error.ErrorMessage.Contains("position "))
                        {
                            var errorResponse = new BadRequest(nameof(Application.Messages.ErrorMessage.BadRequest), Application.Messages.ErrorMessage.BadRequest);
                            filterContext.Result = new ObjectResult(errorResponse)
                            {
                                StatusCode = 400
                            };
                            return;
                        }
                    }
                }

                // 422 Unprocessable Entity
                var errors = filterContext.ModelState.Where(x => x.Value.Errors.Count > 0).ToDictionary(
                    kvp => kvp.Key,
                    kvp => kvp.Value.Errors.Select(e => e.ErrorMessage).ToArray()
                    );


                var validationErrorsResponse = new List <ValidationError>();
                foreach (var error in errors)
                {
                    foreach (var value in error.Value)
                    {
                        // Handle DataAnotations Required
                        if (value.Contains("field is required"))
                        {
                            validationErrorsResponse.Add(new ValidationError(nameof(Application.Messages.ErrorMessage.Required), error.Key, Application.Messages.ErrorMessage.Required));
                        }
                        else // Handle fluent validations
                        {
                            var index   = value.IndexOf(" ", StringComparison.Ordinal);
                            var code    = value.Substring(0, index);
                            var message = value.Substring(index + 1);
                            validationErrorsResponse.Add(new ValidationError(code, error.Key, message));
                        }
                    }
                }
                var validationsResponse = new ValidationFailed(nameof(Application.Messages.ErrorMessage.ValidationFailed), Application.Messages.ErrorMessage.ValidationFailed, validationErrorsResponse);

                filterContext.Result = new ObjectResult(validationsResponse)
                {
                    StatusCode = 422
                };
            }
        }
        public async Task Given_request_which_fails_payment_gateway_validation_when_process_payment_should_return_4xx_and_status_code()
        {
            var request = PaymentRequestBuilder.BuildPaymentRequestToFailPaymentGatewayValidation();

            var processPaymentResponse = await _client.ProcessPayment(request);

            Assert.That(processPaymentResponse.HttpStatusCode, Is.EqualTo(HttpStatusCode.UnprocessableEntity));
            Assert.That(processPaymentResponse.Content.StatusCode, Is.EqualTo(ValidationFailed.ToString()));

            var getResponse = await _client.Get(processPaymentResponse.Content.PaymentId);

            Assert.That(getResponse.HttpStatusCode, Is.EqualTo(HttpStatusCode.NotFound));
        }
Example #8
0
        void SetPropertyValue <T>(T value, bool ignoreValidationResult, string memberName, Action setPropertyAction)
        {
            string result = null;

            if (ValidateProperty(memberName, value, out result) || ignoreValidationResult)
            {
                setPropertyAction();
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(memberName));
            }
            else
            {
                InvalidateProperties();
                ValidationFailed?.Invoke(this, result);
            }
        }
Example #9
0
        /// <summary>
        /// Runs the validation process.
        /// </summary>
        public ValidationStatus Validate()
        {
            var handler = Validator;

            //Status = ValidationStatus.None;

            if (handler != null)
            {
                Validating?.Invoke();

                var args = new ValidatorEventArgs(value);

                handler(args);

                if (Status != args.Status)
                {
                    Status = args.Status;

                    if (args.Status == ValidationStatus.Success)
                    {
                        ValidationSucceeded?.Invoke(new ValidationSucceededEventArgs());
                    }
                    else if (args.Status == ValidationStatus.Error)
                    {
                        ValidationFailed?.Invoke(new ValidationFailedEventArgs(args.ErrorText));
                    }

                    Validated?.Invoke(new ValidatedEventArgs(Status, args.ErrorText));

                    StateHasChanged();
                }
            }

            // force the reload of all child components
            //StateHasChanged();

            return(Status);
        }
Example #10
0
 public CourseValidationException(ValidationFailed reason)
     : base($"Validation failed. Reason: {GetExceptionMessageByType(reason)}")
 {
 }
Example #11
0
 internal void OnValidationFailed(EntityRecord record)
 {
     ValidationFailed?.Invoke(record, EventArgs.Empty);
 }
 private void Apply(ValidationFailed evt)
 {
     var key = $"{evt.ControlId}-{evt.Validator}";
     if (ControlStatus.ContainsKey(key))
     {
         var item = ControlStatus[key];
         item.Values = evt.Values;
         item.State = false;
         item.Message = evt.Message;
         item.Timestamp = evt.Version;
     }
     else
     {
         var item = new ControlValidatorStatus
         {
             ControlId = evt.ControlId,
             Message = evt.Message,
             State = false,
             Timestamp = evt.Version,
             Values = evt.Values,
             Validator = evt.Validator
         };
         ControlStatus.Add(key, item);
     }
     Version = evt.Version;
 }