mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-24 10:40:23 +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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@
|
||||
"projectUrl": "",
|
||||
"licenseUrl": "",
|
||||
"dependencies": {
|
||||
"Microsoft.AspNet.Mvc": "6.0.0-rc1-*",
|
||||
"Microsoft.AspNet.Routing": "1.0.0-rc1-*"
|
||||
},
|
||||
"frameworks": {
|
||||
|
||||
Reference in New Issue
Block a user