Array[Array[X]]] and Array[Hash[X]]] return wrong indices in errors #1998
Comments
Maybe PR the failing spec? Make sure it's still failing against HEAD. |
I think a have the same wrong behaviour with grape 1.3.0. My controller with validations: class OrderController < Grape::API
rescue_from Grape::Exceptions::ValidationErrors do |e|
error!({ error: e.full_messages }, 422)
end
resources :orders do
params do
requires :orders, type: Array do
requires :address, type: Hash do
requires :name, type: String, allow_blank: false, desc: 'Name row 1'
optional :name2, type: String, desc: 'Name row 2'
requires :city, type: String, allow_blank: false
end
requires :positions, type: Array do
requires :quantity, type: Integer, allow_blank: false
requires :article, type: String, allow_blank: false
end
end
end
post do
end
end
end A request with an array of orders as hashes. First order address's city is empty and in the first order the order quantity type in the first position invalid. {
"orders": [
{
"address": {"name": "John Doe", "city": ""},
"positions": [
{"article": "Article 1", "quantity": [3]},
{"article": "Article 2", "quantity": 1}
]
},
{
"address": {"name": "Marie Smith", "city": "Miami"},
"positions": [
{"article": "Article 1", "quantity": 1},
{"article": "Article 2", "quantity": 1}
]
}
]
} The Grape validation errors are: {
"error": [
"orders[1][address][city] is empty",
"orders[0][positions][0][quantity] is invalid"
]
} The result for the validation error has the correct indeces for the error in the position, but for the address the wrong one, it is always the last index. Even if there are many orders with errors in their addresses, only the last index is shown. |
Thanks for the test @infiniteluke. Don't hold back if you find that time ;) |
Hey @infiniteluke ! Did you find a workaround for this problem? I see this issue is still open and your spec is still in draft. I am experiencing the same difficulties regarding indices not being correct when they get raised as validation errors, and I was wondering if you managed to find a temporary solution. Thanks for writing the spec btw! |
Unfortunately, I haven't been able to get to this and likely will not have a fix anytime soon as it's not longer a priority for me |
Basically the indices of nested arguments are not kept correctly during validation process. It happens both for Hashes and Arrays declared inside another Array.
In the case of
Array[Array[X]]
the inner index keeps increasing (in the error messages) suggesting there might be something wrong in the recursion logic of validators -- i.e., it acts as a global count.Created the following spec to explain the issue (disclaimer: first spec file, not sure if I got it right).
The text was updated successfully, but these errors were encountered: