예제 #1
0
        private static ValidationOutcome CreateOutcome(
            IEnumerable <ValidationResult> results)
        {
            var outcome = new ValidationOutcome();

            foreach (var result in results)
            {
                var compositeResult = result as CompositeResult;
                if (compositeResult == null)
                {
                    var error = result.ErrorMessage;
                    foreach (var memberName in result.MemberNames)
                    {
                        outcome.AddError(memberName, error);
                    }
                }
                else
                {
                    foreach (var memberName in result.MemberNames)
                    {
                        outcome.AddError(memberName, compositeResult.Outcome);
                    }
                }
            }
            return(outcome);
        }
예제 #2
0
 public Promise ShouldHaveLicensesCoach(Team team, ValidationOutcome outcome)
 {
     return(Promise.Delay(TimeSpan.FromMilliseconds(10)).Then((d, s) =>
     {
         var coach = team.Coach;
         if (coach == null)
         {
             outcome.AddError("Coach", "Coach is required");
         }
         else if (string.IsNullOrEmpty(coach.License))
         {
             outcome.AddError("Coach.License", "Licensed Coach is required");
         }
     }));
 }
예제 #3
0
 public void CreateTeamIntegritey(CreateTeam request, ValidationOutcome outcome)
 {
     if (request.Team == null)
     {
         outcome.AddError("Team", "Something really bad");
     }
 }
예제 #4
0
            public void ShouldHaveFullName(Player player, ValidationOutcome outcome)
            {
                if (string.IsNullOrEmpty(player.FirstName))
                {
                    outcome.AddError("FirstName", "First name is required");
                }

                if (string.IsNullOrEmpty(player.LastName))
                {
                    outcome.AddError("LastName", "Last name is required");
                }

                if (!player.DOB.HasValue)
                {
                    outcome.AddError("DOB", "DOB is required");
                }
            }
예제 #5
0
            public async Task ShouldHaveName(Team player, ValidationOutcome outcome)
            {
                await Task.Delay(10);

                if (string.IsNullOrEmpty(player.Name))
                {
                    outcome.AddError("Name", "Name is required");
                }
            }
예제 #6
0
 private static void AddErrors(ValidationResult result, ValidationOutcome outcome)
 {
     foreach (var error in result.Errors)
     {
         var child   = error as OutcomeFailure;
         var failure = child?.FailedOutcome ?? (object)error.ErrorMessage;
         outcome.AddError(error.PropertyName, failure);
     }
 }
        public void Should_Notify_Nested_Error_Changes()
        {
            string propertyName = null;
            var    outcome      = new ValidationOutcome();

            outcome.ErrorsChanged += (s, e) => propertyName = e.PropertyName;
            outcome.AddError("Company.Name", "Name can't be empty");
            Assert.AreEqual("Company.Name", propertyName);
        }
        public void Should_Add_Simple_Error()
        {
            var outcome = new ValidationOutcome();

            outcome.AddError("Name", "Name can't be empty");
            Assert.AreEqual("Name can't be empty", outcome["Name"]);
            Assert.AreEqual("Name can't be empty", outcome.Error);
            CollectionAssert.AreEqual(new[] { "Name" }, outcome.Culprits);
            CollectionAssert.Contains(
                outcome.GetErrors("Name").Cast <object>().ToArray(),
                "Name can't be empty"
                );
        }
        public void Should_Add_Nested_Error()
        {
            var outcome = new ValidationOutcome();

            outcome.AddError("Company.Name", "Name can't be empty");
            Assert.AreEqual($"[{Environment.NewLine}Name can't be empty{Environment.NewLine}]",
                            outcome["Company"]);
            CollectionAssert.AreEqual(new[] { "Company" }, outcome.Culprits);
            var company = outcome.GetOutcome("Company");

            Assert.IsFalse(company.IsValid);
            Assert.AreEqual("Name can't be empty", company["Name"]);
            CollectionAssert.AreEqual(new[] { "Name" }, company.Culprits);
            CollectionAssert.Contains(
                outcome.GetErrors("Company").Cast <object>().ToArray(),
                company);
        }
        private HttpResponseMessage CreateInvalidRequestResponse()
        {
            var outcome = new ValidationOutcome();

            foreach (var property in ModelState)
            {
                foreach (var error in property.Value.Errors)
                {
                    var key = property.Key;
                    if (key.StartsWith("message."))
                    {
                        key = key.Substring(8);
                    }
                    outcome.AddError(key,
                                     error.Exception?.Message ?? error.ErrorMessage);
                }
            }
            return(CreateErrorResponse(
                       new ValidationException(outcome),
                       HttpStatusCode.BadRequest));
        }
        private IActionResult CreateInvalidResult(JsonSerializerSettings settings)
        {
            var outcome = new ValidationOutcome();

            foreach (var property in ModelState)
            {
                foreach (var error in property.Value.Errors)
                {
                    var key = property.Key;
                    if (key.StartsWith("message."))
                    {
                        key = key.Substring(8);
                    }
                    var message = error.Exception?.Message ?? error.ErrorMessage;
                    outcome.AddError(key, message);
                }
            }

            return(CreateErrorResult(
                       new ValidationException(outcome), settings,
                       StatusCodes.Status400BadRequest));
        }