grape icon indicating copy to clipboard operation
grape copied to clipboard

Suppress one of the validation errors from `allow_blank` on a required parameter

Open andrykonchin opened this issue 4 years ago • 1 comments

Using allow_blank validator for the required parameter may lead to two validation errors, but I expected to have only one.

Example:

requires :customer_key, type: String, allow_blank: false

If there is no customer_key parameter in request then we receive the following Grape::Exceptions::ValidationErrors exception:

error
=> #<Grape::Exceptions::ValidationErrors: Grape::Exceptions::ValidationErrors>

error.errors
=> {["customer_key"]=>[#<Grape::Exceptions::Validation: is missing>, #<Grape::Exceptions::Validation: is empty>]}

So we receive

  • one error because the required parameter "is missing" and
  • another one caused by allow_blank validator because the parameter "is empty"

The second error (produced by the allow_blank) doesn't make sense and looks a bit annoying. The expected behavior is to receive only "is missing" error.

andrykonchin avatar Aug 20 '21 10:08 andrykonchin

Agreed, but be careful about breaking backwards compatibility if you try to fix. It might make sense to keep things "dumb" like this where validators don't know much about each-other to avoid too clever of a behavior.

From the machine POV a client can easily suppress one of the two when these come back from a REST API.

dblock avatar Aug 20 '21 12:08 dblock