Skip to content

Commit 9369f5f

Browse files
committed
Add spec to confirm that requires validates correctly with deep nested params
1 parent 35b432c commit 9369f5f

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

spec/grape/validations_spec.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,62 @@ def define_optional_using
120120
end
121121
end
122122

123+
context 'requires with nested params' do
124+
before do
125+
subject.params do
126+
requires :first_level, type: Hash do
127+
optional :second_level, type: Array do
128+
requires :value, type: Integer
129+
optional :name, type: String
130+
optional :third_level, type: Array do
131+
requires :value, type: Integer
132+
optional :name, type: String
133+
optional :fourth_level, type: Array do
134+
requires :value, type: Integer
135+
optional :name, type: String
136+
end
137+
end
138+
end
139+
end
140+
end
141+
subject.put('/required') { 'required works' }
142+
end
143+
144+
let(:request_params) do
145+
{
146+
first_level: {
147+
second_level: [
148+
{ value: 1, name: 'Lisa' },
149+
{
150+
value: 2,
151+
name: 'James',
152+
third_level: [
153+
{ value: 'three', name: 'Sophie' },
154+
{
155+
value: 4,
156+
name: 'Jenny',
157+
fourth_level: [
158+
{ name: 'Samuel' }, { value: 6, name: 'Jane' }
159+
]
160+
}
161+
]
162+
}
163+
]
164+
}
165+
}
166+
end
167+
168+
it 'validates correctly in deep nested params' do
169+
put '/required', request_params.to_json, 'CONTENT_TYPE' => 'application/json'
170+
171+
expect(last_response.status).to eq(400)
172+
expect(last_response.body).to eq(
173+
'first_level[second_level][1][third_level][0][value] is invalid, ' \
174+
'first_level[second_level][1][third_level][1][fourth_level][0][value] is missing'
175+
)
176+
end
177+
end
178+
123179
context 'requires :all using Grape::Entity documentation' do
124180
def define_requires_all
125181
documentation = {

0 commit comments

Comments
 (0)