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