@@ -49,20 +49,20 @@ def app; subject end
49
49
end
50
50
51
51
context 'custom validation' do
52
- module CustomValidations
52
+ module CustomValidations
53
53
class Customvalidator < Grape ::Validations ::Validator
54
54
def validate_param! ( attr_name , params )
55
55
unless params [ attr_name ] == 'im custom'
56
56
throw :error , :status => 400 , :message => "#{ attr_name } : is not custom!"
57
- end
57
+ end
58
58
end
59
- end
60
- end
59
+ end
60
+ end
61
61
62
62
context 'when using optional with a custom validator' do
63
63
before do
64
64
subject . params { optional :custom , :customvalidator => true }
65
- subject . get '/optional_custom' do 'optional with custom works!' ; end
65
+ subject . get '/optional_custom' do 'optional with custom works!' ; end
66
66
end
67
67
68
68
it 'validates when param is present' do
@@ -83,7 +83,7 @@ def validate_param!(attr_name, params)
83
83
84
84
it 'validates with custom validator when param present and incorrect type' do
85
85
subject . params { optional :custom , :type => String , :customvalidator => true }
86
-
86
+
87
87
get '/optional_custom' , { :custom => 123 }
88
88
last_response . status . should == 400
89
89
last_response . body . should == 'custom: is not custom!'
@@ -111,6 +111,44 @@ def validate_param!(attr_name, params)
111
111
last_response . status . should == 400
112
112
last_response . body . should == 'missing parameter: custom'
113
113
end
114
+
115
+ context 'nested namespaces' do
116
+ before do
117
+ subject . params { requires :custom , :customvalidator => true }
118
+ subject . namespace 'nested' do
119
+ get 'one' do 'validation failed' end
120
+ namespace 'nested' do
121
+ get 'two' do 'validation failed' end
122
+ end
123
+ end
124
+ subject . namespace 'peer' do
125
+ get 'one' do 'no validation required' end
126
+ namespace 'nested' do
127
+ get 'two' do 'no validation required' end
128
+ end
129
+ end
130
+ end
131
+ specify 'the parent namespace should use the validator' do
132
+ get '/nested/one' , { :custom => 'im wrong, validate me' }
133
+ last_response . status . should == 400
134
+ last_response . body . should == 'custom: is not custom!'
135
+ end
136
+ specify 'the nested namesapce should inherit the custom validator' do
137
+ get '/nested/nested/two' , { :custom => 'im wrong, validate me' }
138
+ last_response . status . should == 400
139
+ last_response . body . should == 'custom: is not custom!'
140
+ end
141
+ specify 'peer namesapces should not have the validator' do
142
+ get '/peer/one' , { :custom => 'im not validated' }
143
+ last_response . status . should == 200
144
+ last_response . body . should == 'no validation required'
145
+ end
146
+ specify 'namespaces nested in peers should also not have the validator' do
147
+ get '/peer/nested/two' , { :custom => 'im not validated' }
148
+ last_response . status . should == 200
149
+ last_response . body . should == 'no validation required'
150
+ end
151
+ end
114
152
end
115
153
end # end custom validation
116
154
end
0 commit comments