Skip to content

Conversation

@oivoodoo
Copy link
Contributor

Hello.

Probably it's not a good way to specify default param using validators. But I haven't found another way for now.

What do you think about it?

optional :type1, default: 'default-value1'

Test Examples:

get '/' { params[:type1] } get '/' last_response.body.should == 'default-value1' get '/?type1=passed-value' last_response.body.should == 'passed-value'
@dblock
Copy link
Member

dblock commented Apr 24, 2013

Semantically, it seems a little strange that a parameter is optional, but has a default value. Let me meditate on this for a bit. Appreciate any further thoughts on this.

Please do update CHANGELOG as well, thank you.

@oivoodoo
Copy link
Contributor Author

@dblock I've just updated changelog. Thanks for reviewing. For now I am using this approach for defining default values for the api.

Example:

params do optional :connection_type, type: String, default: 'all', desc: 'wifi, carrier or all' end get '/' do devices = Device.where(connection_type: params[:connection_type]) # if developer doesn't pass this value I am taking devices with all connection type. end
@dblock
Copy link
Member

dblock commented Apr 29, 2013

Merging, thank you.

@dblock dblock merged commit bd26c19 into ruby-grape:master Apr 29, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants