mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-23 01:58:29 +00:00
Define ValidationErrorResult in SpaServices; use it in MusicStore
This commit is contained in:
30
Microsoft.AspNet.SpaServices/ValidationErrorResult.cs
Normal file
30
Microsoft.AspNet.SpaServices/ValidationErrorResult.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNet.Mvc;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||
|
||||
namespace Microsoft.AspNet.SpaServices
|
||||
{
|
||||
public class ValidationErrorResult : ObjectResult {
|
||||
public const int DefaultStatusCode = 400;
|
||||
|
||||
public ValidationErrorResult(ModelStateDictionary modelState, int errorStatusCode = DefaultStatusCode)
|
||||
: base(CreateResultObject(modelState))
|
||||
{
|
||||
if (!modelState.IsValid) {
|
||||
this.StatusCode = errorStatusCode;
|
||||
}
|
||||
}
|
||||
|
||||
private static IDictionary<string, IEnumerable<string>> CreateResultObject(ModelStateDictionary modelState)
|
||||
{
|
||||
if (modelState.IsValid) {
|
||||
return null;
|
||||
} else {
|
||||
return modelState
|
||||
.Where(m => m.Value.Errors.Any())
|
||||
.ToDictionary(m => m.Key, m => m.Value.Errors.Select(me => me.ErrorMessage));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user