param_checker 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +23 -19
- data/lib/param_checker.rb +41 -33
- data/lib/param_checker/version.rb +1 -1
- data/spec/lib/param_checker_spec.rb +55 -37
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -22,41 +22,45 @@ Include the ParamChecker module where ever you like. I usually put it into my Ra
|
|
22
22
|
include ParamChecker
|
23
23
|
end
|
24
24
|
|
25
|
-
You can then simply call for example <tt>check_string(
|
25
|
+
You can then simply call for example <tt>check_string(params[:name], "Mia", :allowed => ["foo", "bar"])</tt> in every controller.
|
26
26
|
|
27
27
|
Instead of including the module you could also call all functions of the module directly, like
|
28
28
|
|
29
|
-
ParamChecker.check_string(
|
29
|
+
ParamChecker.check_string(params[:name], "Mia", ["foo", "bar"])
|
30
30
|
|
31
31
|
There are currently 5 supported functions:
|
32
32
|
|
33
|
-
check_integer(param, default,
|
34
|
-
check_float(param, default,
|
35
|
-
check_string(param, default,
|
36
|
-
check_symbol(param, default,
|
37
|
-
check_boolean(param, default)
|
33
|
+
check_integer(param, default, options)
|
34
|
+
check_float(param, default, options)
|
35
|
+
check_string(param, default, options)
|
36
|
+
check_symbol(param, default, options)
|
37
|
+
check_boolean(param, default, options)
|
38
38
|
|
39
|
-
* +param+
|
40
|
-
* +default+
|
41
|
-
* +
|
42
|
-
* +
|
43
|
-
* +
|
39
|
+
* +param+ is the string parameter to check.
|
40
|
+
* +default+ is the value that will be returned when +param+ does not pass the check.
|
41
|
+
* +options+ are function specific options to check +param+ against:
|
42
|
+
* +min+, +max+ in +check_integer+ and +check_float+ are the minimum and maximum allowed values of param. (If not provided then no range is checked at all.)
|
43
|
+
* +allowed+ in +check_string+ and +check_symbol+ represent the allowed values of +param+. It can be either a regular expression, a string (resp. a symbol for +check_symbol+), or an array of strings (resp. an array of symbols for +check_symbol+).
|
44
|
+
* +true+ and +false+ represent the allowed string values for the true and false booleans. By default is :true => ["1", "true"] and :false => ["0", "false"]
|
44
45
|
|
45
|
-
All functions return the
|
46
|
+
All functions return the casted value (check_integer returns an integer, check_symbol returns a symbol, and so on).
|
46
47
|
|
47
48
|
== Examples
|
48
49
|
|
49
|
-
Below are some simple examples how
|
50
|
+
Below are some simple examples how to use those functions.
|
50
51
|
|
51
|
-
#
|
52
|
-
page = check_integer(params[:
|
52
|
+
# Check if per_page parameter is a valid integer representation, ensure that it is bigger than 1 and smaller than 100 and return its integer value. Otherwise return 10.
|
53
|
+
page = check_integer(params[:per_page], 10, :min => 1, :max => 100)
|
53
54
|
|
54
|
-
#
|
55
|
-
field = check_string(params[:field], "name", ["name", "address"])
|
55
|
+
# If field parameter is equal to "name" or "address" then return it, otherwise return "name".
|
56
|
+
field = check_string(params[:field], "name", :allowed => ["name", "address"])
|
56
57
|
|
57
|
-
# Return the boolean if params[:accepted] is a valid boolean representation
|
58
|
+
# Return the boolean if params[:accepted] is a valid boolean representation and the default false otherwise.
|
58
59
|
accepted = check_boolean(params[:accepted], false)
|
59
60
|
|
61
|
+
# Have custom boolean string representation values.
|
62
|
+
accepted = check_boolean(params[:accepted], false, :true => ["yep", "yes"], :false => ["nope", "no"])
|
63
|
+
|
60
64
|
== Testing
|
61
65
|
|
62
66
|
ParamChecker uses RSpec for testing and has a rake task for executing the provided specs
|
data/lib/param_checker.rb
CHANGED
@@ -3,15 +3,16 @@ module ParamChecker
|
|
3
3
|
module_function
|
4
4
|
|
5
5
|
# Check a parameter string if it is a valid integer and return its integer value.
|
6
|
-
# +param+: the string parameter to check
|
7
|
-
# +default+: the default integer to return if the check fails
|
8
|
-
# +
|
9
|
-
# +
|
10
|
-
|
11
|
-
|
12
|
-
|
6
|
+
# * +param+: the string parameter to check
|
7
|
+
# * +default+: the default integer to return if the check fails
|
8
|
+
# * +options+: a hash of options:
|
9
|
+
# * +min+: the minimum integer value allowed
|
10
|
+
# * +max+: the maximum integer value allowed
|
11
|
+
def check_integer(param, default, options = {})
|
12
|
+
min = (options[:min] ? param.to_i >= options[:min] : true)
|
13
|
+
max = (options[:max] ? param.to_i <= options[:max] : true)
|
13
14
|
|
14
|
-
if (param && param.strip =~ /^-?[0-9]+$/ &&
|
15
|
+
if (param && param.strip =~ /^-?[0-9]+$/ && min && max)
|
15
16
|
param.to_i
|
16
17
|
else
|
17
18
|
default
|
@@ -19,15 +20,16 @@ module ParamChecker
|
|
19
20
|
end
|
20
21
|
|
21
22
|
# Check a parameter string if it is a valid float and return its float value.
|
22
|
-
# +param+: the string parameter to check
|
23
|
-
# +default+: the default float to return if the check fails
|
24
|
-
# +
|
25
|
-
# +
|
26
|
-
|
27
|
-
|
28
|
-
|
23
|
+
# * +param+: the string parameter to check
|
24
|
+
# * +default+: the default float to return if the check fails
|
25
|
+
# * +options+: a hash of options:
|
26
|
+
# * +min+: the minimum float value allowed (optional)
|
27
|
+
# * +max+: the maximum float value allowed (optional)
|
28
|
+
def check_float(param, default, options = {})
|
29
|
+
min = (options[:min] ? param.to_f >= options[:min] : true)
|
30
|
+
max = (options[:max] ? param.to_f <= options[:max] : true)
|
29
31
|
|
30
|
-
if (param && param.strip =~ /^-?[0-9]+(\.[0-9]+)?$/ &&
|
32
|
+
if (param && param.strip =~ /^-?[0-9]+(\.[0-9]+)?$/ && min && max)
|
31
33
|
param.to_f
|
32
34
|
else
|
33
35
|
default
|
@@ -35,11 +37,12 @@ module ParamChecker
|
|
35
37
|
end
|
36
38
|
|
37
39
|
# Check a parameter string if it is a valid sting and return its string value.
|
38
|
-
# +param+: the string parameter to check
|
39
|
-
# +default+: the default string to return if the check fails
|
40
|
-
# +
|
41
|
-
# a regular expression, a string or an array of strings
|
42
|
-
def check_string(param, default,
|
40
|
+
# * +param+: the string parameter to check
|
41
|
+
# * +default+: the default string to return if the check fails
|
42
|
+
# * +options+: a hash of options:
|
43
|
+
# * +allowed+: the allowed string values to check +param+ against; could be a regular expression, a string or an array of strings
|
44
|
+
def check_string(param, default, options = {})
|
45
|
+
allowed = options[:allowed]
|
43
46
|
if (param && allowed.class == Regexp && param =~ allowed)
|
44
47
|
param
|
45
48
|
elsif (param && allowed.class == Array && allowed.include?(param))
|
@@ -51,12 +54,13 @@ module ParamChecker
|
|
51
54
|
end
|
52
55
|
end
|
53
56
|
|
54
|
-
# Check a parameter string if it is a valid
|
55
|
-
# +param+: the string parameter to check
|
56
|
-
# +default+: the default symbol to return if the check fails
|
57
|
-
# +
|
58
|
-
# a regular expression, a
|
59
|
-
def check_symbol(param, default,
|
57
|
+
# Check a parameter string if it is a valid symbol and return its symbol value.
|
58
|
+
# * +param+: the string parameter to check
|
59
|
+
# * +default+: the default symbol to return if the check fails
|
60
|
+
# * +options+: a hash of options:
|
61
|
+
# * +allowed+: the allowed symbol values to check +param+ against; could be a regular expression, a symbol or an array of symbols.
|
62
|
+
def check_symbol(param, default, options = {})
|
63
|
+
allowed = options[:allowed]
|
60
64
|
if (param && !param.empty? && allowed.class == Regexp && param =~ allowed)
|
61
65
|
param.to_sym
|
62
66
|
elsif (param && !param.empty? && allowed.class == Array && allowed.map { |a| a.to_sym }.include?(param.to_sym))
|
@@ -69,13 +73,17 @@ module ParamChecker
|
|
69
73
|
end
|
70
74
|
|
71
75
|
# Check a parameter string if it represents a valid boolean and return its boolean value.
|
72
|
-
#
|
73
|
-
# +
|
74
|
-
# +
|
75
|
-
|
76
|
-
|
76
|
+
# * +param+: the string parameter to check
|
77
|
+
# * +default+: the default boolean to return if the check fails
|
78
|
+
# * +options+: a hash of options:
|
79
|
+
# * +true+: an array of string representations of true (by default "1" and "true")
|
80
|
+
# * +false+: an array of string representations of false (by default "0" and "false")
|
81
|
+
def check_boolean(param, default, options = {})
|
82
|
+
true_values = (options[:true] ? options[:true] : ["1", "true"])
|
83
|
+
false_values = (options[:false] ? options[:false] : ["0", "false"])
|
84
|
+
if (true_values.include?(param))
|
77
85
|
true
|
78
|
-
elsif (param
|
86
|
+
elsif (false_values.include?(param))
|
79
87
|
false
|
80
88
|
else
|
81
89
|
default
|
@@ -8,9 +8,9 @@ describe "ParamChecker" do
|
|
8
8
|
describe "check integer" do
|
9
9
|
it "should pass" do
|
10
10
|
@model.check_integer("5", 99).should == 5
|
11
|
-
@model.check_integer("5", 99, 4).should == 5
|
12
|
-
@model.check_integer("5", 99,
|
13
|
-
@model.check_integer("5", 99, 4, 6).should == 5
|
11
|
+
@model.check_integer("5", 99, :min => 4).should == 5
|
12
|
+
@model.check_integer("5", 99, :max => 6).should == 5
|
13
|
+
@model.check_integer("5", 99, :min => 4, :max => 6).should == 5
|
14
14
|
@model.check_integer("-5", 99).should == -5
|
15
15
|
@model.check_integer(" 5 ", 99).should == 5
|
16
16
|
end
|
@@ -19,10 +19,10 @@ describe "ParamChecker" do
|
|
19
19
|
@model.check_integer(nil, 99).should == 99
|
20
20
|
@model.check_integer("", 99).should == 99
|
21
21
|
@model.check_integer("5abc", 99).should == 99
|
22
|
-
@model.check_integer("5", 99, 6).should == 99
|
23
|
-
@model.check_integer("5", 99,
|
24
|
-
@model.check_integer("5", 99, 1, 4).should == 99
|
25
|
-
@model.check_integer("5", 99, 10, 1).should == 99
|
22
|
+
@model.check_integer("5", 99, :min => 6).should == 99
|
23
|
+
@model.check_integer("5", 99, :max => 4).should == 99
|
24
|
+
@model.check_integer("5", 99, :min => 1, :max => 4).should == 99
|
25
|
+
@model.check_integer("5", 99, :min => 10, :max => 1).should == 99
|
26
26
|
@model.check_integer("", nil).should == nil
|
27
27
|
end
|
28
28
|
end
|
@@ -30,59 +30,63 @@ describe "ParamChecker" do
|
|
30
30
|
describe "check float" do
|
31
31
|
it "should pass" do
|
32
32
|
@model.check_float("5.1", 99.2).should == 5.1
|
33
|
-
@model.check_float("5.1", 99.2, 4.3).should == 5.1
|
34
|
-
@model.check_float("5.1", 99.2,
|
35
|
-
@model.check_float("5.1", 99.2, 4.3, 6.4).should == 5.1
|
33
|
+
@model.check_float("5.1", 99.2, :min => 4.3).should == 5.1
|
34
|
+
@model.check_float("5.1", 99.2, :max => 6.4).should == 5.1
|
35
|
+
@model.check_float("5.1", 99.2, :min => 4.3, :max => 6.4).should == 5.1
|
36
36
|
@model.check_float("-5.1", 99.2).should == -5.1
|
37
37
|
@model.check_float(" 5.1 ", 99.2).should == 5.1
|
38
|
+
@model.check_float("5.1", 99.2, :min => 5.1, :max => 5.1).should == 5.1
|
38
39
|
end
|
39
40
|
|
40
41
|
it "should fail" do
|
41
42
|
@model.check_float(nil, 99.2).should == 99.2
|
42
43
|
@model.check_float("", 99.2).should == 99.2
|
43
44
|
@model.check_float("5abc", 99.2).should == 99.2
|
44
|
-
@model.check_float("5", 99.2, 6.4).should == 99.2
|
45
|
-
@model.check_float("5", 99.2,
|
46
|
-
@model.check_float("5", 99.2, 1, 4.3).should == 99.2
|
47
|
-
@model.check_float("5", 99.2, 10.5, 1.6).should == 99.2
|
45
|
+
@model.check_float("5", 99.2, :min => 6.4).should == 99.2
|
46
|
+
@model.check_float("5", 99.2, :max => 4.3).should == 99.2
|
47
|
+
@model.check_float("5", 99.2, :min => 1, :max => 4.3).should == 99.2
|
48
|
+
@model.check_float("5", 99.2, :min => 10.5, :max => 1.6).should == 99.2
|
48
49
|
@model.check_float("", nil).should == nil
|
49
50
|
end
|
50
51
|
end
|
51
52
|
|
52
53
|
describe "check string" do
|
53
54
|
it "should pass" do
|
54
|
-
@model.check_string("lorem", "dolor", /.*ore.*/).should == "lorem"
|
55
|
-
@model.check_string("lorem", "dolor", ["lorem", "ipsum"]).should == "lorem"
|
56
|
-
@model.check_string("lorem", "dolor", "lorem").should == "lorem"
|
57
|
-
@model.check_string("", "dolor", /.*/).should == ""
|
58
|
-
@model.check_string("", "dolor", "").should == ""
|
55
|
+
@model.check_string("lorem", "dolor", :allowed => /.*ore.*/).should == "lorem"
|
56
|
+
@model.check_string("lorem", "dolor", :allowed => ["lorem", "ipsum"]).should == "lorem"
|
57
|
+
@model.check_string("lorem", "dolor", :allowed => "lorem").should == "lorem"
|
58
|
+
@model.check_string("", "dolor", :allowed => /.*/).should == ""
|
59
|
+
@model.check_string("", "dolor", :allowed => "").should == ""
|
59
60
|
end
|
60
61
|
|
61
62
|
it "should fail" do
|
62
|
-
@model.check_string(
|
63
|
-
@model.check_string(
|
64
|
-
@model.check_string("lorem", "dolor",
|
65
|
-
@model.check_string("lorem", "dolor", "ipsum").should == "dolor"
|
66
|
-
@model.check_string("lorem",
|
63
|
+
@model.check_string("lorem", "dolor").should == "dolor"
|
64
|
+
@model.check_string(nil, "dolor", :allowed => /.*ore.*/).should == "dolor"
|
65
|
+
@model.check_string("lorem", "dolor", :allowed => /.*ips.*/).should == "dolor"
|
66
|
+
@model.check_string("lorem", "dolor", :allowed => ["patre", "ipsum"]).should == "dolor"
|
67
|
+
@model.check_string("lorem", "dolor", :allowed => "ipsum").should == "dolor"
|
68
|
+
@model.check_string("lorem", nil, :allowed => /.*ips.*/).should == nil
|
67
69
|
end
|
68
70
|
end
|
69
71
|
|
70
72
|
describe "check symbol" do
|
71
73
|
it "should pass" do
|
72
|
-
@model.check_symbol("lorem", :dolor
|
73
|
-
@model.check_symbol("lorem", :dolor, ["lorem", :ipsum]).should == :lorem
|
74
|
-
@model.check_symbol("lorem", :dolor, :lorem).should == :lorem
|
74
|
+
@model.check_symbol("lorem", :dolor,:allowed => /.*ore.*/).should == :lorem
|
75
|
+
@model.check_symbol("lorem", :dolor, :allowed => ["lorem", :ipsum]).should == :lorem
|
76
|
+
@model.check_symbol("lorem", :dolor, :allowed => :lorem).should == :lorem
|
75
77
|
end
|
76
78
|
|
77
79
|
it "should fail" do
|
78
|
-
@model.
|
79
|
-
@model.check_symbol(
|
80
|
-
@model.check_symbol("lorem", :dolor,
|
81
|
-
@model.check_symbol("lorem", :dolor, "ipsum").should == :dolor
|
82
|
-
@model.check_symbol("lorem", :dolor, "ipsum").should == :dolor
|
83
|
-
@model.check_symbol("", :dolor,
|
84
|
-
@model.check_symbol("", :dolor,
|
85
|
-
@model.check_symbol("
|
80
|
+
@model.check_string("lorem", :dolor).should == :dolor
|
81
|
+
@model.check_symbol(nil, :dolor, :allowed => /.*ore.*/).should == :dolor
|
82
|
+
@model.check_symbol("lorem", :dolor, :allowed => /.*ips.*/).should == :dolor
|
83
|
+
@model.check_symbol("lorem", :dolor, :allowed => ["patre", "ipsum"]).should == :dolor
|
84
|
+
@model.check_symbol("lorem", :dolor, :allowed => "ipsum").should == :dolor
|
85
|
+
@model.check_symbol("lorem", :dolor, :allowed => "ipsum").should == :dolor
|
86
|
+
@model.check_symbol("", :dolor, :allowed => /.*/).should == :dolor
|
87
|
+
@model.check_symbol("", :dolor, :allowed => "").should == :dolor
|
88
|
+
@model.check_symbol("lorem", nil, :allowed => /.*ips.*/).should == nil
|
89
|
+
@model.check_symbol("", :dolor, :allowed => "").should == :dolor
|
86
90
|
end
|
87
91
|
end
|
88
92
|
|
@@ -101,13 +105,27 @@ describe "ParamChecker" do
|
|
101
105
|
@model.check_boolean("abc", true).should == true
|
102
106
|
@model.check_boolean("abc", nil).should == nil
|
103
107
|
end
|
108
|
+
|
109
|
+
it "should pass with custom true and false values" do
|
110
|
+
@model.check_boolean("yes", false, :true => "yes").should == true
|
111
|
+
@model.check_boolean("no", true, :false => "no").should == false
|
112
|
+
@model.check_boolean("ja", false, :true => ["yes", "ja"]).should == true
|
113
|
+
@model.check_boolean("nein", true, :false => ["no", "nein"]).should == false
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should fail with custom true and false values" do
|
117
|
+
@model.check_boolean("yep", false, :true => "yes").should == false
|
118
|
+
@model.check_boolean("nope", true, :false => "no").should == true
|
119
|
+
@model.check_boolean("yep", false, :true => ["yes", "ja"]).should == false
|
120
|
+
@model.check_boolean("nope", true, :false => ["no", "nein"]).should == true
|
121
|
+
end
|
104
122
|
end
|
105
123
|
|
106
124
|
it "can be called as module functions" do
|
107
125
|
ParamChecker.check_integer("5", 99).should == 5
|
108
126
|
ParamChecker.check_float("5.1", 99.2).should == 5.1
|
109
|
-
ParamChecker.check_string("lorem", "dolor", /.*ore.*/).should == "lorem"
|
110
|
-
ParamChecker.check_symbol("lorem", :dolor, /.*ore.*/).should == :lorem
|
127
|
+
ParamChecker.check_string("lorem", "dolor", :allowed => /.*ore.*/).should == "lorem"
|
128
|
+
ParamChecker.check_symbol("lorem", :dolor, :allowed => /.*ore.*/).should == :lorem
|
111
129
|
ParamChecker.check_boolean("1", false).should == true
|
112
130
|
end
|
113
131
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: param_checker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kai Schlamp
|