public void MulitpleValidationFailureForMultiplePropertiesCreatesAMultipleElementErrorDictionaryEachWithMultipleValues()
        {
            var failures = new List <ValidationFailure>
            {
                new ValidationFailure("Age", "must be 18 or older"),
                new ValidationFailure("Age", "must be 25 or younger"),
                new ValidationFailure("Password", "must contain at least 8 characters"),
                new ValidationFailure("Password", "must contain a digit"),
                new ValidationFailure("Password", "must contain upper case letter"),
                new ValidationFailure("Password", "must contain lower case letter"),
            };

            var actual = new CustomValidationException(failures).Errors;

            actual.Keys.Should().BeEquivalentTo(new string[] { "Password", "Age" });

            actual["Age"].Should().BeEquivalentTo(new string[]
            {
                "must be 25 or younger",
                "must be 18 or older",
            });

            actual["Password"].Should().BeEquivalentTo(new string[]
            {
                "must contain lower case letter",
                "must contain upper case letter",
                "must contain at least 8 characters",
                "must contain a digit",
            });
        }
        public void SingleValidationFailureCreatesASingleElementErrorDictionary()
        {
            var failures = new List <ValidationFailure>
            {
                new ValidationFailure("Age", "must be over 18"),
            };

            var actual = new CustomValidationException(failures).Errors;

            actual.Keys.Should().BeEquivalentTo(new string[] { "Age" });
            actual["Age"].Should().BeEquivalentTo(new string[] { "must be over 18" });
        }
示例#3
0
 public void OnActionExecuting(ActionExecutingContext context)
 {
     if (!context.ModelState.IsValid)
     {
         var exception = new CustomValidationException("Model properties is not valid!");
         foreach (var state in context.ModelState)
         {
             foreach (var error in state.Value.Errors)
             {
                 exception.ValidationErrors.Add(new ValidationResult(error.ErrorMessage, new[] { state.Key }));
             }
         }
         throw exception;
     }
 }
        public void DefaultConstructorCreatesAnEmptyErrorDictionary()
        {
            var actual = new CustomValidationException().Errors;

            actual.Keys.Should().BeEquivalentTo(Array.Empty <string>());
        }
 private IHttpActionResult ReturnCustomError(System.Web.Http.ModelBinding.ModelStateDictionary modleState, CustomValidationException customEx)
 {
     foreach (var dicItem in customEx.ErrorCollection)
     {
         modleState.AddModelError(dicItem.Key, dicItem.Value);
     }
     return(BadRequest(modleState));
 }