8000 How to convert ModelState errors to valid JSON API response · Issue #129 · joukevandermaas/saule · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
How to convert ModelState errors to valid JSON API response #129
Open
@rhyek

Description

@rhyek

My API does some validation on POST/PUT requests on some non-JSONAPI endpoints and may generate an error response using ModelState. When trying to convert these endpoints to JSONAPI, I notice Saule isn't currently trying to use the information available in the ModelState response to provide useful error messages back to the client.

I have a request filter that does the following:

    public class InvalidModelStateFilterAttribute : ActionFilterAttribute {
        public override void OnActionExecuting(HttpActionContext actionContext) {
            if (!actionContext.ModelState.IsValid) {
                actionContext.Response = actionContext.Request.CreateErrorResponse(
                    HttpStatusCode.BadRequest, actionContext.ModelState);
            } else {
                base.OnActionExecuting(actionContext);
            }
        }
    }

Currently, that gets serialized to something like this:

{"Message":"The request is invalid.","ModelState":{"json.Name":["The category already exists."]}}

With Saule, I'm now getting something like this:

{"errors":[{"title":"The request is invalid."}]}

What would be necessary to change to have this errors array contain the actual fields and error descriptions?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0