spec/grape/validations_spec.rb in grape-1.6.0 vs spec/grape/validations_spec.rb in grape-1.6.1
- old
+ new
@@ -487,23 +487,29 @@
expect(last_response.status).to eq(400)
end
end
context 'custom validator for a Hash' do
- module ValuesSpec
- module DateRangeValidations
- class DateRangeValidator < Grape::Validations::Base
- def validate_param!(attr_name, params)
- return if params[attr_name][:from] <= params[attr_name][:to]
+ let(:date_range_validator) do
+ Class.new(Grape::Validations::Validators::Base) do
+ def validate_param!(attr_name, params)
+ return if params[attr_name][:from] <= params[attr_name][:to]
- raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: "'from' must be lower or equal to 'to'")
- end
+ raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: "'from' must be lower or equal to 'to'")
end
end
end
before do
+ described_class.register_validator('date_range', date_range_validator)
+ end
+
+ after do
+ described_class.deregister_validator('date_range')
+ end
+
+ before do
subject.params do
optional :date_range, date_range: true, type: Hash do
requires :from, type: Integer
requires :to, type: Integer
end
@@ -1181,20 +1187,28 @@
expect(last_response.body).to match(/swag is missing/)
end
end
context 'custom validation' do
- module CustomValidations
- class Customvalidator < Grape::Validations::Base
+ let(:custom_validator) do
+ Class.new(Grape::Validations::Validators::Base) do
def validate_param!(attr_name, params)
return if params[attr_name] == 'im custom'
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: 'is not custom!')
end
end
end
+ before do
+ described_class.register_validator('customvalidator', custom_validator)
+ end
+
+ after do
+ described_class.deregister_validator('customvalidator')
+ end
+
context 'when using optional with a custom validator' do
before do
subject.params do
optional :custom, customvalidator: true
end
@@ -1330,21 +1344,29 @@
end
end
end
context 'when using options on param' do
- module CustomValidations
- class CustomvalidatorWithOptions < Grape::Validations::Base
+ let(:custom_validator_with_options) do
+ Class.new(Grape::Validations::Validators::Base) do
def validate_param!(attr_name, params)
return if params[attr_name] == @option[:text]
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
end
end
end
before do
+ described_class.register_validator('customvalidator_with_options', custom_validator_with_options)
+ end
+
+ after do
+ described_class.deregister_validator('customvalidator_with_options')
+ end
+
+ before do
subject.params do
optional :custom, customvalidator_with_options: { text: 'im custom with options', message: 'is not custom with options!' }
end
subject.get '/optional_custom' do
'optional with custom works!'
@@ -1434,25 +1456,29 @@
order: params[:order],
order_by: params[:order_by]
}
end
end
+
it 'returns defaults' do
get '/order'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq({ order: :asc, order_by: :created_at }.to_json)
end
+
it 'overrides default value for order' do
get '/order?order=desc'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq({ order: :desc, order_by: :created_at }.to_json)
end
+
it 'overrides default value for order_by' do
get '/order?order_by=name'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq({ order: :asc, order_by: :name }.to_json)
end
+
it 'fails with invalid value' do
get '/order?order=invalid'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"order does not have a valid value"}')
end
@@ -1473,11 +1499,11 @@
end
end
context 'all or none' do
context 'optional params' do
- before :each do
+ before do
subject.resource :custom_message do
params do
optional :beer
optional :wine
optional :juice
@@ -1486,21 +1512,24 @@
get '/all_or_none' do
'all_or_none works!'
end
end
end
+
context 'with a custom validation message' do
it 'errors when any one is present' do
get '/custom_message/all_or_none', beer: 'string'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq 'beer, wine, juice all params are required or none is required'
end
+
it 'works when all params are present' do
get '/custom_message/all_or_none', beer: 'string', wine: 'anotherstring', juice: 'anotheranotherstring'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq 'all_or_none works!'
end
+
it 'works when none are present' do
get '/custom_message/all_or_none'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq 'all_or_none works!'
end
@@ -1660,11 +1689,11 @@
end
end
context 'exactly one of' do
context 'params' do
- before :each do
+ before do
subject.resources :custom_message do
params do
optional :beer
optional :wine
optional :juice
@@ -1724,11 +1753,11 @@
expect(last_response.body).to eq 'beer, wine are mutually exclusive'
end
end
context 'nested params' do
- before :each do
+ before do
subject.params do
requires :nested, type: Hash do
optional :beer_nested
optional :wine_nested
optional :juice_nested
@@ -1766,11 +1795,11 @@
end
end
context 'at least one of' do
context 'params' do
- before :each do
+ before do
subject.resources :custom_message do
params do
optional :beer
optional :wine
optional :juice
@@ -1830,10 +1859,10 @@
expect(last_response.body).to eq 'at_least_one_of works!'
end
end
context 'nested params' do
- before :each do
+ before do
subject.params do
requires :nested, type: Hash do
optional :beer
optional :wine
optional :juice