param_checker 0.0.4 → 0.1.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 +10 -8
- data/lib/param_checker.rb +16 -20
- data/lib/param_checker/version.rb +1 -1
- data/spec/lib/param_checker_spec.rb +83 -58
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
= ParamChecker
|
2
2
|
|
3
|
-
ParamChecker is a small library for
|
3
|
+
ParamChecker is a small Ruby library for validating and casting string parameters. It is for example a handy way to check GET/POST +params+ in Ruby On Rails.
|
4
4
|
|
5
5
|
== Installation
|
6
6
|
|
@@ -22,9 +22,9 @@ 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 <tt>check_string(param_to_check, "my default", ["foo", "bar"])</tt> in every controller.
|
25
|
+
You can then simply call for example <tt>check_string(param_to_check, "my default", ["foo", "bar"])</tt> in every controller.
|
26
26
|
|
27
|
-
Instead of including the module you could also call all functions of the module directly
|
27
|
+
Instead of including the module you could also call all functions of the module directly, like
|
28
28
|
|
29
29
|
ParamChecker.check_string(param_to_check, "my default", ["foo", "bar"])
|
30
30
|
|
@@ -36,11 +36,13 @@ There are currently 5 supported functions:
|
|
36
36
|
check_symbol(param, default, allowed)
|
37
37
|
check_boolean(param, default)
|
38
38
|
|
39
|
-
+param+ is
|
40
|
-
|
41
|
-
+
|
42
|
-
+
|
43
|
-
|
39
|
+
* +param+ (_required_) is the string parameter to check.
|
40
|
+
* +default+ (_required_) is the value that is returned when +param+ does not pass the check.
|
41
|
+
* +min+ (_optional_), +max+ (_optional_) 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.)
|
42
|
+
* +allowed+ (_optional_) in +check_string+ and +check_symbol+ represent the allowed values of +param+. They can be either a string (resp. a symbol for +check_symbol+), a regular expression, or an array of strings (resp. symbols for +check_symbol+).
|
43
|
+
* +check_boolean+ evaluates "1" or "true" string as true and "0" or "false" string to false.
|
44
|
+
|
45
|
+
All functions return the the casted value (check_integer returns an integer, check_symbol returns a symbol, and so on).
|
44
46
|
|
45
47
|
== Examples
|
46
48
|
|
data/lib/param_checker.rb
CHANGED
@@ -8,11 +8,11 @@ module ParamChecker
|
|
8
8
|
# +min+: the minimum value allowed (optional)
|
9
9
|
# +max+: the maximum value allowed (optional)
|
10
10
|
def check_integer(param, default, min = nil, max = nil)
|
11
|
-
min_lambda = (min.nil? ? lambda { true } : lambda { param.
|
12
|
-
max_lambda = (max.nil? ? lambda { true } : lambda { param.
|
11
|
+
min_lambda = (min.nil? ? lambda { true } : lambda { param.to_i >= min })
|
12
|
+
max_lambda = (max.nil? ? lambda { true } : lambda { param.to_i <= max })
|
13
13
|
|
14
14
|
if (param && param.strip =~ /^-?[0-9]+$/ && min_lambda.call && max_lambda.call)
|
15
|
-
param.
|
15
|
+
param.to_i
|
16
16
|
else
|
17
17
|
default
|
18
18
|
end
|
@@ -24,11 +24,11 @@ module ParamChecker
|
|
24
24
|
# +min+: the minimum value allowed (optional)
|
25
25
|
# +max+: the maximum value allowed (optional)
|
26
26
|
def check_float(param, default, min = nil, max = nil)
|
27
|
-
min_lambda = (min.nil? ? lambda { true } : lambda { param.
|
28
|
-
max_lambda = (max.nil? ? lambda { true } : lambda { param.
|
27
|
+
min_lambda = (min.nil? ? lambda { true } : lambda { param.to_i >= min })
|
28
|
+
max_lambda = (max.nil? ? lambda { true } : lambda { param.to_i <= max })
|
29
29
|
|
30
30
|
if (param && param.strip =~ /^-?[0-9]+(\.[0-9]+)?$/ && min_lambda.call && max_lambda.call)
|
31
|
-
param.
|
31
|
+
param.to_f
|
32
32
|
else
|
33
33
|
default
|
34
34
|
end
|
@@ -41,11 +41,11 @@ module ParamChecker
|
|
41
41
|
# a regular expression, a string or an array of strings
|
42
42
|
def check_string(param, default, allowed)
|
43
43
|
if (param && allowed.class == Regexp && param =~ allowed)
|
44
|
-
param
|
44
|
+
param
|
45
45
|
elsif (param && allowed.class == Array && allowed.include?(param))
|
46
|
-
param
|
46
|
+
param
|
47
47
|
elsif (param && allowed.class == String && allowed == param)
|
48
|
-
param
|
48
|
+
param
|
49
49
|
else
|
50
50
|
default
|
51
51
|
end
|
@@ -57,17 +57,13 @@ module ParamChecker
|
|
57
57
|
# +allowed+: the allowed symbol value to check +param+ against; could be
|
58
58
|
# a regular expression, a string, a symbol, an array of strings or an array of symbols.
|
59
59
|
def check_symbol(param, default, allowed)
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
else
|
68
|
-
default
|
69
|
-
end
|
70
|
-
rescue
|
60
|
+
if (param && !param.empty? && allowed.class == Regexp && param =~ allowed)
|
61
|
+
param.to_sym
|
62
|
+
elsif (param && !param.empty? && allowed.class == Array && allowed.map { |a| a.to_sym }.include?(param.to_sym))
|
63
|
+
param.to_sym
|
64
|
+
elsif (param && !param.empty? && (allowed.class == String || allowed.class == Symbol) && allowed.to_sym == param.to_sym)
|
65
|
+
param.to_sym
|
66
|
+
else
|
71
67
|
default
|
72
68
|
end
|
73
69
|
end
|
@@ -5,77 +5,102 @@ describe "ParamChecker" do
|
|
5
5
|
@model = ParamCheckerModel.new
|
6
6
|
end
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
8
|
+
describe "check integer" do
|
9
|
+
it "should pass" do
|
10
|
+
@model.check_integer("5", 99).should == 5
|
11
|
+
@model.check_integer("5", 99, 4).should == 5
|
12
|
+
@model.check_integer("5", 99, nil, 6).should == 5
|
13
|
+
@model.check_integer("5", 99, 4, 6).should == 5
|
14
|
+
@model.check_integer("-5", 99).should == -5
|
15
|
+
@model.check_integer(" 5 ", 99).should == 5
|
16
|
+
end
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
18
|
+
it "should fail" do
|
19
|
+
@model.check_integer(nil, 99).should == 99
|
20
|
+
@model.check_integer("", 99).should == 99
|
21
|
+
@model.check_integer("5abc", 99).should == 99
|
22
|
+
@model.check_integer("5", 99, 6).should == 99
|
23
|
+
@model.check_integer("5", 99, nil, 4).should == 99
|
24
|
+
@model.check_integer("5", 99, 1, 4).should == 99
|
25
|
+
@model.check_integer("5", 99, 10, 1).should == 99
|
26
|
+
@model.check_integer("", nil).should == nil
|
27
|
+
end
|
23
28
|
end
|
24
29
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
30
|
+
describe "check float" do
|
31
|
+
it "should pass" do
|
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, nil, 6.4).should == 5.1
|
35
|
+
@model.check_float("5.1", 99.2, 4.3, 6.4).should == 5.1
|
36
|
+
@model.check_float("-5.1", 99.2).should == -5.1
|
37
|
+
@model.check_float(" 5.1 ", 99.2).should == 5.1
|
38
|
+
end
|
32
39
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
+
it "should fail" do
|
41
|
+
@model.check_float(nil, 99.2).should == 99.2
|
42
|
+
@model.check_float("", 99.2).should == 99.2
|
43
|
+
@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, nil, 4.3).should == 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
|
48
|
+
@model.check_float("", nil).should == nil
|
49
|
+
end
|
40
50
|
end
|
41
51
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
52
|
+
describe "check string" do
|
53
|
+
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 == ""
|
59
|
+
end
|
48
60
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
61
|
+
it "should fail" do
|
62
|
+
@model.check_string(nil, "dolor", /.*ore.*/).should == "dolor"
|
63
|
+
@model.check_string("lorem", "dolor", /.*ips.*/).should == "dolor"
|
64
|
+
@model.check_string("lorem", "dolor", ["patre", "ipsum"]).should == "dolor"
|
65
|
+
@model.check_string("lorem", "dolor", "ipsum").should == "dolor"
|
66
|
+
@model.check_string("lorem", nil, /.*ips.*/).should == nil
|
67
|
+
end
|
53
68
|
end
|
54
69
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
70
|
+
describe "check symbol" do
|
71
|
+
it "should pass" do
|
72
|
+
@model.check_symbol("lorem", :dolor, /.*ore.*/).should == :lorem
|
73
|
+
@model.check_symbol("lorem", :dolor, ["lorem", :ipsum]).should == :lorem
|
74
|
+
@model.check_symbol("lorem", :dolor, :lorem).should == :lorem
|
75
|
+
end
|
59
76
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
77
|
+
it "should fail" do
|
78
|
+
@model.check_symbol(nil, :dolor, /.*ore.*/).should == :dolor
|
79
|
+
@model.check_symbol("lorem", :dolor, /.*ips.*/).should == :dolor
|
80
|
+
@model.check_symbol("lorem", :dolor, ["patre", "ipsum"]).should == :dolor
|
81
|
+
@model.check_symbol("lorem", :dolor, "ipsum").should == :dolor
|
82
|
+
@model.check_symbol("lorem", :dolor, "ipsum").should == :dolor
|
83
|
+
@model.check_symbol("", :dolor, /.*/).should == :dolor
|
84
|
+
@model.check_symbol("", :dolor, "").should == :dolor
|
85
|
+
@model.check_symbol("lorem", nil, /.*ips.*/).should == nil
|
86
|
+
end
|
67
87
|
end
|
68
88
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
89
|
+
describe "check boolean" do
|
90
|
+
it "should pass" do
|
91
|
+
@model.check_boolean("1", false).should == true
|
92
|
+
@model.check_boolean("true", false).should == true
|
93
|
+
@model.check_boolean("0", true).should == false
|
94
|
+
@model.check_boolean("false", true).should == false
|
95
|
+
end
|
74
96
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
97
|
+
it "should fail" do
|
98
|
+
@model.check_boolean(nil, true).should == true
|
99
|
+
@model.check_boolean("3", true).should == true
|
100
|
+
@model.check_boolean("", true).should == true
|
101
|
+
@model.check_boolean("abc", true).should == true
|
102
|
+
@model.check_boolean("abc", nil).should == nil
|
103
|
+
end
|
79
104
|
end
|
80
105
|
|
81
106
|
it "can be called as module functions" do
|
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: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.4
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kai Schlamp
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-03-
|
18
|
+
date: 2011-03-21 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|