1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
ActiveRecord::Base.class_eval do
def self.validate_mac_address( attr_names, options={} )
validates_format_of( attr_names, {
:with => /^ [0-9A-F]{2} (?: [0-9A-F]{2} ){5} $/x,
:allow_blank => false,
:allow_nil => false,
}.merge!( options ) )
end
def self.validate_ip_address( attr_names, options={} )
validates_format_of( attr_names, {
:with => /^
(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)
(?:\.(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)){3}
$/x, # OPTIMIZE IPv6
:allow_blank => false,
:allow_nil => false,
}.merge!( options ) )
end
def self.validate_ip_port( attr_names, options={} )
validates_numericality_of( attr_names, {
:only_integer => true,
:greater_than => 0,
:less_than => 65536,
:allow_nil => false,
}.merge!( options ) )
end
def self.validate_netmask( attr_names, options={} )
configuration = {
:allow_nil => false,
:allow_blank => false,
:with =>
/^(
((128|192|224|240|248|252|254)\.0\.0\.0)
|(255\.(0|128|192|224|240|248|252|254)\.0\.0)
|(255\.255\.(0|128|192|224|240|248|252|254)\.0)
|(255\.255\.255\.(0|128|192|224|240|248|252|254))
)$/x
}
configuration.merge!( options )
validates_format_of( attr_names, configuration )
end
def self.validate_hostname_or_ip( attr_names, options={} )
# Validate the server. This is the "host" rule from RFC 3261
# (but the patterns for IPv4 and IPv6 addresses have been fixed here).
configuration = {
:allow_nil => false,
:allow_blank => false,
:with =>
/^
(?:
(?:
(?:
(?:
[A-Za-z0-9] |
[A-Za-z0-9] [A-Za-z0-9\-]* [A-Za-z0-9]
)
\.
)*
(?:
[A-Za-z] |
[A-Za-z] [A-Za-z0-9\-]* [A-Za-z0-9]
)
\.?
)
|
(?:
(?: 25[0-5] | 2[0-4]\d | 1\d\d | [1-9]?\d )
(?: \. (?: 25[0-5] | 2[0-4]\d | 1\d\d | [1-9]?\d ) ){3}
)
|
(
(
( [0-9A-Fa-f]{1,4} [:] ){7} ( [0-9A-Fa-f]{1,4} | [:] )
)|
(
( [0-9A-Fa-f]{1,4} [:] ){6}
(
[:] [0-9A-Fa-f]{1,4} |
(
( 25[0-5] | 2[0-4]\d | 1\d\d | [1-9]?\d )
( \. ( 25[0-5] | 2[0-4]\d | 1\d\d | [1-9]?\d ) ){3}
) | [:]
)
)|
(
( [0-9A-Fa-f]{1,4} [:] ){5}
(
(
( [:] [0-9A-Fa-f]{1,4} ){1,2}
)|
[:](
( 25[0-5] | 2[0-4]\d | 1\d\d | [1-9]?\d )
( \. ( 25[0-5] | 2[0-4]\d | 1\d\d | [1-9]?\d ) ){3}
)|
[:]
)
)|
(
( [0-9A-Fa-f]{1,4} [:] ){4}
(
( ( [:] [0-9A-Fa-f]{1,4} ){1,3} ) |
(
( [:] [0-9A-Fa-f]{1,4} )? [:]
(
( 25[0-5] | 2[0-4]\d | 1\d\d | [1-9]?\d )
( \. ( 25[0-5] | 2[0-4]\d | 1\d\d | [1-9]?\d ) ){3}
)
) | [:]
)
)|
(
( [0-9A-Fa-f]{1,4} [:] ){3}
(
( ( [:] [0-9A-Fa-f]{1,4} ){1,4} ) |
(
( [:] [0-9A-Fa-f]{1,4} ){0,2} [:]
(
( 25[0-5] | 2[0-4]\d | 1\d\d | [1-9]?\d )
( \. ( 25[0-5] | 2[0-4]\d | 1\d\d | [1-9]?\d ) ){3}
)
) | [:]
)
)|
(
( [0-9A-Fa-f]{1,4} [:] ){2}
(
( ( [:] [0-9A-Fa-f]{1,4} ){1,5} ) |
(
( [:] [0-9A-Fa-f]{1,4} ){0,3} [:]
(
( 25[0-5] | 2[0-4]\d | 1\d\d | [1-9]?\d )
( \. ( 25[0-5] | 2[0-4]\d | 1\d\d | [1-9]?\d ) ){3}
)
) | [:]
)
)|
(
( [0-9A-Fa-f]{1,4} [:] ){1}
(
( ( [:] [0-9A-Fa-f]{1,4} ){1,6} ) |
(
( [:] [0-9A-Fa-f]{1,4} ){0,4} [:]
(
( 25[0-5] | 2[0-4]\d | 1\d\d | [1-9]?\d )
( \. ( 25[0-5] | 2[0-4]\d | 1\d\d | [1-9]?\d ) ){3}
)
) | [:]
)
)|
(
[:]
(
( ( [:] [0-9A-Fa-f]{1,4} ){1,7} ) |
(
( [:] [0-9A-Fa-f]{1,4} ){0,5} [:]
(
( 25[0-5] | 2[0-4]\d | 1\d\d | [1-9]?\d )
( \. ( 25[0-5] | 2[0-4]\d | 1\d\d | [1-9]?\d ) ){3}
)
) | [:]
)
)
)
)
$/x
}
configuration.merge!( options )
validates_format_of( attr_names, configuration )
end
# Validate SIP username. This is the "user" rule from RFC 3261.
def self.validate_username( attr_names, options={} )
configuration = {
:allow_nil => false,
:allow_blank => false,
:with =>
/^
(?:
(?:
[A-Za-z0-9] |
[\-_.!~*'()]
) |
%[0-9A-F]{2} |
[&=+$,;?\/]
){1,255}
$/x
}
configuration.merge!( options )
validates_format_of( attr_names, configuration )
end
# Validate SIP password.
def self.validate_sip_password( attr_names, options={} )
configuration = {
:allow_nil => true,
:allow_blank => true,
:with =>
/^
(?:
(?:
[A-Za-z0-9] |
[\-_.!~*'()]
) |
%[0-9A-F]{2} |
[&=+$,]
){0,255}
$/x
}
configuration.merge!( options )
validates_format_of( attr_names, configuration )
end
end
module CustomValidators
# Validates a URL.
# TODO Convert this into a proper ActiveRecord validator.
#
def self.validate_url( url_str )
return false if url_str.blank?
require 'uri'
begin
uri = URI.parse( url_str )
return false if ! uri.absolute?
return false if ! uri.hierarchical?
return false if !( uri.is_a?( URI::HTTP ) || uri.is_a?( URI::HTTPS ) )
rescue URI::InvalidURIError
return false
end
return true
end
end
|