-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: #211: Custom validator shouldn't be triggered when optional and …
…not present Updated validator to only run custom optional validation when the param is present. Re-wrote validations_spec to be a little more robust. Added section to documentation for custom validation.
- Loading branch information
1 parent
261b40d
commit 5ec9c57
Showing
4 changed files
with
125 additions
and
32 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 |
---|---|---|
@@ -1,33 +1,85 @@ | ||
require 'spec_helper' | ||
|
||
describe Grape::Validations do | ||
module ValidationsSpec | ||
class API < Grape::API | ||
default_format :json | ||
|
||
params do | ||
requires :name, :company | ||
optional :a_number, :regexp => /^[0-9]+$/ | ||
module CustomValidations | ||
class Customvalidator < Grape::Validations::Validator | ||
def validate_param!(attr_name, params) | ||
unless params[attr_name] == 'im custom' | ||
throw :error, :status => 400, :message => "#{attr_name}: is not custom!" | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe Grape::API do | ||
subject { Class.new(Grape::API) } | ||
def app; subject end | ||
|
||
describe 'params' do | ||
it 'validates optional parameter if present' do | ||
subject.params { optional :a_number, :regexp => /^[0-9]+$/ } | ||
subject.get '/optional' do 'optional works!'; end | ||
|
||
get '/optional', { :a_number => 'string' } | ||
last_response.status.should == 400 | ||
last_response.body.should == 'invalid parameter: a_number' | ||
|
||
get '/optional', { :a_number => 45 } | ||
last_response.status.should == 200 | ||
last_response.body.should == 'optional works!' | ||
end | ||
|
||
context 'when using optional with a custom validator' do | ||
before do | ||
subject.params { optional :custom, :customvalidator => true } | ||
subject.get '/optional_custom' do 'optional with custom works!'; end | ||
end | ||
|
||
get do | ||
"Hello" | ||
|
||
it 'validates when param is present' do | ||
get '/optional_custom', { :custom => 'im custom' } | ||
last_response.status.should == 200 | ||
last_response.body.should == 'optional with custom works!' | ||
|
||
get '/optional_custom', { :custom => 'im wrong' } | ||
last_response.status.should == 400 | ||
last_response.body.should == 'custom: is not custom!' | ||
end | ||
end | ||
end | ||
|
||
def app | ||
ValidationsSpec::API | ||
end | ||
|
||
it 'validates optional parameter if present' do | ||
get('/', :name => "Bob", :company => "TestCorp", :a_number => "string") | ||
last_response.status.should == 400 | ||
last_response.body.should == "invalid parameter: a_number" | ||
it "skip validation when parameter isn't present" do | ||
get '/optional_custom' | ||
last_response.status.should == 200 | ||
last_response.body.should == 'optional with custom works!' | ||
end | ||
|
||
it 'validates with custom validator when param present and incorrect type' do | ||
subject.params { optional :custom, :type => String, :customvalidator => true } | ||
|
||
get('/', :name => "Bob", :company => "TestCorp", :a_number => 45) | ||
last_response.status.should == 200 | ||
last_response.body.should == "Hello" | ||
get '/optional_custom', { :custom => 123 } | ||
last_response.status.should == 400 | ||
last_response.body.should == 'custom: is not custom!' | ||
end | ||
end | ||
|
||
context 'when using requires with a custom validator' do | ||
before do | ||
subject.params { requires :custom, :customvalidator => true } | ||
subject.get '/required_custom' do 'required with custom works!'; end | ||
end | ||
|
||
it 'validates when param is present' do | ||
get '/required_custom', { :custom => 'im wrong, validate me' } | ||
last_response.status.should == 400 | ||
last_response.body.should == 'custom: is not custom!' | ||
|
||
get '/required_custom', { :custom => 'im custom' } | ||
last_response.status.should == 200 | ||
last_response.body.should == 'required with custom works!' | ||
end | ||
|
||
it 'validates when param is not present' do | ||
get '/required_custom' | ||
last_response.status.should == 400 | ||
last_response.body.should == 'custom: is not custom!' | ||
end | ||
end | ||
end | ||
|
||
end |