File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change 3
3
require 'spec_helper'
4
4
5
5
describe Grape ::Validations do
6
+
7
+ context "when a validator uses an instance variable" do
8
+ before do
9
+ module CustomValidationsSpec
10
+ module HelperMethods
11
+ extend Grape ::API ::Helpers
12
+ def max_ref ( req_params )
13
+ return @max unless @max . nil?
14
+ @max = req_params [ :max ] . to_i
15
+ end
16
+ end
17
+
18
+ class DefaultLength < Grape ::Validations ::Base
19
+ include CustomValidationsSpec ::HelperMethods
20
+ def validate_param! ( attr_name , params )
21
+ return if params [ attr_name ] . length <= max_ref ( params )
22
+ fail Grape ::Exceptions ::Validation , params : [ @scope . full_name ( attr_name ) ] , message : "must be at the most #{ max_ref ( params ) } characters long"
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ subject do
29
+ Class . new ( Grape ::API ) do
30
+ params do
31
+ requires :text , default_length : 140
32
+ requires :max
33
+ end
34
+ get do
35
+ 'bacon'
36
+ end
37
+ end
38
+ end
39
+
40
+ def app
41
+ subject
42
+ end
43
+
44
+ it 'between 2 calls, helper inside a validator does not keep old reference of instance variable' do
45
+ get '/' , text : 'a' * 130 , max : 140
46
+ expect ( last_response . status ) . to eq 200
47
+
48
+ get '/' , text : 'a' * 130 , max : 120
49
+ expect ( last_response . status ) . to eq 400
50
+ end
51
+ end
52
+
53
+
6
54
context 'using a custom length validator' do
7
55
before do
8
56
module CustomValidationsSpec
You can’t perform that action at this time.
0 commit comments