Skip to content

Say a param is invalid when its validation error has no message - #2993

Merged
ericproulx merged 1 commit into
masterfrom
fix/validation-error-without-message
Sep 23, 2026
Merged

ericproulx merged 1 commit into
masterfrom
fix/validation-error-without-message

Conversation

@ericproulx

Copy link
Copy Markdown
Contributor

A custom validator with no default_message_key that raises through validation_error!(attr_name) renders the exception's class name as the error message:

class EvenNumberValidator < Grape::Validations::Validators::Base
  def validate_param!(attr_name, params)
    validation_error!(attr_name) unless params[attr_name].to_i.even?
  end
end

params { requires :number, even_number: true }
# GET /?number=3 => 400 "number Grape::Exceptions::Validation"

The same happens on 3.3.5 and 4.0.1. Exceptions::Validation#initialize only translates a message when one is given. Without one, super(message: nil) leaves StandardError#message to fall back to the class name.

Such an error now falls back to a new invalid locale message (is invalid), with :invalid as its message_key. The response above becomes number is invalid.

This changes what a message-less Exceptions::Validation reports: message_key is :invalid instead of nil, and message is is invalid instead of the class name. The existing spec pinning the old values is updated, and UPGRADING describes the change.

Found while covering Validators::Base#message's fallback with no key, no :message option and no block. That branch returns nil, and the new custom-validator spec reaches it.

🤖 Generated with Claude Code

@ericproulx
ericproulx force-pushed the fix/validation-error-without-message branch from 8cafec0 to 8ce6fdc Compare September 23, 2026 06:58
@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Danger Report

No issues found.

View run

A `Grape::Exceptions::Validation` created without a message only
translated one when given one, so Ruby's `StandardError#message` fell
back to the exception's class name, and that reached the response. A
custom validator with no `default_message_key` that calls
`validation_error!(attr_name)` answered
`number Grape::Exceptions::Validation`.

Such an error now takes a new `invalid` message ("is invalid"), with
`:invalid` as its message key.

Also covers `Validators::Base#message` called with no key, no
`:message` option and no block, which a custom validator can do and no
spec did.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ericproulx
ericproulx force-pushed the fix/validation-error-without-message branch from 8ce6fdc to 2e8144d Compare September 23, 2026 15:06
@ericproulx
ericproulx merged commit 986e39c into master Sep 23, 2026
33 checks passed
@ericproulx
ericproulx deleted the fix/validation-error-without-message branch September 23, 2026 19:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants