Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
rails_param
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
ikcrm_common
rails_param
Commits
ca96eb98
Commit
ca96eb98
authored
Oct 29, 2014
by
Brad Urani
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3 from stantoncbradley/develop
add recursive functionality for validating nested attributes
parents
e0c42cb8
5187e6a6
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
167 additions
and
80 deletions
+167
-80
lib/rails_param/param.rb
+45
-34
spec/fixtures/controllers.rb
+14
-0
spec/fixtures/fake_rails_application.rb
+1
-0
spec/rails_integration_spec.rb
+49
-1
spec/rails_param/param_spec.rb
+58
-45
No files found.
lib/rails_param/param.rb
View file @
ca96eb98
...
@@ -7,6 +7,11 @@ module RailsParam
...
@@ -7,6 +7,11 @@ module RailsParam
attr_accessor
:param
,
:options
attr_accessor
:param
,
:options
end
end
class
MockController
include
RailsParam
::
Param
attr_accessor
:params
end
def
param!
(
name
,
type
,
options
=
{})
def
param!
(
name
,
type
,
options
=
{})
name
=
name
.
to_s
name
=
name
.
to_s
...
@@ -17,8 +22,14 @@ module RailsParam
...
@@ -17,8 +22,14 @@ 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
[
: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
]
params
[
name
]
=
options
[
:transform
].
to_proc
.
call
(
params
[
name
])
if
params
[
name
]
and
options
[
:transform
]
validate!
(
params
[
name
],
options
)
validate!
(
params
[
name
],
options
)
if
block_given?
controller
=
RailsParam
::
Param
::
MockController
.
new
controller
.
params
=
params
[
name
]
yield
(
controller
)
end
rescue
InvalidParameterError
=>
exception
rescue
InvalidParameterError
=>
exception
exception
.
param
,
exception
.
options
=
name
,
options
exception
.
param
||=
name
exception
.
options
||=
options
raise
exception
raise
exception
end
end
end
end
...
@@ -54,11 +65,11 @@ module RailsParam
...
@@ -54,11 +65,11 @@ module RailsParam
return
Time
.
parse
(
param
)
if
type
==
Time
return
Time
.
parse
(
param
)
if
type
==
Time
return
DateTime
.
parse
(
param
)
if
type
==
DateTime
return
DateTime
.
parse
(
param
)
if
type
==
DateTime
return
Array
(
param
.
split
(
options
[
:delimiter
]
||
","
))
if
type
==
Array
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
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
if
type
==
BigDecimal
param
=
param
.
delete
(
'$,'
).
strip
.
to_f
if
param
.
is_a?
(
String
)
param
=
param
.
delete
(
'$,'
).
strip
.
to_f
if
param
.
is_a?
(
String
)
return
BigDecimal
.
new
(
param
,
(
options
[
:precision
]
||
DEFAULT_PRECISION
))
if
type
==
BigDecimal
return
BigDecimal
.
new
(
param
,
(
options
[
:precision
]
||
DEFAULT_PRECISION
))
end
end
return
nil
return
nil
rescue
ArgumentError
rescue
ArgumentError
...
@@ -69,37 +80,37 @@ module RailsParam
...
@@ -69,37 +80,37 @@ module RailsParam
def
validate!
(
param
,
options
)
def
validate!
(
param
,
options
)
options
.
each
do
|
key
,
value
|
options
.
each
do
|
key
,
value
|
case
key
case
key
when
:required
when
:required
raise
InvalidParameterError
,
"Parameter is required"
if
value
&&
param
.
nil?
raise
InvalidParameterError
,
"Parameter is required"
if
value
&&
param
.
nil?
when
:blank
when
:blank
raise
InvalidParameterError
,
"Parameter cannot be blank"
if
!
value
&&
case
param
raise
InvalidParameterError
,
"Parameter cannot be blank"
if
!
value
&&
case
param
when
String
when
String
!
(
/\S/
===
param
)
!
(
/\S/
===
param
)
when
Array
,
Hash
when
Array
,
Hash
param
.
empty?
param
.
empty?
else
else
param
.
nil?
param
.
nil?
end
end
when
:format
when
:format
raise
InvalidParameterError
,
"Parameter must be a string if using the format validation"
unless
param
.
kind_of?
(
String
)
raise
InvalidParameterError
,
"Parameter must be a string if using the format validation"
unless
param
.
kind_of?
(
String
)
raise
InvalidParameterError
,
"Parameter must match format
#{
value
}
"
unless
param
=~
value
raise
InvalidParameterError
,
"Parameter must match format
#{
value
}
"
unless
param
=~
value
when
:is
when
:is
raise
InvalidParameterError
,
"Parameter must be
#{
value
}
"
unless
param
===
value
raise
InvalidParameterError
,
"Parameter must be
#{
value
}
"
unless
param
===
value
when
:in
,
:within
,
:range
when
:in
,
:within
,
:range
raise
InvalidParameterError
,
"Parameter must be within
#{
value
}
"
unless
param
.
nil?
||
case
value
raise
InvalidParameterError
,
"Parameter must be within
#{
value
}
"
unless
param
.
nil?
||
case
value
when
Range
when
Range
value
.
include?
(
param
)
value
.
include?
(
param
)
else
else
Array
(
value
).
include?
(
param
)
Array
(
value
).
include?
(
param
)
end
end
when
:min
when
:min
raise
InvalidParameterError
,
"Parameter cannot be less than
#{
value
}
"
unless
param
.
nil?
||
value
<=
param
raise
InvalidParameterError
,
"Parameter cannot be less than
#{
value
}
"
unless
param
.
nil?
||
value
<=
param
when
:max
when
:max
raise
InvalidParameterError
,
"Parameter cannot be greater than
#{
value
}
"
unless
param
.
nil?
||
value
>=
param
raise
InvalidParameterError
,
"Parameter cannot be greater than
#{
value
}
"
unless
param
.
nil?
||
value
>=
param
when
:min_length
when
:min_length
raise
InvalidParameterError
,
"Parameter cannot have length less than
#{
value
}
"
unless
param
.
nil?
||
value
<=
param
.
length
raise
InvalidParameterError
,
"Parameter cannot have length less than
#{
value
}
"
unless
param
.
nil?
||
value
<=
param
.
length
when
:max_length
when
:max_length
raise
InvalidParameterError
,
"Parameter cannot have length greater than
#{
value
}
"
unless
param
.
nil?
||
value
>=
param
.
length
raise
InvalidParameterError
,
"Parameter cannot have length greater than
#{
value
}
"
unless
param
.
nil?
||
value
>=
param
.
length
end
end
end
end
end
end
...
...
spec/fixtures/controllers.rb
View file @
ca96eb98
...
@@ -17,4 +17,18 @@ class FakeController < ActionController::Base
...
@@ -17,4 +17,18 @@ class FakeController < ActionController::Base
def
new
def
new
render
text:
"new"
render
text:
"new"
end
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
end
spec/fixtures/fake_rails_application.rb
View file @
ca96eb98
...
@@ -15,6 +15,7 @@ module Rails
...
@@ -15,6 +15,7 @@ module Rails
get
'/fake/new'
=>
"fake#new"
get
'/fake/new'
=>
"fake#new"
get
'/fakes'
=>
"fake#index"
get
'/fakes'
=>
"fake#index"
get
'/fake/(:id)'
=>
"fake#show"
get
'/fake/(:id)'
=>
"fake#show"
get
'/fake/edit'
=>
"fake#edit"
end
end
@routes
@routes
end
end
...
...
spec/rails_integration_spec.rb
View file @
ca96eb98
...
@@ -10,12 +10,60 @@ describe FakeController, type: :controller do
...
@@ -10,12 +10,60 @@ describe FakeController, type: :controller do
end
end
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
describe
"InvalidParameterError"
do
it
"raises an exception with params attributes"
do
it
"raises an exception with params attributes"
do
expect
{
get
:index
,
sort:
"foo"
}.
to
raise_error
{
|
error
|
expect
{
get
:index
,
sort:
"foo"
}.
to
raise_error
{
|
error
|
expect
(
error
).
to
be_a
(
RailsParam
::
Param
::
InvalidParameterError
)
expect
(
error
).
to
be_a
(
RailsParam
::
Param
::
InvalidParameterError
)
expect
(
error
.
param
).
to
eql
(
"sort"
)
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
end
end
...
...
spec/rails_param/param_spec.rb
View file @
ca96eb98
...
@@ -4,7 +4,8 @@ require 'action_controller'
...
@@ -4,7 +4,8 @@ require 'action_controller'
class
MyController
<
ActionController
::
Base
class
MyController
<
ActionController
::
Base
include
RailsParam
::
Param
include
RailsParam
::
Param
def
params
;
end
def
params
;
end
end
end
describe
RailsParam
::
Param
do
describe
RailsParam
::
Param
do
...
@@ -18,7 +19,7 @@ describe RailsParam::Param do
...
@@ -18,7 +19,7 @@ describe RailsParam::Param do
describe
"transform"
do
describe
"transform"
do
context
"with a method"
do
context
"with a method"
do
it
"transforms the value"
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
controller
.
param!
:word
,
String
,
transform: :upcase
expect
(
controller
.
params
[
"word"
]).
to
eql
(
"FOO"
)
expect
(
controller
.
params
[
"word"
]).
to
eql
(
"FOO"
)
end
end
...
@@ -26,7 +27,7 @@ describe RailsParam::Param do
...
@@ -26,7 +27,7 @@ describe RailsParam::Param do
context
"with a block"
do
context
"with a block"
do
it
"transforms the value"
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
}
controller
.
param!
:word
,
String
,
transform:
lambda
{
|
n
|
n
.
downcase
}
expect
(
controller
.
params
[
"word"
]).
to
eql
(
"foo"
)
expect
(
controller
.
params
[
"word"
]).
to
eql
(
"foo"
)
end
end
...
@@ -36,7 +37,7 @@ describe RailsParam::Param do
...
@@ -36,7 +37,7 @@ describe RailsParam::Param do
describe
"default"
do
describe
"default"
do
context
"with a value"
do
context
"with a value"
do
it
"defaults to the 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"
controller
.
param!
:word
,
String
,
default:
"foo"
expect
(
controller
.
params
[
"word"
]).
to
eql
(
"foo"
)
expect
(
controller
.
params
[
"word"
]).
to
eql
(
"foo"
)
end
end
...
@@ -44,7 +45,7 @@ describe RailsParam::Param do
...
@@ -44,7 +45,7 @@ describe RailsParam::Param do
context
"with a block"
do
context
"with a block"
do
it
"defaults to the block value"
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"
}
controller
.
param!
:word
,
String
,
default:
lambda
{
"foo"
}
expect
(
controller
.
params
[
"word"
]).
to
eql
(
"foo"
)
expect
(
controller
.
params
[
"word"
]).
to
eql
(
"foo"
)
end
end
...
@@ -53,68 +54,68 @@ describe RailsParam::Param do
...
@@ -53,68 +54,68 @@ describe RailsParam::Param do
describe
"coerce"
do
describe
"coerce"
do
it
"converts to String"
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
controller
.
param!
:foo
,
String
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
"bar"
)
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
"bar"
)
end
end
it
"converts to Integer"
do
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
controller
.
param!
:foo
,
Integer
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
42
)
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
42
)
end
end
it
"converts to Float"
do
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
controller
.
param!
:foo
,
Float
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
42.22
)
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
42.22
)
end
end
it
"converts to Array"
do
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
controller
.
param!
:foo
,
Array
expect
(
controller
.
params
[
"foo"
]).
to
eql
([
"2"
,
"3"
,
"4"
,
"5"
])
expect
(
controller
.
params
[
"foo"
]).
to
eql
([
"2"
,
"3"
,
"4"
,
"5"
])
end
end
it
"converts to Hash"
do
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
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
end
it
"converts to Date"
do
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
controller
.
param!
:foo
,
Date
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
Date
.
parse
(
"1984-01-10"
))
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
Date
.
parse
(
"1984-01-10"
))
end
end
it
"converts to Time"
do
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
controller
.
param!
:foo
,
Time
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
Time
.
parse
(
"2014-08-07T12:25:00.000+02:00"
))
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
Time
.
parse
(
"2014-08-07T12:25:00.000+02:00"
))
end
end
it
"converts to DateTime"
do
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
controller
.
param!
:foo
,
DateTime
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
DateTime
.
parse
(
"2014-08-07T12:25:00.000+02:00"
))
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
DateTime
.
parse
(
"2014-08-07T12:25:00.000+02:00"
))
end
end
describe
"BigDecimals"
do
describe
"BigDecimals"
do
it
"converts to BigDecimal using default precision"
do
it
"converts to BigDecimal using default precision"
do
allow
(
controller
).
to
receive
(
:params
).
and_return
({
"foo"
=>
12345.67890123456
})
allow
(
controller
).
to
receive
(
:params
).
and_return
({
"foo"
=>
12345.67890123456
})
controller
.
param!
:foo
,
BigDecimal
controller
.
param!
:foo
,
BigDecimal
expect
(
controller
.
params
[
"foo"
]).
to
eql
12345.678901235
expect
(
controller
.
params
[
"foo"
]).
to
eql
12345.678901235
end
end
it
"converts to BigDecimal using precision option"
do
it
"converts to BigDecimal using precision option"
do
allow
(
controller
).
to
receive
(
:params
).
and_return
({
"foo"
=>
12345.6789
})
allow
(
controller
).
to
receive
(
:params
).
and_return
({
"foo"
=>
12345.6789
})
controller
.
param!
:foo
,
BigDecimal
,
precision:
6
controller
.
param!
:foo
,
BigDecimal
,
precision:
6
expect
(
controller
.
params
[
"foo"
]).
to
eql
12345.7
expect
(
controller
.
params
[
"foo"
]).
to
eql
12345.7
end
end
it
"converts formatted currency string to big decimal"
do
it
"converts formatted currency string to big decimal"
do
allow
(
controller
).
to
receive
(
:params
).
and_return
({
"foo"
=>
"$100,000"
})
allow
(
controller
).
to
receive
(
:params
).
and_return
({
"foo"
=>
"$100,000"
})
controller
.
param!
:foo
,
BigDecimal
controller
.
param!
:foo
,
BigDecimal
expect
(
controller
.
params
[
"foo"
]).
to
eql
100000.0
expect
(
controller
.
params
[
"foo"
]).
to
eql
100000.0
end
end
...
@@ -123,161 +124,173 @@ describe RailsParam::Param do
...
@@ -123,161 +124,173 @@ describe RailsParam::Param do
describe
"booleans"
do
describe
"booleans"
do
it
"converts 1/0"
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
controller
.
param!
:foo
,
TrueClass
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
true
)
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
controller
.
param!
:foo
,
TrueClass
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
false
)
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
false
)
end
end
it
"converts true/false"
do
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
controller
.
param!
:foo
,
TrueClass
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
true
)
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
controller
.
param!
:foo
,
TrueClass
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
false
)
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
false
)
end
end
it
"converts t/f"
do
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
controller
.
param!
:foo
,
TrueClass
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
true
)
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
controller
.
param!
:foo
,
TrueClass
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
false
)
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
false
)
end
end
it
"converts yes/no"
do
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
controller
.
param!
:foo
,
TrueClass
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
true
)
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
controller
.
param!
:foo
,
TrueClass
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
false
)
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
false
)
end
end
it
"converts y/n"
do
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
controller
.
param!
:foo
,
TrueClass
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
true
)
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
controller
.
param!
:foo
,
TrueClass
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
false
)
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
false
)
end
end
end
end
it
"raises InvalidParameterError if the value is invalid"
do
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
)
expect
{
controller
.
param!
:foo
,
Date
}.
to
raise_error
(
RailsParam
::
Param
::
InvalidParameterError
)
end
end
end
end
describe
'validating nested hash'
do
it
'typecasts nested attribtues'
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
end
describe
"validation"
do
describe
"validation"
do
describe
"required parameter"
do
describe
"required parameter"
do
it
"succeeds"
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
expect
{
controller
.
param!
:price
,
Integer
,
required:
true
}.
to_not
raise_error
end
end
it
"raises"
do
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
)
expect
{
controller
.
param!
:price
,
Integer
,
required:
true
}.
to
raise_error
(
RailsParam
::
Param
::
InvalidParameterError
)
end
end
end
end
describe
"blank parameter"
do
describe
"blank parameter"
do
it
"succeeds"
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
expect
{
controller
.
param!
:price
,
String
,
blank:
false
}.
to_not
raise_error
end
end
it
"raises"
do
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
)
expect
{
controller
.
param!
:price
,
String
,
blank:
false
}.
to
raise_error
(
RailsParam
::
Param
::
InvalidParameterError
)
end
end
end
end
describe
"format parameter"
do
describe
"format parameter"
do
it
"succeeds"
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
expect
{
controller
.
param!
:price
,
String
,
format:
/[0-9]+\$/
}.
to_not
raise_error
end
end
it
"raises"
do
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
)
expect
{
controller
.
param!
:price
,
String
,
format:
/[0-9]+\$/
}.
to
raise_error
(
RailsParam
::
Param
::
InvalidParameterError
)
end
end
end
end
describe
"is parameter"
do
describe
"is parameter"
do
it
"succeeds"
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
expect
{
controller
.
param!
:price
,
String
,
is:
"50"
}.
to_not
raise_error
end
end
it
"raises"
do
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
)
expect
{
controller
.
param!
:price
,
String
,
is:
"50"
}.
to
raise_error
(
RailsParam
::
Param
::
InvalidParameterError
)
end
end
end
end
describe
"min parameter"
do
describe
"min parameter"
do
it
"succeeds"
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
expect
{
controller
.
param!
:price
,
Integer
,
min:
50
}.
to_not
raise_error
end
end
it
"raises"
do
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
)
expect
{
controller
.
param!
:price
,
Integer
,
min:
51
}.
to
raise_error
(
RailsParam
::
Param
::
InvalidParameterError
)
end
end
end
end
describe
"max parameter"
do
describe
"max parameter"
do
it
"succeeds"
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
expect
{
controller
.
param!
:price
,
Integer
,
max:
50
}.
to_not
raise_error
end
end
it
"raises"
do
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
)
expect
{
controller
.
param!
:price
,
Integer
,
max:
49
}.
to
raise_error
(
RailsParam
::
Param
::
InvalidParameterError
)
end
end
end
end
describe
"min_length parameter"
do
describe
"min_length parameter"
do
it
"succeeds"
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
expect
{
controller
.
param!
:word
,
String
,
min_length:
3
}.
to_not
raise_error
end
end
it
"raises"
do
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
)
expect
{
controller
.
param!
:word
,
String
,
min_length:
4
}.
to
raise_error
(
RailsParam
::
Param
::
InvalidParameterError
)
end
end
end
end
describe
"max_length parameter"
do
describe
"max_length parameter"
do
it
"succeeds"
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
expect
{
controller
.
param!
:word
,
String
,
max_length:
3
}.
to_not
raise_error
end
end
it
"raises"
do
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
)
expect
{
controller
.
param!
:word
,
String
,
max_length:
2
}.
to
raise_error
(
RailsParam
::
Param
::
InvalidParameterError
)
end
end
end
end
describe
"in, within, range parameters"
do
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
it
"succeeds in the range"
do
controller
.
param!
:price
,
Integer
,
in:
1
..
100
controller
.
param!
:price
,
Integer
,
in:
1
..
100
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment