Commit 5799d1a7 by Nicolas Blanco

Merge pull request #7 from stantoncbradley/master

Hash, array, nested and BigDecimal feature
parents c6d98432 d594af0f
......@@ -47,7 +47,7 @@ end
### Parameter Types
By declaring parameter types, incoming parameters will automatically be transformed into an object of that type. For instance, if a param is `:boolean`, values of `'1'`, `'true'`, `'t'`, `'yes'`, and `'y'` will be automatically transformed into `true`.
By declaring parameter types, incoming parameters will automatically be transformed into an object of that type. For instance, if a param is `:boolean`, values of `'1'`, `'true'`, `'t'`, `'yes'`, and `'y'` will be automatically transformed into `true`. `BigDecimal` defaults to a precision of 14, but this can but changed by passing in the optional `precision:` argument. Any `$` and `,` are automatically stripped when converting to `BigDecimal`.
- `String`
- `Integer`
......@@ -56,6 +56,7 @@ By declaring parameter types, incoming parameters will automatically be transfor
- `Array` _("1,2,3,4,5")_
- `Hash` _("key1:value1,key2:value2")_
- `Date`, `Time`, & `DateTime`
- `BigDecimal` _("$1,000,000")_
### Validations
......@@ -85,6 +86,44 @@ param! :order, String, in: ["ASC", "DESC"], transform: :upcase, default: "ASC"
param! :offset, Integer, min: 0, transform: lambda {|n| n - (n % 10)}
```
### Nested Attributes
rails_param allows you to apply any of the above mentioned validations to attributes nested in hashes:
```ruby
param! :book, Hash do |b|
b.param! :title, String, blank: false
b.param! :price, BigDecimal, precision: 4, required: true
b.param! :author, Hash, required: true do |a|
a.param! :first_name, String
a.param! :last_name, String, blank: false
end
end
```
### Arrays
Validate every element of your array, including nested hashes and arrays:
```ruby
# primitive datatype syntax
param! :integer_array, Array do |array,index|
array.param! index, Integer, required: true
end
# complex array
param! :books_array, Array, required: true do |b|
b.param! :title, String, blank: false
b.param! :author, Hash, required: true, do |a|
a.param! :first_name, String
a.param! :last_name, String, required: true
end
b.param! :subjects, Array do |s,i|
s.param! i, String, blank: false
end
end
```
## Thank you
Many thanks to:
......
module RailsParam
module Param
DEFAULT_PRECISION = 14
class InvalidParameterError < StandardError
attr_accessor :param, :options
end
def param!(name, type, options = {})
name = name.to_s
class MockController
include RailsParam::Param
attr_accessor :params
end
def param!(name, type, options = {}, &block)
name = name.to_s unless name.is_a? Integer # keep index for validating elements
return unless params.member?(name) || options[:default].present? || options[:required]
......@@ -15,8 +22,25 @@ 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)
if block_given?
if type == Array
params[name].each_with_index do |element, i|
if element.is_a?(Hash)
recurse element, &block
else
params[name][i] = recurse({ i => element }, i, &block) # supply index as key unless value is hash
end
end
else
recurse params[name], &block
end
end
params[name]
rescue InvalidParameterError => exception
exception.param, exception.options = name, options
exception.param ||= name
exception.options ||= options
raise exception
end
end
......@@ -41,6 +65,13 @@ module RailsParam
private
def recurse(params, index = nil)
raise InvalidParameterError, 'no block given' unless block_given?
controller = RailsParam::Param::MockController.new
controller.params = params
yield(controller, index)
end
def coerce(param, type, options = {})
begin
return nil if param.nil?
......@@ -52,8 +83,12 @@ module RailsParam
return Time.parse(param) if type == Time
return DateTime.parse(param) if type == DateTime
return Array(param.split(options[:delimiter] || ",")) if type == Array
return Hash[param.split(options[:delimiter] || ",").map{|c| c.split(options[:separator] || ":")}] if type == Hash
return Hash[param.split(options[:delimiter] || ",").map { |c| c.split(options[:separator] || ":") }] if type == Hash
return (/(false|f|no|n|0)$/i === param.to_s ? false : (/(true|t|yes|y|1)$/i === param.to_s ? true : nil)) if type == TrueClass || type == FalseClass || type == :boolean
if type == BigDecimal
param = param.delete('$,').strip.to_f if param.is_a?(String)
return BigDecimal.new(param, (options[:precision] || DEFAULT_PRECISION))
end
return nil
rescue ArgumentError
raise InvalidParameterError, "'#{param}' is not a valid #{type}"
......
......@@ -17,4 +17,18 @@ class FakeController < ActionController::Base
def new
render text: "new"
end
def edit
param! :book, Hash, required: true do |b|
b.param! :title, String, required: true
b.param! :author, Hash do |a|
a.param! :first_name, String, required: true
a.param! :last_name, String, required: true
a.param! :age, Integer, required: true
end
b.param! :price, BigDecimal, required: true
end
render text: :book
end
end
......@@ -15,6 +15,7 @@ module Rails
get '/fake/new' => "fake#new"
get '/fakes' => "fake#index"
get '/fake/(:id)' => "fake#show"
get '/fake/edit' => "fake#edit"
end
@routes
end
......
......@@ -10,12 +10,60 @@ describe FakeController, type: :controller do
end
end
describe "nested_hash" do
it "validates nested properties" do
params = {
'book' => {
'title' => 'One Hundred Years of Solitude',
'author' => {
'first_name' => 'Garbriel Garcia',
'last_name' => 'Marquez',
'age' => '70'
},
'price' => '$1,000.00'
}}
get :edit, params
expect(controller.params[:book][:author][:age]).to eql 70
expect(controller.params[:book][:author][:age]).to be_kind_of Integer
expect(controller.params[:book][:price]).to eql 1000.0
expect(controller.params[:book][:price]).to be_instance_of BigDecimal
end
it "raises error when required nested attribute missing" do
params = {
'book' => {
'title' => 'One Hundred Years of Solitude',
'author' => {
'last_name' => 'Marquez',
'age' => '70'
},
'price' => '$1,000.00'
}}
expect { get :edit, params }.to raise_error { |error|
expect(error).to be_a(RailsParam::Param::InvalidParameterError)
expect(error.param).to eql("first_name")
expect(error.options).to eql({:required => true})
}
end
it "passes when hash that's not required but has required attributes is missing" do
params = {
'book' => {
'title' => 'One Hundred Years of Solitude',
'price' => '$1,000.00'
}}
get :edit, params
expect(controller.params[:book][:price]).to eql 1000.0
expect(controller.params[:book][:price]).to be_instance_of BigDecimal
end
end
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 })
expect(error.options).to eql({:in => ["asc", "desc"], :default => "asc", :transform => :downcase})
}
end
end
......
......@@ -4,7 +4,8 @@ require 'action_controller'
class MyController < ActionController::Base
include RailsParam::Param
def params; end
def params;
end
end
describe RailsParam::Param do
......@@ -18,7 +19,7 @@ describe RailsParam::Param do
describe "transform" do
context "with a method" do
it "transforms the value" do
allow(controller).to receive(:params).and_return({ "word" => "foo" })
allow(controller).to receive(:params).and_return({"word" => "foo"})
controller.param! :word, String, transform: :upcase
expect(controller.params["word"]).to eql("FOO")
end
......@@ -26,7 +27,7 @@ describe RailsParam::Param do
context "with a block" do
it "transforms the value" do
allow(controller).to receive(:params).and_return({ "word" => "FOO" })
allow(controller).to receive(:params).and_return({"word" => "FOO"})
controller.param! :word, String, transform: lambda { |n| n.downcase }
expect(controller.params["word"]).to eql("foo")
end
......@@ -36,7 +37,7 @@ describe RailsParam::Param do
describe "default" do
context "with a value" do
it "defaults to the value" do
allow(controller).to receive(:params).and_return({ })
allow(controller).to receive(:params).and_return({})
controller.param! :word, String, default: "foo"
expect(controller.params["word"]).to eql("foo")
end
......@@ -44,7 +45,7 @@ describe RailsParam::Param do
context "with a block" do
it "defaults to the block value" do
allow(controller).to receive(:params).and_return({ })
allow(controller).to receive(:params).and_return({})
controller.param! :word, String, default: lambda { "foo" }
expect(controller.params["word"]).to eql("foo")
end
......@@ -53,210 +54,365 @@ describe RailsParam::Param do
describe "coerce" do
it "converts to String" do
allow(controller).to receive(:params).and_return({ "foo" => :bar })
allow(controller).to receive(:params).and_return({"foo" => :bar})
controller.param! :foo, String
expect(controller.params["foo"]).to eql("bar")
end
it "converts to Integer" do
allow(controller).to receive(:params).and_return({ "foo" => "42" })
allow(controller).to receive(:params).and_return({"foo" => "42"})
controller.param! :foo, Integer
expect(controller.params["foo"]).to eql(42)
end
it "converts to Float" do
allow(controller).to receive(:params).and_return({ "foo" => "42.22" })
allow(controller).to receive(:params).and_return({"foo" => "42.22"})
controller.param! :foo, Float
expect(controller.params["foo"]).to eql(42.22)
end
it "converts to Array" do
allow(controller).to receive(:params).and_return({ "foo" => "2,3,4,5" })
allow(controller).to receive(:params).and_return({"foo" => "2,3,4,5"})
controller.param! :foo, Array
expect(controller.params["foo"]).to eql(["2", "3", "4", "5"])
end
it "converts to Hash" do
allow(controller).to receive(:params).and_return({ "foo" => "key1:foo,key2:bar" })
allow(controller).to receive(:params).and_return({"foo" => "key1:foo,key2:bar"})
controller.param! :foo, Hash
expect(controller.params["foo"]).to eql({ "key1" => "foo", "key2" => "bar" })
expect(controller.params["foo"]).to eql({"key1" => "foo", "key2" => "bar"})
end
it "converts to Date" do
allow(controller).to receive(:params).and_return({ "foo" => "1984-01-10" })
allow(controller).to receive(:params).and_return({"foo" => "1984-01-10"})
controller.param! :foo, Date
expect(controller.params["foo"]).to eql(Date.parse("1984-01-10"))
end
it "converts to Time" do
allow(controller).to receive(:params).and_return({ "foo" => "2014-08-07T12:25:00.000+02:00" })
allow(controller).to receive(:params).and_return({"foo" => "2014-08-07T12:25:00.000+02:00"})
controller.param! :foo, Time
expect(controller.params["foo"]).to eql(Time.parse("2014-08-07T12:25:00.000+02:00"))
end
it "converts to DateTime" do
allow(controller).to receive(:params).and_return({ "foo" => "2014-08-07T12:25:00.000+02:00" })
allow(controller).to receive(:params).and_return({"foo" => "2014-08-07T12:25:00.000+02:00"})
controller.param! :foo, DateTime
expect(controller.params["foo"]).to eql(DateTime.parse("2014-08-07T12:25:00.000+02:00"))
end
describe "BigDecimals" do
it "converts to BigDecimal using default precision" do
allow(controller).to receive(:params).and_return({"foo" => 12345.67890123456})
controller.param! :foo, BigDecimal
expect(controller.params["foo"]).to eql 12345.678901235
end
it "converts to BigDecimal using precision option" do
allow(controller).to receive(:params).and_return({"foo" => 12345.6789})
controller.param! :foo, BigDecimal, precision: 6
expect(controller.params["foo"]).to eql 12345.7
end
it "converts formatted currency string to big decimal" do
allow(controller).to receive(:params).and_return({"foo" => "$100,000"})
controller.param! :foo, BigDecimal
expect(controller.params["foo"]).to eql 100000.0
end
end
describe "booleans" do
it "converts 1/0" do
allow(controller).to receive(:params).and_return({ "foo" => "1" })
allow(controller).to receive(:params).and_return({"foo" => "1"})
controller.param! :foo, TrueClass
expect(controller.params["foo"]).to eql(true)
allow(controller).to receive(:params).and_return({ "foo" => "0" })
allow(controller).to receive(:params).and_return({"foo" => "0"})
controller.param! :foo, TrueClass
expect(controller.params["foo"]).to eql(false)
end
it "converts true/false" do
allow(controller).to receive(:params).and_return({ "foo" => "true" })
allow(controller).to receive(:params).and_return({"foo" => "true"})
controller.param! :foo, TrueClass
expect(controller.params["foo"]).to eql(true)
allow(controller).to receive(:params).and_return({ "foo" => "false" })
allow(controller).to receive(:params).and_return({"foo" => "false"})
controller.param! :foo, TrueClass
expect(controller.params["foo"]).to eql(false)
end
it "converts t/f" do
allow(controller).to receive(:params).and_return({ "foo" => "t" })
allow(controller).to receive(:params).and_return({"foo" => "t"})
controller.param! :foo, TrueClass
expect(controller.params["foo"]).to eql(true)
allow(controller).to receive(:params).and_return({ "foo" => "f" })
allow(controller).to receive(:params).and_return({"foo" => "f"})
controller.param! :foo, TrueClass
expect(controller.params["foo"]).to eql(false)
end
it "converts yes/no" do
allow(controller).to receive(:params).and_return({ "foo" => "yes" })
allow(controller).to receive(:params).and_return({"foo" => "yes"})
controller.param! :foo, TrueClass
expect(controller.params["foo"]).to eql(true)
allow(controller).to receive(:params).and_return({ "foo" => "no" })
allow(controller).to receive(:params).and_return({"foo" => "no"})
controller.param! :foo, TrueClass
expect(controller.params["foo"]).to eql(false)
end
it "converts y/n" do
allow(controller).to receive(:params).and_return({ "foo" => "y" })
allow(controller).to receive(:params).and_return({"foo" => "y"})
controller.param! :foo, TrueClass
expect(controller.params["foo"]).to eql(true)
allow(controller).to receive(:params).and_return({ "foo" => "n" })
allow(controller).to receive(:params).and_return({"foo" => "n"})
controller.param! :foo, TrueClass
expect(controller.params["foo"]).to eql(false)
end
end
it "raises InvalidParameterError if the value is invalid" do
allow(controller).to receive(:params).and_return({ "foo" => "1984-01-32" })
allow(controller).to receive(:params).and_return({"foo" => "1984-01-32"})
expect { controller.param! :foo, Date }.to raise_error(RailsParam::Param::InvalidParameterError)
end
end
describe 'validating nested hash' do
it 'typecasts nested attributes' do
allow(controller).to receive(:params).and_return({'foo' => {'bar' => 1, 'baz' => 2}})
controller.param! :foo, Hash do |p|
p.param! :bar, BigDecimal
p.param! :baz, Float
end
expect(controller.params['foo']['bar']).to be_instance_of BigDecimal
expect(controller.params['foo']['baz']).to be_instance_of Float
end
it 'does not raise exception if hash is not required but nested attributes are, and no hash is provided' do
allow(controller).to receive(:params).and_return(foo: nil)
controller.param! :foo, Hash do |p|
p.param! :bar, BigDecimal, required: true
p.param! :baz, Float, required: true
end
expect(controller.params['foo']).to be_nil
end
it 'raises exception if hash is required, nested attributes are not required, and no hash is provided' do
allow(controller).to receive(:params).and_return(foo: nil)
expect {
controller.param! :foo, Hash, required: true do |p|
p.param! :bar, BigDecimal
p.param! :baz, Float
end
}.to raise_exception
end
it 'raises exception if hash is not required but nested attributes are, and hash has missing attributes' do
allow(controller).to receive(:params).and_return({'foo' => {'bar' => 1, 'baz' => nil}})
expect {
controller.param! :foo, Hash do |p|
p.param! :bar, BigDecimal, required: true
p.param! :baz, Float, required: true
end
}.to raise_exception
end
end
describe 'validating arrays' do
it 'typecasts array of primitive elements' do
allow(controller).to receive(:params).and_return({'array' => ['1', '2']})
controller.param! :array, Array do |a, i|
a.param! i, Integer, required: true
end
expect(controller.params['array'][0]).to be_a Integer
expect(controller.params['array'][1]).to be_a Integer
end
it 'validates array of hashes' do
params = {'array' => [{'object'=>{ 'num' => '1', 'float' => '1.5' }},{'object'=>{ 'num' => '2', 'float' => '2.3' }}] }
allow(controller).to receive(:params).and_return(params)
controller.param! :array, Array do |a|
a.param! :object, Hash do |h|
h.param! :num, Integer, required: true
h.param! :float, Float, required: true
end
end
expect(controller.params['array'][0]['object']['num']).to be_a Integer
expect(controller.params['array'][0]['object']['float']).to be_instance_of Float
expect(controller.params['array'][1]['object']['num']).to be_a Integer
expect(controller.params['array'][1]['object']['float']).to be_instance_of Float
end
it 'validates an array of arrays' do
params = {'array' => [[ '1', '2' ],[ '3', '4' ]] }
allow(controller).to receive(:params).and_return(params)
controller.param! :array, Array do |a, i|
a.param! i, Array do |b, e|
b.param! e, Integer, required: true
end
end
expect(controller.params['array'][0][0]).to be_a Integer
expect(controller.params['array'][0][1]).to be_a Integer
expect(controller.params['array'][1][0]).to be_a Integer
expect(controller.params['array'][1][1]).to be_a Integer
end
it 'raises exception when primitive element missing' do
allow(controller).to receive(:params).and_return({'array' => ['1', nil]})
expect {
controller.param! :array, Array do |a, i|
a.param! i, Integer, required: true
end
}.to raise_exception
end
it 'raises exception when nested hash element missing' do
params = {'array' => [{'object'=>{ 'num' => '1', 'float' => nil }},{'object'=>{ 'num' => '2', 'float' => '2.3' }}] }
allow(controller).to receive(:params).and_return(params)
expect {
controller.param! :array, Array do |a|
a.param! :object, Hash do |h|
h.param! :num, Integer, required: true
h.param! :float, Float, required: true
end
end
}.to raise_exception
end
it 'raises exception when nested array element missing' do
params = {'array' => [[ '1', '2' ],[ '3', nil ]] }
allow(controller).to receive(:params).and_return(params)
expect {
controller.param! :array, Array do |a, i|
a.param! i, Array do |b, e|
b.param! e, Integer, required: true
end
end
}.to raise_exception
end
it 'does not raise exception if array is not required but nested attributes are, and no array is provided' do
allow(controller).to receive(:params).and_return(foo: nil)
controller.param! :foo, Array do |p|
p.param! :bar, BigDecimal, required: true
p.param! :baz, Float, required: true
end
expect(controller.params['foo']).to be_nil
end
it 'raises exception if array is required, nested attributes are not required, and no array is provided' do
allow(controller).to receive(:params).and_return(foo: nil)
expect {
controller.param! :foo, Array, required: true do |p|
p.param! :bar, BigDecimal
p.param! :baz, Float
end
}.to raise_exception
end
end
describe "validation" do
describe "required parameter" do
it "succeeds" do
allow(controller).to receive(:params).and_return({ "price" => "50" })
allow(controller).to receive(:params).and_return({"price" => "50"})
expect { controller.param! :price, Integer, required: true }.to_not raise_error
end
it "raises" do
allow(controller).to receive(:params).and_return({ })
allow(controller).to receive(:params).and_return({})
expect { controller.param! :price, Integer, required: true }.to raise_error(RailsParam::Param::InvalidParameterError)
end
end
describe "blank parameter" do
it "succeeds" do
allow(controller).to receive(:params).and_return({ "price" => "50" })
allow(controller).to receive(:params).and_return({"price" => "50"})
expect { controller.param! :price, String, blank: false }.to_not raise_error
end
it "raises" do
allow(controller).to receive(:params).and_return({ "price" => "" })
allow(controller).to receive(:params).and_return({"price" => ""})
expect { controller.param! :price, String, blank: false }.to raise_error(RailsParam::Param::InvalidParameterError)
end
end
describe "format parameter" do
it "succeeds" do
allow(controller).to receive(:params).and_return({ "price" => "50$" })
allow(controller).to receive(:params).and_return({"price" => "50$"})
expect { controller.param! :price, String, format: /[0-9]+\$/ }.to_not raise_error
end
it "raises" do
allow(controller).to receive(:params).and_return({ "price" => "50" })
allow(controller).to receive(:params).and_return({"price" => "50"})
expect { controller.param! :price, String, format: /[0-9]+\$/ }.to raise_error(RailsParam::Param::InvalidParameterError)
end
end
describe "is parameter" do
it "succeeds" do
allow(controller).to receive(:params).and_return({ "price" => "50" })
allow(controller).to receive(:params).and_return({"price" => "50"})
expect { controller.param! :price, String, is: "50" }.to_not raise_error
end
it "raises" do
allow(controller).to receive(:params).and_return({ "price" => "51" })
allow(controller).to receive(:params).and_return({"price" => "51"})
expect { controller.param! :price, String, is: "50" }.to raise_error(RailsParam::Param::InvalidParameterError)
end
end
describe "min parameter" do
it "succeeds" do
allow(controller).to receive(:params).and_return({ "price" => "50" })
allow(controller).to receive(:params).and_return({"price" => "50"})
expect { controller.param! :price, Integer, min: 50 }.to_not raise_error
end
it "raises" do
allow(controller).to receive(:params).and_return({ "price" => "50" })
allow(controller).to receive(:params).and_return({"price" => "50"})
expect { controller.param! :price, Integer, min: 51 }.to raise_error(RailsParam::Param::InvalidParameterError)
end
end
describe "max parameter" do
it "succeeds" do
allow(controller).to receive(:params).and_return({ "price" => "50" })
allow(controller).to receive(:params).and_return({"price" => "50"})
expect { controller.param! :price, Integer, max: 50 }.to_not raise_error
end
it "raises" do
allow(controller).to receive(:params).and_return({ "price" => "50" })
allow(controller).to receive(:params).and_return({"price" => "50"})
expect { controller.param! :price, Integer, max: 49 }.to raise_error(RailsParam::Param::InvalidParameterError)
end
end
describe "min_length parameter" do
it "succeeds" do
allow(controller).to receive(:params).and_return({ "word" => "foo" })
allow(controller).to receive(:params).and_return({"word" => "foo"})
expect { controller.param! :word, String, min_length: 3 }.to_not raise_error
end
it "raises" do
allow(controller).to receive(:params).and_return({ "word" => "foo" })
allow(controller).to receive(:params).and_return({"word" => "foo"})
expect { controller.param! :word, String, min_length: 4 }.to raise_error(RailsParam::Param::InvalidParameterError)
end
end
describe "max_length parameter" do
it "succeeds" do
allow(controller).to receive(:params).and_return({ "word" => "foo" })
allow(controller).to receive(:params).and_return({"word" => "foo"})
expect { controller.param! :word, String, max_length: 3 }.to_not raise_error
end
it "raises" do
allow(controller).to receive(:params).and_return({ "word" => "foo" })
allow(controller).to receive(:params).and_return({"word" => "foo"})
expect { controller.param! :word, String, max_length: 2 }.to raise_error(RailsParam::Param::InvalidParameterError)
end
end
describe "in, within, range parameters" do
before(:each) { allow(controller).to receive(:params).and_return({ "price" => "50" }) }
before(:each) { allow(controller).to receive(:params).and_return({"price" => "50"}) }
it "succeeds in the range" do
controller.param! :price, Integer, in: 1..100
......
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