You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 8049469
Browse filesBrowse the repository at this point in the historyBrowse files
Report a required param the request left out as missing only
Every validator of a `requires` param was called whether the request
sent the param or not, because PresenceValidator has to be called for
a missing one to report it. A custom validator that read the value was
then handed nil and raised, so the request answered 500 instead of
400. The README's own AlphaNumeric example does that:
requires :text, alpha_numeric: true
GET / Before: 500 NoMethodError (undefined method 'match?' for nil)
After: 400 text is missing
The built-in validators added a second error to `is missing`
instead: `n is missing, n does not have a valid value` from `values`,
`email is missing, email is empty` from `allow_blank: false`.
Only PresenceValidator is now called for an attribute the params do
not hold. Every other validator sees only the params a request sent,
as it already did for an `optional` param, so a missing one is
reported once. Each validator settles this when it is built, so the
attribute loop reads an ivar where it called `required?`. A param
sent as `null` is still validated, and `values` still rejects it.
UPGRADING notes the dropped second messages, including the one #3010
documented for a nested param with `values`. The README says when
`validate_param!` is called.
Fixes#1693.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -54,6 +54,7 @@
54
54
*[#3083](https://github.com/ruby-grape/grape/pull/3083): Apply a mounting API's `default_error_status` to routes defined before the mount - [@ericproulx](https://github.com/ericproulx).
55
55
*[#3087](https://github.com/ruby-grape/grape/pull/3087): Let a mounted API `use` the named params of the scope it is mounted in - [@ericproulx](https://github.com/ericproulx).
56
56
*[#3093](https://github.com/ruby-grape/grape/pull/3093): Move the endpoint Rack stack building into `Endpoint::RackStack` to fix RuboCop - [@ericproulx](https://github.com/ericproulx).
57
+
*[#3100](https://github.com/ruby-grape/grape/pull/3100): Report a required param the request left out as missing only - [@ericproulx](https://github.com/ericproulx).
Copy file name to clipboardExpand all lines: README.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1863,6 +1863,8 @@ params do
1863
1863
end
1864
1864
```
1865
1865
1866
+
`validate_param!` is only called for a param the request sent. A `requires` param the request left out is reported as `text is missing`, and none of its other validators are called. A param sent as `null` reaches `validate_param!` as `nil`.
1867
+
1866
1868
You can also create custom classes that take parameters.
Copy file name to clipboardExpand all lines: UPGRADING.md
+22Lines changed: 22 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,28 @@ Upgrading Grape
3
3
4
4
### Upgrading to >= 4.2.0
5
5
6
+
#### A required param the request left out is only reported missing
7
+
8
+
A `requires` param the request left out now answers `is missing` and nothing else ([#3100](https://github.com/ruby-grape/grape/pull/3100)). Its other validators, custom ones included, are only called for a param the request sent, as they already were for an `optional` one. A custom validator that read the value used to be handed `nil` for it and raise, so the request answered `500` instead of `400`. The README's own `AlphaNumeric` example did that.
9
+
10
+
The built-in validators that added a second error for a missing param no longer do:
11
+
12
+
```ruby
13
+
params do
14
+
requires :n, type:Integer, values: [1, 2]
15
+
requires :email, type:String, allow_blank:false
16
+
end
17
+
```
18
+
19
+
```
20
+
# Before
21
+
n is missing, n does not have a valid value, email is missing, email is empty
22
+
# After
23
+
n is missing, email is missing
24
+
```
25
+
26
+
This also drops the `does not have a valid value` that [#3010](https://github.com/ruby-grape/grape/pull/3010) added after `is missing` for a nested param with `values` (see [A required param with `values` rejects `nil` in a nested scope](#a-required-param-with-values-rejects-nil-in-a-nested-scope)). A param sent as `null` is still validated, and `values` still rejects it. A custom validator that has to see a missing param, to report it in its own words for instance, can override `validate(request)`, which is called whether the param was sent or not.
27
+
6
28
#### `Grape::Util::MediaType` parses for a vendor's pattern
7
29
8
30
`Grape::Util::MediaType.parse`, `.best_quality` and `.new` take `vendor_pattern:`, the pattern `Grape::Util::MediaType.vendor_pattern(vendor)` answers, in place of `vendor:` ([#3098](https://github.com/ruby-grape/grape/pull/3098)). The header versioner looks its vendor's pattern up as it is built, so a request no longer reads a cache. Only code calling these internals is affected: pass `vendor_pattern: Grape::Util::MediaType.vendor_pattern(vendor)`.
0 commit comments