Esempio n. 1
0
        public ResponseModel <List <PropertyErrorModel> > GetErrors()
        {
            if (_modelState.IsValid)
            {
                new ResponseModel <List <PropertyErrorModel> > {
                    Success = false, Response = null
                }
            }
            ;

            var errors = _modelState
                         .Where(x => x.Value.Errors.Any())
                         .Select(x =>
            {
                return(new PropertyErrorModel
                {
                    PropertyName = x.Key,
                    AttemptedValue = x.Value.AttemptedValue,
                    ErrorMessages = x.Value.Errors.Select(y => y.ErrorMessage).ToList()
                });
            });

            return(new ResponseModel <List <PropertyErrorModel> > {
                Success = false, Response = errors.ToList()
            });
        }

        #endregion
    }
Esempio n. 2
0
 /// <summary>
 /// The method would iterate through the errors
 /// </summary>
 /// <param name="modelState">The model state</param>
 /// <param name="prefix">The prefix to be used</param>
 public static void ClearModelErrors(this Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary modelState, string prefix)
 {
     foreach (var modelKey in modelState.Where(m => m.Key.StartsWith(prefix)))
     {
         modelKey.Value.Errors.Clear();
     }
 }
        public static ValidationMessages ToValidationMessages(
            this Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary modelState, bool camelCaseKeyName = true)
        {
            var errors = modelState
                         .Where(x => x.Value.Errors.Any())
                         .ToDictionary(
                kvp => CamelCasePropNames(kvp.Key),
                kvp => kvp.Value.Errors.Select(e => e.ErrorMessage)
                );

            return(new ValidationMessages(errors));
        }
        /// <summary>
        /// Generates a response with a message for each error in the <see cref="ModelStateDictionary"/>
        /// </summary>
        /// <param name="state">The state containing all error messages</param>
        public IActionResult InvalidModelState(Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary state)
        {
            var model = GenerateModel(ErrorCode.ParseError,
                                      state.Where(kv => kv.Value.Errors.Count > 0)
                                      .SelectMany(kv => kv.Value.Errors)
                                      .Select(err => GenerateModel(ErrorCode.ParseError, err.ToString()))
                                      .ToArray()
                                      );

            return(new ObjectResult(model)
            {
                StatusCode = 400
            });
        }