Skip to content

How to support JSON API Error Objects? #73

@Bouke

Description

@Bouke

JSON API specifies a format for so-called "error objects". While I could create such error documents myself, I would be interested to see what would be needed to support creating such documents based on ModelState validation.

An example from the JSON API site:

HTTP/1.1 422 Unprocessable Entity
Content-Type: application/vnd.api+json

{
  "jsonapi": { "version": "1.0" },
  "errors": [
    {
      "code":   "123",
      "source": { "pointer": "/data/attributes/firstName" },
      "title":  "Value is too short",
      "detail": "First name must contain at least three characters."
    },
    {
      "code":   "225",
      "source": { "pointer": "/data/attributes/password" },
      "title": "Passwords must contain a letter, number, and punctuation character.",
      "detail": "The password provided is missing a punctuation character."
    },
    {
      "code":   "226",
      "source": { "pointer": "/data/attributes/password" },
      "title": "Password and password confirmation do not match."
    }
  ]
}

For reference, the default output of ModelState looks like this:

if (!ModelState.IsValid)
    throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
{
    "Message": "The request is invalid.",
    "ModelState": {
        "category.Name": [
            "The field Name must be a string or array type with a minimum length of '20'."
        ]
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions