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

Param alias #1673

Closed
glaucocustodio opened this issue Aug 2, 2017 · 7 comments
Closed

Param alias #1673

glaucocustodio opened this issue Aug 2, 2017 · 7 comments

Comments

@glaucocustodio
Copy link
Contributor

Hi folks, given the following api:

resource :users do
  params do
    requires :email_address, allow_blank: false
    requires :password, allow_blank: false
  end
  post do
    User.create!(email: params[:email_address], password: params[:password])
  end
end

Do you think the following api would be nice? (alias as:)

resource :users do
  params do
    requires :email, allow_blank: false, as: :email_address
    requires :password, allow_blank: false
  end
  post do
    User.create!(params)
  end
end
@dblock
Copy link
Member

dblock commented Aug 3, 2017

I think that would be valuable.

@glaucocustodio
Copy link
Contributor Author

glaucocustodio commented Aug 4, 2017

Do you think the first suggestion is better than the below one @dblock?

resource :users do
  params do
    requires :email_address, allow_blank: false, as: :email
    requires :password, allow_blank: false
  end
  post do
    User.create!(params)
  end
end

I just implemented this one requires :email_address, allow_blank: false, as: :email but I'm not sure which is better.

Ps: supposing users table has :email and :password attributes

@dblock
Copy link
Member

dblock commented Aug 4, 2017

I think that if the model takes :email and the API takes :email_address you're trying to rename the API's :email_address into :email, so I think as: means "rename what's required into the value of :as, aka the second suggestion. Otherwise it feels backwards.

@glaucocustodio
Copy link
Contributor Author

glaucocustodio commented Aug 5, 2017

Do you think requires :email_address, as: :email would lead to mistakes given that in Grape Entity we would do expose :email, as: email_address to expose the key as email_address?

@dblock
Copy link
Member

dblock commented Aug 5, 2017

I mean it's what renaming is about ;)

@tlabeeuw
Copy link

PS: this currently doesn't seem to work for nested params:

group :foo, alias: :bar do
  requires :a
end
requires :baz, alias: :cat do
  requires :b
end

These above both fail.

@dblock
Copy link
Member

dblock commented Mar 24, 2018

@tlabeeuw Can you please see if this is still broken on HEAD? Maybe PR a spec?

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

3 participants