Skip to content

Commit 30c531d

Browse files
committed
wip: new 422 validation errors with source.pointer
1 parent efa4aa7 commit 30c531d

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace JsonApiDotNetCore.Controllers
1010
{
11-
public class BaseJsonApiController<T>
11+
public class BaseJsonApiController<T>
1212
: BaseJsonApiController<T, int>
1313
where T : class, IIdentifiable<int>
1414
{
@@ -47,7 +47,7 @@ public class BaseJsonApiController<T, TId>
4747
private readonly ICreateService<T, TId> _create;
4848
private readonly IUpdateService<T, TId> _update;
4949
private readonly IUpdateRelationshipService<T, TId> _updateRelationships;
50-
private readonly IDeleteService<T, TId> _delete;
50+
private readonly IDeleteService<T, TId> _delete;
5151
private readonly IJsonApiContext _jsonApiContext;
5252

5353
public BaseJsonApiController(
@@ -156,7 +156,7 @@ public virtual async Task<IActionResult> PostAsync([FromBody] T entity)
156156
return Forbidden();
157157

158158
if (_jsonApiContext.Options.ValidateModelState && !ModelState.IsValid)
159-
return BadRequest(ModelState.ConvertToErrorCollection());
159+
return BadRequest(ModelState.ConvertToErrorCollection(_jsonApiContext.ContextGraph));
160160

161161
entity = await _create.CreateAsync(entity);
162162

@@ -170,7 +170,7 @@ public virtual async Task<IActionResult> PatchAsync(TId id, [FromBody] T entity)
170170
if (entity == null)
171171
return UnprocessableEntity();
172172
if (_jsonApiContext.Options.ValidateModelState && !ModelState.IsValid)
173-
return BadRequest(ModelState.ConvertToErrorCollection());
173+
return BadRequest(ModelState.ConvertToErrorCollection(_jsonApiContext.ContextGraph));
174174

175175
var updatedEntity = await _update.UpdateAsync(id, entity);
176176

src/JsonApiDotNetCore/Extensions/ModelStateExtensions.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace JsonApiDotNetCore.Extensions
66
{
77
public static class ModelStateExtensions
88
{
9-
public static ErrorCollection ConvertToErrorCollection(this ModelStateDictionary modelState)
9+
public static ErrorCollection ConvertToErrorCollection(this ModelStateDictionary modelState, IContextGraph contextGraph)
1010
{
1111
ErrorCollection collection = new ErrorCollection();
1212
foreach (var entry in modelState)
@@ -16,10 +16,19 @@ public static ErrorCollection ConvertToErrorCollection(this ModelStateDictionary
1616

1717
foreach (var modelError in entry.Value.Errors)
1818
{
19+
var attrName = entry.Key;
20+
1921
if (modelError.Exception is JsonApiException jex)
2022
collection.Errors.AddRange(jex.GetError().Errors);
2123
else
22-
collection.Errors.Add(new Error(400, entry.Key, modelError.ErrorMessage, modelError.Exception != null ? ErrorMeta.FromException(modelError.Exception) : null));
24+
collection.Errors.Add(new Error(
25+
status: 422,
26+
title: entry.Key,
27+
detail: modelError.ErrorMessage,
28+
meta: modelError.Exception != null ? ErrorMeta.FromException(modelError.Exception) : null,
29+
source: new {
30+
pointer = $"/data/attributes/{attrName}"
31+
}));
2332
}
2433
}
2534

0 commit comments

Comments
 (0)