param_check 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.
- checksums.yaml +4 -4
- data/README.md +14 -0
- data/config/locales/en.yml +4 -1
- data/lib/param_check/version.rb +1 -1
- data/lib/param_check.rb +56 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d98a6fa8c72c459888f417ac75fe6a2f9ff5167a
|
4
|
+
data.tar.gz: 341fa70017a064a1b5046f21d2d7b1dd3697fd2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9a92099a2944dbe2889ce787cbaacb1e65a46808967e738f8e03be5197e595ce3cb760d7ab3f79b7b2f29773be9b3b61a150903c57ef76e8c21f1186ea40671
|
7
|
+
data.tar.gz: f66096a368a3e10b300d1da425f988dbe9753fb498e6abf3cba26977b0646945540fec8305975fa7a35e1ccc39c9f1e75beb46991406cf7cb7a6f1c1b052a635
|
data/README.md
CHANGED
@@ -68,6 +68,20 @@ params[:foo] = {
|
|
68
68
|
params[:foo] = 'bar'
|
69
69
|
```
|
70
70
|
|
71
|
+
### Validating range
|
72
|
+
```ruby
|
73
|
+
def index
|
74
|
+
param! :foo, min: 1, max: 3
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
78
|
+
### Validating inclusion
|
79
|
+
```ruby
|
80
|
+
def index
|
81
|
+
param! :foo, in: ['foo', 'bar']
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
71
85
|
By default, ParamCheck throws a `ParameterError` exception. You can rescue from this exception in order to
|
72
86
|
respond with JSON for example:
|
73
87
|
|
data/config/locales/en.yml
CHANGED
@@ -1,4 +1,7 @@
|
|
1
1
|
en:
|
2
2
|
param_check:
|
3
3
|
missing_required_parameter: "Missing required parameter: %{parameter}"
|
4
|
-
invalid_parameter: "Parameter %{parameter} is invalid. Expected %{expected} got %{got}"
|
4
|
+
invalid_parameter: "Parameter %{parameter} is invalid. Expected %{expected}, got %{got}"
|
5
|
+
invalid_minimum: "Parameter %{parameter} must be at least %{min}, got %{got}"
|
6
|
+
invalid_maximum: "Parameter %{parameter} must not be more than %{max}, got %{got}"
|
7
|
+
invalid_inclusion: "Parameter %{parameter} is not in the list. Expected inclusion in %{expected}, got %{got}"
|
data/lib/param_check/version.rb
CHANGED
data/lib/param_check.rb
CHANGED
@@ -28,16 +28,26 @@ module ParamCheck
|
|
28
28
|
yield controller, index
|
29
29
|
end
|
30
30
|
|
31
|
-
def validate! name,
|
31
|
+
def validate! name, value, options
|
32
32
|
if options[:required]
|
33
|
-
validate_presence name,
|
33
|
+
validate_presence name, value
|
34
34
|
end
|
35
35
|
|
36
|
-
|
36
|
+
if options[:type].present?
|
37
|
+
validate_type name, value, options[:type]
|
38
|
+
end
|
39
|
+
|
40
|
+
if options[:min].present? || options[:max].present?
|
41
|
+
validate_range name, value, options[:min], options[:max]
|
42
|
+
end
|
43
|
+
|
44
|
+
if options[:in].present?
|
45
|
+
validate_inclusion name, value, options[:in]
|
46
|
+
end
|
37
47
|
end
|
38
48
|
|
39
|
-
def validate_presence name,
|
40
|
-
if
|
49
|
+
def validate_presence name, value
|
50
|
+
if value.nil?
|
41
51
|
raise ParameterError, I18n.t(
|
42
52
|
'param_check.missing_required_parameter',
|
43
53
|
parameter: name,
|
@@ -45,25 +55,60 @@ module ParamCheck
|
|
45
55
|
end
|
46
56
|
end
|
47
57
|
|
48
|
-
def validate_type name,
|
49
|
-
return if
|
58
|
+
def validate_type name, value, type
|
59
|
+
return if value.nil?
|
50
60
|
|
51
61
|
if type.in?([Integer, Fixnum])
|
52
|
-
is_numeric = Float(
|
62
|
+
is_numeric = Float(value) rescue nil
|
53
63
|
if ! is_numeric
|
54
64
|
raise ParameterError, I18n.t(
|
55
65
|
'param_check.invalid_parameter',
|
56
66
|
parameter: name,
|
57
67
|
expected: type,
|
58
|
-
got:
|
68
|
+
got: value.class.name
|
59
69
|
)
|
60
70
|
end
|
61
|
-
elsif !
|
71
|
+
elsif ! value.is_a? type
|
62
72
|
raise ParameterError, I18n.t(
|
63
73
|
'param_check.invalid_parameter',
|
64
74
|
parameter: name,
|
65
75
|
expected: type,
|
66
|
-
got:
|
76
|
+
got: value.class.name
|
77
|
+
)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def validate_range name, value, min = nil, max = nil
|
82
|
+
if min.present?
|
83
|
+
if value.nil? || value.to_i < min.to_i
|
84
|
+
raise ParameterError, I18n.t(
|
85
|
+
'param_check.invalid_minimum',
|
86
|
+
parameter: name,
|
87
|
+
min: min,
|
88
|
+
got: value,
|
89
|
+
)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
if max.present?
|
94
|
+
if value.nil? || value.to_i > max.to_i
|
95
|
+
raise ParameterError, I18n.t(
|
96
|
+
'param_check.invalid_maximum',
|
97
|
+
parameter: name,
|
98
|
+
max: max,
|
99
|
+
got: value,
|
100
|
+
)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def validate_inclusion name, value, inclusion = []
|
106
|
+
if ! value.in?(inclusion)
|
107
|
+
raise ParameterError, I18n.t(
|
108
|
+
'param_check.invalid_inclusion',
|
109
|
+
parameter: name,
|
110
|
+
expected: inclusion,
|
111
|
+
got: value,
|
67
112
|
)
|
68
113
|
end
|
69
114
|
end
|