Skip to content

Commit 53660aa

Browse files
committed
Merge pull request #547 from bwalex/nested_vals_readmes
README.md and UPGRADE.md update for #543, #545
2 parents 5b51c90 + 50f0c02 commit 53660aa

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,23 @@ end
376376
Parameters can be nested using `group` or by calling `requires` or `optional` with a block.
377377
In the above example, this means `params[:media][:url]` is required along with `params[:id]`,
378378
and `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

UPGRADE.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
```

0 commit comments

Comments
 (0)