File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 376376Parameters can be nested using ` group ` or by calling ` requires ` or ` optional ` with a block.
377377In the above example, this means ` params[:media][:url] ` is required along with ` params[:id] ` ,
378378and ` params[:audio][:format] ` is required only if ` params[:audio] ` is present.
379+ With a block, ` group ` , ` requires ` and ` optional ` accept an additional option ` type ` which can
380+ be either ` Array ` or ` Hash ` , and defaults to ` Array ` . Depending on the value, the nested
381+ parameters will be treated either as values of a hash or as values of hashes in an array.
382+
383+ ``` ruby
384+ params do
385+ optional :preferences , type: Array do
386+ requires :key
387+ requires :value
388+ end
389+
390+ requires :name , type: Hash do
391+ requires :first_name
392+ requires :last_name
393+ end
394+ end
395+ ```
379396
380397### Namespace Validation and Coercion
381398
Original file line number Diff line number Diff line change 1+ # Upgrading
2+
3+ ## upgrading to 0.6.2
4+
5+ In grape <= 0.6.1, ` group ` , ` optional ` and ` requires ` with block accepted
6+ either an Array or a Hash.
7+
8+ In grape 0.6.2, these have an additional ` type ` attribute which defaults
9+ to ` Array ` . This means that without a ` type ` attribute, these nested parameters
10+ will no longer accept a single hash, only an array (of hashes).
11+
12+ ``` ruby
13+ params do
14+ requires :id , type: Integer
15+ group :name do
16+ requires :first_name
17+ requires :last_name
18+ end
19+ end
20+ ```
21+
22+ Whereas in 0.6.1 this accepted the following json,
23+
24+ ``` json
25+ {
26+ "id" : 1 ,
27+ "name" : {
28+ "first_name" : " John" ,
29+ "last_name" : " Doe"
30+ }
31+ }
32+ ```
33+
34+ it no longer does in 0.6.2. The params block should now read:
35+
36+ ``` ruby
37+ params do
38+ requires :id , type: Integer
39+ requires :name , type: Hash do
40+ requires :first_name
41+ requires :last_name
42+ end
43+ end
44+ ```
You can’t perform that action at this time.
0 commit comments