ruby-grape / ruby-grape/grape

Support nested attribute names for validation error messages

Open
#1,933 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Hello,

I'm trying to override a nested attribute's name that gets show in the validation message, but it doesn't seem possible for nested params

I want to override registration[total_capacity] attribute name:

my params:

params :sessions_params do
  optional :registration_allowed, type: Boolean, desc: 'registration allowed for session?'
  optional :registration, type: Hash, desc: 'session registration settings' do
    requires :total_capacity, type: Integer, desc: 'session registration total capacity', message: :name_required
    requires :waiting_list_enabled, type: Boolean, desc: 'is session registration waiting list enabled?'
  end
end

en.yml

en:
  grape:
    errors:
      format: ! '%{attributes} %{message}'
      messages:
        name_required: 'must be present'
      attributes:
        registration:
          total_capacity: 'Total capacity'

expected error message if total capacity is not sent:

"Total capacity must be present"

actual error message:

"registration[total_capacity] must be present"

is this not possible or is there a problem with my code?

using grape 0.16.2, ruby 2.3.3, rails 4.2

thanks

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 nested params definition and locale configuration shown in the issue, then trace how the validation error formats the attribute name and looks up nested translations. Done means a missing registration[total_capacity] value produces “Total capacity must be present” while preserving the existing message behavior for other attributes.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.