@@ -10,25 +10,30 @@ def validate_param!(attr_name, params)
1010 fail Grape ::Exceptions ::Validation , params : [ @scope . full_name ( attr_name ) ] , message : "must be at the most #{ @option } characters long"
1111 end
1212 end
13+ class InBody < Grape ::Validations ::PresenceValidator
14+ def validate ( request )
15+ validate! ( request . env [ 'api.request.body' ] )
16+ end
17+ end
1318 end
1419 end
1520
16- subject do
17- Class . new ( Grape ::API ) do
18- params do
19- requires :text , default_length : 140
20- end
21- get do
22- 'bacon'
21+ context 'using a custom length validator' do
22+ subject do
23+ Class . new ( Grape ::API ) do
24+ params do
25+ requires :text , default_length : 140
26+ end
27+ get do
28+ 'bacon'
29+ end
2330 end
2431 end
25- end
2632
27- def app
28- subject
29- end
33+ def app
34+ subject
35+ end
3036
31- context 'using a custom length validator' do
3237 it 'under 140 characters' do
3338 get '/' , text : 'abc'
3439 expect ( last_response . status ) . to eq 200
@@ -45,4 +50,32 @@ def app
4550 expect ( last_response . body ) . to eq 'bacon'
4651 end
4752 end
53+
54+ context 'using a custom body-only validator' do
55+ subject do
56+ Class . new ( Grape ::API ) do
57+ params do
58+ requires :text , in_body : true
59+ end
60+ get do
61+ 'bacon'
62+ end
63+ end
64+ end
65+
66+ def app
67+ subject
68+ end
69+
70+ it 'allows field in body' do
71+ get '/' , text : 'abc'
72+ expect ( last_response . status ) . to eq 200
73+ expect ( last_response . body ) . to eq 'bacon'
74+ end
75+ it 'ignores field in query' do
76+ get '/' , nil , text : 'abc'
77+ expect ( last_response . status ) . to eq 400
78+ expect ( last_response . body ) . to eq 'text is missing'
79+ end
80+ end
4881end
0 commit comments