Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions spec/grape/validations/params_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,43 @@ def app
end
end

context 'when using multiple types' do
it 'coerces the parameter via the type\'s parse method' do
subject.params do
requires :bar, type: Array do
optional :foo, types: [String, Float, Integer], coerce_with: ->(c) {
if c
if /([0-9]+):([0-9]+):([0-9]+)/ =~ c.to_s
'HH:MM:SS format'
elsif /\A[0-9]+\.{0,1}[0-9]*\z/ =~ c.to_s
'Float or Integer format'
else
'Invalid Time value'
end
end
}
end
end
subject.post('/types') { declared(params)[:bar].first[:foo] }

post '/types', bar: [{ foo: '00:00:01' }]
expect(last_response.status).to eq(201)
expect(last_response.body).to eq('HH:MM:SS format')

post '/types', bar: [{ foo: 1 }]
expect(last_response.status).to eq(201)
expect(last_response.body).to eq('Float or Integer format')

post '/types', bar: [{ foo: 1.0 }]
expect(last_response.status).to eq(201)
expect(last_response.body).to eq('Float or Integer format')

post '/types', bar: [{ foo: nil }]
expect(last_response.status).to eq(201)
expect(last_response.body).to eq('')
end
end

context 'when using custom types' do
module ParamsScopeSpec
class CustomType
Expand Down