-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow validation of nested parameters through block
- Loading branch information
Cody Wright
committed
Nov 3, 2015
1 parent
b856928
commit bb9db2d
Showing
4 changed files
with
268 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
require 'spec_helper' | ||
|
||
describe 'Nested Validation' do | ||
context '' do | ||
it 'should validate the children when the parent is present' do | ||
params = { | ||
:parent => { | ||
:required_child => 1, | ||
} | ||
} | ||
|
||
get("/validation/hash/nested_values", params) do |response| | ||
expect(response.status).to eq(200) | ||
expect(JSON.parse(response.body)['message']).to eq("OK") | ||
end | ||
end | ||
|
||
it 'should be invalid when the parent is present but a nested validation fails' do | ||
params = { | ||
:parent => { | ||
:optional_chlid => 'test' | ||
} | ||
} | ||
|
||
get("/validation/hash/nested_values", params) do |response| | ||
expect(response.status).to eq(400) | ||
body = JSON.parse(response.body) | ||
expect(body['message']).to eq("Parameter is required") | ||
expect(body['errors']).to eq({ | ||
"parent[required_child]" => "Parameter is required" | ||
}) | ||
end | ||
end | ||
|
||
it 'should not require sub params when the parent hash is not present and not required' do | ||
params = {} | ||
get("/validation/hash/nested_values", params) do |response| | ||
expect(response.status).to eq(200) | ||
expect(JSON.parse(response.body)['message']).to eq("OK") | ||
end | ||
end | ||
|
||
it 'should allow arbitrary levels of nesting' do | ||
params = { | ||
:parent => { | ||
:required_child => 1, | ||
:nested_child => { | ||
:required_sub_child => 'test' | ||
} | ||
} | ||
} | ||
|
||
get("/validation/hash/nested_values", params) do |response| | ||
expect(response.status).to eq(200) | ||
expect(JSON.parse(response.body)['message']).to eq("OK") | ||
end | ||
end | ||
|
||
it 'should have the proper error message for multiple levels deep validation errors' do | ||
params = { | ||
:parent => { | ||
:required_child => 1, | ||
:nested_child => { | ||
:required_sub_child => 'test', | ||
:optional_sub_child => 'test' | ||
} | ||
} | ||
} | ||
|
||
get("/validation/hash/nested_values", params) do |response| | ||
expect(response.status).to eq(400) | ||
body = JSON.parse(response.body) | ||
expect(body['message']).to eq("'test' is not a valid Integer") | ||
expect(body['errors']).to eq({ | ||
"parent[nested_child][optional_sub_child]" => "'test' is not a valid Integer" | ||
}) | ||
end | ||
end | ||
|
||
it 'should error when sub hash validation is tried on a non Hash parameter' do | ||
params = { | ||
:parent => { | ||
:child => 'test' | ||
} | ||
} | ||
|
||
get("/validation/hash/bad_nested_values", params) do |response| | ||
expect(response.status).to eq(400) | ||
body = JSON.parse(response.body) | ||
expect(body['message']).to eq("Only the Hash parameter validation can use sub hash validation method") | ||
expect(body['errors']).to eq({ | ||
"parent" => "Only the Hash parameter validation can use sub hash validation method" | ||
}) | ||
end | ||
end | ||
|
||
it 'should work with one_of nested in a hash' do | ||
params = { | ||
:parent => { | ||
:a => 'test' | ||
} | ||
} | ||
|
||
get("/one_of/nested", params) do |response| | ||
expect(response.status).to eq(200) | ||
expect(JSON.parse(response.body)['message']).to eq("OK") | ||
end | ||
end | ||
|
||
it "should error when one_of isn't satisfied in a nested hash" do | ||
params = { | ||
:parent => { | ||
:a => 'test', | ||
:b => 'test' | ||
} | ||
} | ||
|
||
get("/one_of/nested", params) do |response| | ||
expect(response.status).to eq(400) | ||
expect(JSON.parse(response.body)['message']).to eq("Invalid parameters parent[[a, b, c]]") | ||
end | ||
end | ||
|
||
it 'should work with any_of nested in a hash' do | ||
params = { | ||
:parent => { | ||
:a => 'test' | ||
} | ||
} | ||
|
||
get("/any_of/nested", params) do |response| | ||
expect(response.status).to eq(200) | ||
expect(JSON.parse(response.body)['message']).to eq("OK") | ||
end | ||
end | ||
|
||
it "should error when one_of isn't satisfied in a nested hash" do | ||
params = { | ||
:parent => { | ||
:d => 'test' | ||
} | ||
} | ||
|
||
get("/any_of/nested", params) do |response| | ||
expect(response.status).to eq(400) | ||
expect(JSON.parse(response.body)['message']).to eq("Invalid parameters parent[[a, b, c]]") | ||
end | ||
end | ||
|
||
end | ||
end |