Esempio n. 1
0
 /// <summary>
 /// Vrací ValidationErrorModel pro ModelState.
 /// </summary>
 public static ValidateModelAttribute.ResultSelectorDelegate FromModelState()
 {
     return((int statusCode, ModelStateDictionary modelState) => new ValidationErrorModel(statusCode)
     {
         Errors = FieldValidationError.FromModelState(modelState)
     });
 }
Esempio n. 2
0
        public static void UseInvalidModelStateToError(this ApiBehaviorOptions options)
        {
            options.InvalidModelStateResponseFactory = context =>
            {
                var errorsWithMessage = context.ModelState
                                        .Where(x => x.Value.ValidationState == ModelValidationState.Invalid).ToDictionary(
                    x => string.Join('.', x.Key.Split('.').Select(StringExtensions.ToCamelCase)),
                    x => x.Value.Errors.First().ErrorMessage);

                var fieldValidationError = new FieldValidationError(errorsWithMessage);
                return(new BadRequestObjectResult(fieldValidationError));
            };
        }