public static IEnumerable<Error> ConvertValidationResultToErrorList(ModelValidationResult result) { if (result == null) return null; return from error in result.Errors from memberName in error.MemberNames select new Error(false, error.GetMessage(memberName), memberName); }
public void Should_return_invalid_if_any_errors_exist() { // Given var subject = new ModelValidationResult(new[] { new ModelValidationError("blah", "blah") }); // When var result = subject.IsValid; // Then result.ShouldBeFalse(); }
public void Should_return_valid_when_no_errors_exist() { // Given var subject = new ModelValidationResult((IEnumerable<ModelValidationError>)null); // When var result = subject.IsValid; // Then result.ShouldBeTrue(); }
public static List<string> GetValidationErrors(ModelValidationResult result) { List<string> errors = new List<string>(); foreach(var e in result.Errors) { foreach(var message in e.Value) { errors.Add (message.ErrorMessage); } } return errors; }
public static ActionResult AsErrorOnValidation(ModelValidationResult validationResult) { var result = new ActionResult(false); foreach (var error in validationResult.Errors) { foreach (var member in error.MemberNames) { result.WithError(error.GetMessage(member)); } } return result; }
public static Response AsValidationError(this IResponseFormatter response, ModelValidationResult result) { var model = new { Errors = new List<string>() }; foreach (var error in result.Errors) { foreach (var member in error.MemberNames) { model.Errors.Add(error.GetMessage(member)); } } return response.AsJson(model) .WithStatusCode(400); }
public void AddValidationErrors(ModelValidationResult result) { Model.Errors.AddRange(Helpers.ErrorHelper.ConvertValidationResultToErrorList(result)); }