Skip to content

Commit 16b92ff

Browse files
committed
README.md and UPGRADE.md update for ruby-grape#543, ruby-grape#545
1 parent 5b51c90 commit 16b92ff

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

README.md

+17
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

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
"id": 1,
26+
"name": {
27+
"first_name": "John",
28+
"last_name" : "Doe"
29+
}
30+
```
31+
32+
it no longer does in 0.6.2. The params block should now read:
33+
34+
```ruby
35+
params do
36+
requires :id, type: Integer
37+
requires :name, type: Hash do
38+
requires :first_name
39+
requires :last_name
40+
end
41+
end
42+
```

0 commit comments

Comments
 (0)