Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nested attribute names for validation error messages #1933

Open
amrrbakry opened this issue Nov 20, 2019 · 6 comments
Open

Support nested attribute names for validation error messages #1933

amrrbakry opened this issue Nov 20, 2019 · 6 comments

Comments

@amrrbakry
Copy link

amrrbakry commented Nov 20, 2019

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

@dblock
Copy link
Member

dblock commented Nov 20, 2019

If you put a project up that reproduces this I can try to help.

@amrrbakry
Copy link
Author

Hi @dblock

here's a project with steps to reproduce in the README:

https://github.com/amrrbakry/grape-nested-params-validation

👍

@dblock
Copy link
Member

dblock commented Nov 24, 2019

@amrrbakry I tried it. Looked like a bug at first, but is more like a non-feature. Looking at https://github.com/ruby-grape/grape/blob/master/lib/grape/exceptions/base.rb#L73 is getting called with registration[total_capacity], so the following en.yml yields what you expect:

      attributes:
        registration[total_capacity]: '...'

This is pretty annoying and we should be supporting the syntax that you were trying to use above with nested attributes, and hopefully preserving backwards compatibility.

Would you care to write some specs and maybe implement this?

@dblock dblock changed the title overriding nested attribute name for validation message does not work Support nested attribute names for validation error messages Nov 24, 2019
@amrrbakry
Copy link
Author

Thank you, @dblock. I'd be happy to help with this. I'll take a look and see what I can do 👍

@amrrbakry
Copy link
Author

amrrbakry commented Nov 26, 2019

Hey @dblock, I need some help/opinion for the ideal way to tackle this. it seems that ParamsScope class constructs the full name of the param as a string (e.g. "registration[total_capacity]") here:

def full_name(name, index: nil)
before passing it to the Validation class for translation.

do you suggest a change to the full_name method or is it better to try to extract the names of the nested attributes from the string before the translation?

@dblock
Copy link
Member

dblock commented Nov 26, 2019

Without digging how it's used I think full_name could return a structure (array?) and that's what we would be passing around until we need a string representation of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants