ruby-grape / ruby-grape/grape

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

Open
#2,187 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request
Dominant language
Ruby
Stars
10k
Forks
1.2k
Avg merge
14h 38m
Merged PRs (30d)
92

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example request using requires :customer_key, type: String, allow_blank: false, then trace the validation handling for a missing parameter. Done means the resulting Grape::Exceptions::ValidationErrors contains only the “is missing” validation and no duplicate “is empty” error.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.