Skip to content

Commit 730f5d2

Browse files
committed
failing test custom validator keeping ref to instance var
1 parent 9dcd94a commit 730f5d2

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

spec/grape/api/custom_validations_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,54 @@
11
require 'spec_helper'
22

33
describe 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

0 commit comments

Comments
 (0)