request_validator 0.0.1 → 0.0.2
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/lib/request_validator.rb +128 -50
- 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: 428230278c28e33433973f91b2d8d41affde77a1
|
4
|
+
data.tar.gz: 6d32aee0f0ccf38a0ed83b499e412585f569e55f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c8829d7b30eeda884b342e5a2d0cb98c2e41ec3a52ef94ac0c49bea1e49542fd446e33a4baa11ced304ff1c73b2d524d46f2b7381e7b1b6fb75c1113386a2fa
|
7
|
+
data.tar.gz: feba31be55f64c9b41902ac976d6839baa813096f8477f6192adb9fed6056b5e335ab5ed9f1c122ce158bd3b9ba7f72b183f98b306dbf4720c3fe9195eb89d9d
|
data/lib/request_validator.rb
CHANGED
@@ -1,20 +1,33 @@
|
|
1
|
-
|
1
|
+
# This Gem help validate request parameters before
|
2
|
+
# they hit the models. Why do we need to validate
|
3
|
+
# in ActiveModel ?
|
4
|
+
#
|
5
|
+
# Author:: Williams Isaac (mailto:williamscalg@gmail.com)
|
6
|
+
# License:: MIT
|
2
7
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
class Configuration
|
8
|
-
attr_accessor :skip_main_object_empty, :error_mode
|
9
|
-
|
10
|
-
def initialize
|
11
|
-
@skip_main_object_empty = false
|
12
|
-
end
|
8
|
+
# This Validator class holds all the method for our operation
|
9
|
+
|
10
|
+
class Validator
|
11
|
+
require 'config/config'
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
# Initialize the Validator.new and sets injected_params to
|
14
|
+
# to be remembered thoughout the cause of operation
|
15
|
+
#
|
16
|
+
# @param params [Hash] value to be remembered
|
17
|
+
# @return [Object] instance of class Validator
|
18
|
+
#
|
19
|
+
|
20
|
+
def initialize(params = {})
|
21
|
+
@response = {
|
22
|
+
has_errors: false,
|
23
|
+
errors: []
|
24
|
+
}
|
25
|
+
@injected_params = params
|
17
26
|
end
|
27
|
+
|
28
|
+
# Returns new instance of configuration
|
29
|
+
# @return [Object] instance of class Configuration
|
30
|
+
#
|
18
31
|
|
19
32
|
def self.configure
|
20
33
|
@config ||= Configuration.new
|
@@ -22,20 +35,76 @@ class Validator
|
|
22
35
|
@config
|
23
36
|
end
|
24
37
|
|
25
|
-
def
|
26
|
-
@config || configure
|
38
|
+
def config
|
39
|
+
@config || Validator.configure
|
27
40
|
end
|
28
41
|
|
29
|
-
def
|
42
|
+
def validate(object, checks)
|
30
43
|
raise ArgumentError, 'Object cannnot be empty' unless !object.empty? || config.skip_main_object_empty
|
31
44
|
checks.each_pair do |key, againsts|
|
32
|
-
res = @@functions_hash[againsts]
|
45
|
+
res = @@functions_hash[againsts]
|
46
|
+
if res.nil?
|
47
|
+
next if config.error_mode == 'surpressed'
|
48
|
+
raise ArgumentError, "Invalid identifier - #{againsts} - Corresponding method not found"
|
49
|
+
end
|
50
|
+
res = res.call(object[key], key)
|
33
51
|
if !res[:valid]
|
34
|
-
|
52
|
+
p res[:error]
|
53
|
+
@response[:errors] << res[:error]
|
35
54
|
end
|
36
55
|
end
|
37
|
-
|
38
|
-
|
56
|
+
@response[:has_errors] = @response[:errors].length > 0 ? true : false
|
57
|
+
@response
|
58
|
+
end
|
59
|
+
|
60
|
+
def check(key)
|
61
|
+
@value = @injected_params[key]
|
62
|
+
@key = key
|
63
|
+
self
|
64
|
+
end
|
65
|
+
|
66
|
+
def withMessage(message)
|
67
|
+
@custom_error_message = message
|
68
|
+
end
|
69
|
+
|
70
|
+
def notEmpty
|
71
|
+
@response[:errors] << message_composer(@key, "cannot be empty") if @value.length < 1
|
72
|
+
self
|
73
|
+
end
|
74
|
+
|
75
|
+
def isEmail
|
76
|
+
check = (@value =~ /\A[a-zA-Z0-9.!\#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*\z/).nil?
|
77
|
+
@response[:errors] << message_composer(@key, "is not a valid email address") if check
|
78
|
+
self
|
79
|
+
end
|
80
|
+
|
81
|
+
def isString
|
82
|
+
check = !(@value =~ /[^a-zA-Z0-9]/).nil?
|
83
|
+
@response[:errors] << message_composer(@key, "is not a valid string") if check
|
84
|
+
self
|
85
|
+
end
|
86
|
+
|
87
|
+
def isIn(arr)
|
88
|
+
check = arr.include? @value
|
89
|
+
@response[:errors] << message_composer(@key, "is not a valid option") if !check
|
90
|
+
self
|
91
|
+
end
|
92
|
+
|
93
|
+
def isArray
|
94
|
+
check = @value.kind_of?(Array)
|
95
|
+
@response[:errors] << message_composer(@key, "must be an array") if !check
|
96
|
+
self
|
97
|
+
end
|
98
|
+
|
99
|
+
def validUrl
|
100
|
+
check = (@value =~ /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix).nil?
|
101
|
+
@response[:errors] << message_composer(@key, "is not a valid url") if check
|
102
|
+
self
|
103
|
+
end
|
104
|
+
|
105
|
+
def result
|
106
|
+
@response[:has_errors] = @response[:errors].length > 0 ? true : false
|
107
|
+
@response
|
39
108
|
end
|
40
109
|
|
41
110
|
# Structure
|
@@ -47,38 +116,47 @@ class Validator
|
|
47
116
|
# checked: String
|
48
117
|
# }
|
49
118
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
119
|
+
private
|
120
|
+
check_string = lambda { | value, key |
|
121
|
+
check = (value =~ /[^a-zA-Z0-9]/).nil?
|
122
|
+
return {
|
123
|
+
valid: check,
|
124
|
+
error: check ? nil : "#{key} is not a valid string",
|
125
|
+
checked: value
|
126
|
+
}
|
56
127
|
}
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
128
|
+
|
129
|
+
check_email = lambda { | value, key|
|
130
|
+
# http://www.rubydoc.info/stdlib/uri/URI/MailTo
|
131
|
+
check = (value =~ /\A[a-zA-Z0-9.!\#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*\z/)
|
132
|
+
return {
|
133
|
+
valid: check,
|
134
|
+
error: check ? nil : "#{key} is not a valid email address",
|
135
|
+
checked: value
|
136
|
+
}
|
137
|
+
}
|
138
|
+
|
139
|
+
check_number = lambda { | value, key |
|
140
|
+
check = (value =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/).nil?
|
141
|
+
return {
|
142
|
+
valid: check,
|
143
|
+
error: check ? nil : "#{key} is not a valid number",
|
144
|
+
checked: value
|
145
|
+
}
|
66
146
|
}
|
67
|
-
}
|
68
147
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
error: check ? nil : "#{value} is not a valid number",
|
74
|
-
checked: value
|
148
|
+
@@functions_hash = {
|
149
|
+
'string' => check_string,
|
150
|
+
'email' => check_email,
|
151
|
+
'number' => check_number
|
75
152
|
}
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
153
|
+
|
154
|
+
def message_composer(key, default_msg)
|
155
|
+
if @custom_error_message
|
156
|
+
"#{key} #{@custom_error_message}"
|
157
|
+
else
|
158
|
+
"#{key} #{default_msg}"
|
159
|
+
end
|
160
|
+
end
|
83
161
|
|
84
162
|
end
|