question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve 422 validation error responses

See original GitHub issue

Our current validation error format (not sure if this is consistent across the app?) looks like this:

{
    "errors": [
        {
            "message": "Value in [posts.title] exceeds maximum length of 150 characters.",
            "errorType": "ValidationError"
        },
        {
            "message": "Value in [posts.meta_title] exceeds maximum length of 150 characters.",
            "errorType": "ValidationError"
        }
    ]
}

This has a couple of issues when attempting to consume on the client side:

  • It’s necessary to extract the invalid property name from the message string
  • The message is not suitable for display within in-line validations

Due to the above issues we don’t currently have anything in our Ember Data adapters to take the error response and normalize it so that the errors are automatically picked up by Ember Data/DS.Errors and made available on the relevant model object.

There are two formats that are widely used with Ember Data and have built-in support:

ActiveModelSerializer format:

{
    "errors": {
        "title": ["Title cannot be longer than 150 characters."],
        "message": ["Meta Title cannot be longer than 150 characters."]
    }
}

JSON API format:

{
    "errors": [
        {
            "source": { "pointer": "/data/attributes/title" },
            "title":  "Value is too long",
            "detail": "Title cannot be longer than 150 characters."
        },
        {
            "source": { "pointer": "/data/attributes/meta_title" },
            "title": "Value is too long",
            "detail": "Meta Title cannot be longer than 150 characters."
        }
    ]
}

The JSON API format also allows for additional fields such as code for application-specific error codes or the meta field that can contain completely custom data.

i18n: If we are going to update our validation response format would it also be a good time to make accommodations for i18n support? To do so it would be best to provide a “key” for the localisation lookup, e.g. a generic validation.too_long or a more specific post.errors.title.too_long.

Perhaps the meta field would be best for that? That would allow us to both provide a localisation key and any other details that can be used for interpolation. For example:

"meta": {
    "i18n": {
        "key": "validation.too_long",
        "max_chars": 150
    }
}

Could map nicely to the translation key:

"validation.too_long": "cannot be longer than {{max_chars}} characters"


I’d like to get some discussion started on which direction we’d like move towards for two main reasons:

  1. Even though we’ve mostly eliminated hitting the server with invalid data from the admin client there are still some edge cases where automatic mapping would be useful.
  2. More importantly when we get to opening write-access through the public API and its out in the wild and in use it will be a lot more difficult to change.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
kevinansfieldcommented, Jan 2, 2018

In any case, this can be closed with the later tag ready for when it becomes a more pressing problem.

0reactions
kevinansfieldcommented, Jan 2, 2018

but even with pointers/codes the admin client would need to differentiate the validation errors

That’s already built in to Ember Data, the errors would appear on the model.errors array for the right field automatically. At the moment we detect server-side validation errors manually and push errors on to the model in some cases or the error isn’t picked up and ends up in a generic error alert.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Fix the HTTP 422 Error - Kinsta
Error 422 is an HTTP code that tells you that the server can't process your request, although it understands it. The full name...
Read more >
Use 422 for validation errors instead of 400 #1819 - GitHub
It is more common to reply with a 400 than 422 for validation errors. You are not really adding much value because those...
Read more >
400 vs 422 response to POST of data - Stack Overflow
A 422 status code occurs when a request is well-formed, however, due to semantic errors it is unable to be processed. This HTTP...
Read more >
422 Unprocessable Entity - HTTP - MDN Web Docs
The HyperText Transfer Protocol (HTTP) 422 Unprocessable Entity response status code indicates that the server understands the content type ...
Read more >
Which HTTP code is best suited for validation errors: 400 or ...
Use 400 if the query is syntactically incorrect. Use 422 if the query is semantically incorrect. I'll show you a clean example :...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found