Commit 07255396 by slainer68

Test InvalidParameterError

parent 986874b6
The MIT License (MIT)
Copyright (c) 2014 Nicolas Blanco
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
......@@ -45,7 +45,8 @@ By declaring parameter types, incoming parameters will automatically be transfor
### Validations
Encapsulate business logic in a consistent way with validations. If a parameter does not satisfy a particular condition, a `400` error is returned with a message explaining the failure.
Encapsulate business logic in a consistent way with validations. If a parameter does not satisfy a particular condition, an exception (RailsParam::Param::InvalidParameterError) is raised.
You may use the [rescue_from](http://api.rubyonrails.org/classes/ActiveSupport/Rescuable/ClassMethods.html#method-i-rescue_from) method in your controller to catch this kind of exception.
- `required`
- `blank`
......
#!/usr/bin/env ruby
#
# This file was generated by Bundler.
#
# The application 'autospec' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
Pathname.new(__FILE__).realpath)
require 'rubygems'
require 'bundler/setup'
load Gem.bin_path('rspec-core', 'autospec')
#!/usr/bin/env ruby
#
# This file was generated by Bundler.
#
# The application 'rspec' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
Pathname.new(__FILE__).realpath)
require 'rubygems'
require 'bundler/setup'
load Gem.bin_path('rspec-core', 'rspec')
......@@ -15,21 +15,13 @@ module RailsParam
params[name] = (options[:default].call if options[:default].respond_to?(:call)) || options[:default] if params[name].nil? and options[:default]
params[name] = options[:transform].to_proc.call(params[name]) if params[name] and options[:transform]
validate!(params[name], options)
# rescue InvalidParameterError => exception
# if options[:raise]
# exception.param, exception.options = name, options
# raise exception
# end
# error = "Invalid Parameter: #{name}"
# if content_type and content_type.match(mime_type(:json))
# error = {message: error, errors: {name => exception.message}}.to_json
# end
# # do something with error object
rescue InvalidParameterError => exception
exception.param, exception.options = name, options
raise exception
end
end
# TODO: should we reintegrate this method?
# def one_of!(*names)
# count = 0
# names.each do |name|
......
......@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
s.authors = ["Nicolas Blanco"]
s.email = 'nicolas@nicolasblanco.fr'
s.homepage = 'http://github.com/nicolasblanco/rails_param'
s.license = 'WTFPL'
s.license = 'MIT'
s.description = %q{
Parameter Validation and Type Coercion for Rails
......
require 'fixtures/controllers'
require 'rspec/rails'
require 'pry'
describe FakeController, type: :controller do
describe "type coercion" do
......@@ -10,9 +11,13 @@ describe FakeController, type: :controller do
end
end
describe ":raise parameter" do
it "raises an exception if set" do
expect { get :index, sort: "foo" }.to raise_error(RailsParam::Param::InvalidParameterError)
describe "InvalidParameterError" do
it "raises an exception with params attributes" do
expect { get :index, sort: "foo" }.to raise_error { |error|
expect(error).to be_a(RailsParam::Param::InvalidParameterError)
expect(error.param).to eql("sort")
expect(error.options).to eql({ :in => ["asc", "desc"], :default => "asc", :transform => :downcase })
}
end
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment