Hi there!
I am here for some best practice support to understand better validation. I am currently working on a headless d8 and I want to setup server side validation.
I am having hard time figuring out best practice on how to handle the error coming back from a request. Some example:
I have a node, the title is required and I added a constraint:
$fields['title']->addPropertyConstraints('value', ['Length' => ['min' => 4, 'max' => 10]]);
When I POST when empty title, I get a 422 with detail:
title: This value should not be null.
When I POST when title length < 4, I get a 422 with detail:
title.0.value: This value is too short. It should have 4 characters or more.
How would you handle these errors? I would like to map back the error to the field in my UI (to display the error message under the field) but it comes back once with the key "title", the other time with the key "title.0.value". For now, the only idea I have is to split that key to isolate "title" and map it back to the field in the UI.
Also, as the error detail info is a string, to I would need to split that string to get the field name.
I don't feel like I am on the right path here and that I am missing something.
Thanks for your help!
Ed
Comments
Comment #2
wim leersThis is an excellent question. I don't have time right now to dive into details. But I can tell you 3 things:
restmodule, and in fact, thejsonapimodule pretty much does the same thing. #1916302: RFC 7807: "Problem Details for HTTP APIs" — serve REST error responses as application/problem+json exists to improve that.Looking forward to what the other JSON API maintainers have to say about this one :)
Comment #3
wim leersThanks for taking the time to describe the problem so clearly by the way! That alone is a valuable contribution! 👍❤️
Comment #4
wim leersI realize I forgot to answer your concrete question:
Correct, you would indeed have to parse that string. That's today's reality. Hopefully we can improve that in the future!
Comment #5
heyddi commentedThanks for this quick answer! I kind of feel better, I thought I was missing something very obvious :)
Also very interested to see what the other maintainers have to say about this!
Edit: removing some useless comments after reading all the related issue.
Comment #6
wim leersRight, I totally understand that — that's how I felt too the first time I saw it:
That's still true to this day. Remember, Drupal is FLOSS, so some things just don't get done unless somebody who cares about them finds the time to get it done. The good thing is that this is an area where you can have a lot of impact: you can help shape how this will/should work. With code/patches, but also with discussion/research to help guide us to the best possible approach :)
I'm not saying you have to do that, but your input would definitely be deeply valued!
Comment #7
heyddi commentedI would love to contribute and I will see if I find some time to figure out something that may be a starting point for this.
Comment #8
wim leers