Skip to content

Commit

Permalink
Add array spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Braktar committed Mar 4, 2021
1 parent 279da2a commit 6cffb14
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions spec/grape/validations/validators/coerce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,44 @@ def self.parse(_val)
expect(JSON.parse(last_response.body)).to eq([1, 1, 1, 1])
end

context 'Array type and coerce_with should' do
before do
subject.params do
optional :arr, type: Array, coerce_with: (lambda do |val|
if val.nil?
[]
else
val
end
end)
end
subject.get '/' do
params[:arr].class.to_s
end
end

it 'coerce nil value to array' do
get '/', arr: nil

expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Array')
end

it 'not coerce missing field' do
get '/'

expect(last_response.status).to eq(200)
expect(last_response.body).to eq('NilClass')
end

it 'coerce array as array' do
get '/', arr: []

expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Array')
end
end

it 'uses parse where available' do
subject.params do
requires :ints, type: Array, coerce_with: JSON do
Expand Down

0 comments on commit 6cffb14

Please sign in to comment.