|
1 | 1 | require 'spec_helper'
|
2 | 2 |
|
3 |
| -describe Grape::Validations do |
4 |
| - module ValidationsSpec |
5 |
| - class API < Grape::API |
6 |
| - default_format :json |
7 |
| - |
8 |
| - params do |
9 |
| - requires :name, :company |
10 |
| - optional :a_number, :regexp => /^[0-9]+$/ |
| 3 | +module CustomValidations |
| 4 | + class Customvalidator < Grape::Validations::Validator |
| 5 | + def validate_param!(attr_name, params) |
| 6 | + unless params[attr_name] == 'im custom' |
| 7 | + throw :error, :status => 400, :message => "#{attr_name}: is not custom!" |
| 8 | + end |
| 9 | + end |
| 10 | + end |
| 11 | +end |
| 12 | + |
| 13 | +describe Grape::API do |
| 14 | + subject { Class.new(Grape::API) } |
| 15 | + def app; subject end |
| 16 | + |
| 17 | + describe 'params' do |
| 18 | + it 'validates optional parameter if present' do |
| 19 | + subject.params { optional :a_number, :regexp => /^[0-9]+$/ } |
| 20 | + subject.get '/optional' do 'optional works!'; end |
| 21 | + |
| 22 | + get '/optional', { :a_number => 'string' } |
| 23 | + last_response.status.should == 400 |
| 24 | + last_response.body.should == 'invalid parameter: a_number' |
| 25 | + |
| 26 | + get '/optional', { :a_number => 45 } |
| 27 | + last_response.status.should == 200 |
| 28 | + last_response.body.should == 'optional works!' |
| 29 | + end |
| 30 | + |
| 31 | + context 'when using optional with a custom validator' do |
| 32 | + before do |
| 33 | + subject.params { optional :custom, :customvalidator => true } |
| 34 | + subject.get '/optional_custom' do 'optional with custom works!'; end |
11 | 35 | end
|
12 |
| - |
13 |
| - get do |
14 |
| - "Hello" |
| 36 | + |
| 37 | + it 'validates when param is present' do |
| 38 | + get '/optional_custom', { :custom => 'im custom' } |
| 39 | + last_response.status.should == 200 |
| 40 | + last_response.body.should == 'optional with custom works!' |
| 41 | + |
| 42 | + get '/optional_custom', { :custom => 'im wrong' } |
| 43 | + last_response.status.should == 400 |
| 44 | + last_response.body.should == 'custom: is not custom!' |
15 | 45 | end
|
16 |
| - end |
17 |
| - end |
18 |
| - |
19 |
| - def app |
20 |
| - ValidationsSpec::API |
21 |
| - end |
22 | 46 |
|
23 |
| - it 'validates optional parameter if present' do |
24 |
| - get('/', :name => "Bob", :company => "TestCorp", :a_number => "string") |
25 |
| - last_response.status.should == 400 |
26 |
| - last_response.body.should == "invalid parameter: a_number" |
| 47 | + it "skip validation when parameter isn't present" do |
| 48 | + get '/optional_custom' |
| 49 | + last_response.status.should == 200 |
| 50 | + last_response.body.should == 'optional with custom works!' |
| 51 | + end |
| 52 | + |
| 53 | + it 'validates with custom validator when param present and incorrect type' do |
| 54 | + subject.params { optional :custom, :type => String, :customvalidator => true } |
27 | 55 |
|
28 |
| - get('/', :name => "Bob", :company => "TestCorp", :a_number => 45) |
29 |
| - last_response.status.should == 200 |
30 |
| - last_response.body.should == "Hello" |
| 56 | + get '/optional_custom', { :custom => 123 } |
| 57 | + last_response.status.should == 400 |
| 58 | + last_response.body.should == 'custom: is not custom!' |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + context 'when using requires with a custom validator' do |
| 63 | + before do |
| 64 | + subject.params { requires :custom, :customvalidator => true } |
| 65 | + subject.get '/required_custom' do 'required with custom works!'; end |
| 66 | + end |
| 67 | + |
| 68 | + it 'validates when param is present' do |
| 69 | + get '/required_custom', { :custom => 'im wrong, validate me' } |
| 70 | + last_response.status.should == 400 |
| 71 | + last_response.body.should == 'custom: is not custom!' |
| 72 | + |
| 73 | + get '/required_custom', { :custom => 'im custom' } |
| 74 | + last_response.status.should == 200 |
| 75 | + last_response.body.should == 'required with custom works!' |
| 76 | + end |
| 77 | + |
| 78 | + it 'validates when param is not present' do |
| 79 | + get '/required_custom' |
| 80 | + last_response.status.should == 400 |
| 81 | + last_response.body.should == 'custom: is not custom!' |
| 82 | + end |
| 83 | + end |
31 | 84 | end
|
32 |
| - |
33 | 85 | end
|
0 commit comments