Skip to content

Commit

Permalink
Add nest array coercion spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Braktar committed Aug 25, 2020
1 parent 192a2a2 commit 15019af
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions spec/grape/validations/validators/coerce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,23 @@ def self.parsed?(value)
expect(JSON.parse(last_response.body)).to eq(%w[a b c d])
end

it 'parses parameters with Array[Array[String]] type and coerce_with' do
subject.params do
requires :values, type: Array[Array[String]], coerce_with: ->(val) { val.is_a?(String) ? [val.split(/\s+/).strip] : val }
end
subject.get '/coerce_nested_strings' do
params[:values]
end

get '/coerce_nested_strings', values: 'a,b,c,d'
expect(last_response.status).to eq(200)
expect(JSON.parse(last_response.body)).to eq([%w[a b c d]])

get '/coerce_nested_strings', values: [%w[a], %w[b]]
expect(last_response.status).to eq(200)
expect(JSON.parse(last_response.body)).to eq([%w[a], %w[b]])
end

it 'parses parameters with Array[Integer] type' do
subject.params do
requires :values, type: Array[Integer], coerce_with: ->(val) { val.split(/\s+/).map(&:to_i) }
Expand Down

0 comments on commit 15019af

Please sign in to comment.