/// <summary>
        /// This transfers error messages from the DtoValidation methods to the MVC modelState error dictionary.
        /// It looks for errors that have member names corresponding to the properties in the displayDto.
        /// This means that errors assciated with a field on display will show next to the name.
        /// Other errors will be shown in the ValidationSummary
        /// </summary>
        /// <param name="errorHolder">The interface that holds the errors</param>
        /// <param name="modelState">The MVC modelState to add errors to</param>
        /// <param name="displayDto">This is the Dto that will be used to display the error messages</param>
        public static void CopyErrorsToModelState <T>(this ISuccessOrErrors errorHolder, ModelStateDictionary modelState, T displayDto)
        {
            if (errorHolder.IsValid)
            {
                return;
            }

            var namesThatWeShouldInclude = PropertyNamesInDto(displayDto);

            foreach (var error in errorHolder.Errors)
            {
                if (!error.MemberNames.Any())
                {
                    modelState.AddModelError("", error.ErrorMessage);
                }
                else
                {
                    foreach (var errorKeyName in error.MemberNames)
                    {
                        modelState.AddModelError(
                            (namesThatWeShouldInclude.Any(x => x == errorKeyName) ? errorKeyName : ""),
                            error.ErrorMessage);
                    }
                }
            }
        }
        protected SuccessOrErrors(ISuccessOrErrors nonResultStatus)
        {
            var status = (SuccessOrErrors)nonResultStatus;

            _localWarnings = status._localWarnings;
            _localErrors   = status._localErrors;
        }
Beispiel #3
0
 private void SetAppropriateIndexError(ISuccessOrErrors status, string format, params string [] args)
 {
     if (_showMismatchedIndexsAsErrors)
     {
         status.AddSingleError(format, args);
     }
     else
     {
         status.AddWarning(format, args);
     }
 }
Beispiel #4
0
 internal static void ShouldBeValid(this ISuccessOrErrors status, bool isValid = true)
 {
     if (isValid)
     {
         Xunit.Assert.True(status.IsValid, string.Join("\n", status.Errors));
     }
     else
     {
         Xunit.Assert.False(status.IsValid, "This should have returned some errors");
     }
 }
        /// <summary>
        /// This copies errors for general display where we are not returning to a page with the fields on them
        /// </summary>
        /// <param name="errorHolder"></param>
        /// <param name="modelState"></param>
        public static void CopyErrorsToModelState(this ISuccessOrErrors errorHolder, ModelStateDictionary modelState)
        {
            if (errorHolder.IsValid)
            {
                return;
            }

            foreach (var error in errorHolder.Errors)
            {
                modelState.AddModelError("", error.ErrorMessage);
            }
        }
        /// <summary>
        /// This returns an Errors with any errors in ISuccessOrErrors transferred
        /// It looks for errors that have member names corresponding to the properties in the displayDto.
        /// This means that errors assciated with a field on display will show next to the name.
        /// Other errors will be shown in the ValidationSummary
        /// Should only be called if there is an error
        /// </summary>
        /// <param name="errorHolder">The interface that holds the errors</param>
        /// <param name="displayDto">Dto that the error messages came from</param>
        /// <returns>It returns a JsonNetResult with one parameter called errors which contains key value pairs.
        /// The key is the name of the property which had the error, or is empty string if global error.
        /// The value is an array of error strings for that property key</returns>
        public static JsonResult ReturnErrorsAsJson <T>(this ISuccessOrErrors errorHolder, T displayDto)
        {
            if (errorHolder.IsValid)
            {
                throw new ArgumentException("You should only call ReturnErrorsAsJson when there are errors in the status", "errorHolder");
            }

            var modelState = new ModelStateDictionary();

            errorHolder.CopyErrorsToModelState(modelState, displayDto);
            return(modelState.ReturnModelErrorsAsJson());
        }
        private ISuccessOrErrors <TDto> validateDtoState <TDto>(ISuccessOrErrors <TDto> status, TDto dto, ActionFlags flags)
            where TDto : DtoBase <TContext, TEntity, TDto>, new()
        {
            var currentItem = dto.FindItemTrackedForUpdate(dbContext);

            if (!new TDto().AllowedActions.HasFlag(ActionFlags.Update) && currentItem != null)
            {
                return(status.AddSingleError("Dto is not allowed for this kind of action"));
            }
            if (!new TDto().AllowedActions.HasFlag(ActionFlags.Create) && currentItem == null)
            {
                return(status.AddSingleError("Dto is not allowed for this kind of action"));
            }
            if (!flags.HasFlag(ActionFlags.Update) && currentItem != null)
            {
                return(status.AddSingleError("Object already exists"));
            }
            if (!flags.HasFlag(ActionFlags.Create) && currentItem == null)
            {
                return(status.AddSingleError("Object doesn't exist"));
            }

            return(status);
        }
 private SuccessOrErrors(ISuccessOrErrors nonResultStatus)
     : base(nonResultStatus)
 {
 }
Beispiel #9
0
 internal static void ShouldBeValid <T>(this ISuccessOrErrors <T> status, bool isValid = true)
 {
     ShouldBeValid(status as ISuccessOrErrors, isValid);
 }
 internal static void ShouldBeValid <T>(this ISuccessOrErrors <T> status, bool isValid = true)
 {
     Assert.AreEqual(isValid, status.IsValid, string.Join("\n", status.Errors));
 }
Beispiel #11
0
        // not working as expected

        //[Fact]
        //public void DbContext_model_should_match_actual_database_simple_check()
        //{
        //    using (var ctx = new ApplicationContext())
        //    {
        //        var comparer = new CompareEfSql();
        //        var status = comparer.CompareEfWithDb(ctx);

        //        status.IsValid.Should().BeTrue(GetErrorMessage(status));
        //    }
        //}

        //[Fact]
        //public void DbContext_model_should_match_actual_database_detailed_check()
        //{
        //    using (var ctx = new ApplicationContext())
        //    {
        //        var comparer = new CompareSqlSql();
        //        var status = comparer.CompareEfGeneratedSqlToSql(ctx, "DbConnection");

        //        status.IsValid.Should().BeTrue(GetErrorMessage(status));
        //    }
        //}

        private static string GetErrorMessage(ISuccessOrErrors status)
        {
            string message = status.GetAllErrors() + string.Join("\n", status.Warnings);

            return(message);
        }