valid 0.3.0 → 0.3.1
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/lib/validation/rule/uri.rb +31 -0
- data/lib/validation/validator.rb +12 -1
- data/lib/validation/version.rb +1 -1
- metadata +3 -2
@@ -0,0 +1,31 @@
|
|
1
|
+
module Validation
|
2
|
+
module Rule
|
3
|
+
class URI
|
4
|
+
def initialize(parts=[:host])
|
5
|
+
@required_parts = parts
|
6
|
+
end
|
7
|
+
|
8
|
+
def error_key
|
9
|
+
:uri
|
10
|
+
end
|
11
|
+
|
12
|
+
def params
|
13
|
+
{:required_elements => @required_parts}
|
14
|
+
end
|
15
|
+
|
16
|
+
def valid_value?(uri_string)
|
17
|
+
begin
|
18
|
+
uri = URI(uri_string)
|
19
|
+
@required_parts.each do |part|
|
20
|
+
if uri.send(part).nil? || uri.send(part).empty?
|
21
|
+
return false
|
22
|
+
end
|
23
|
+
end
|
24
|
+
true
|
25
|
+
rescue ::URI::InvalidURIError => e
|
26
|
+
return false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/validation/validator.rb
CHANGED
@@ -53,8 +53,19 @@ module Validation
|
|
53
53
|
# one error per field
|
54
54
|
def valid?
|
55
55
|
valid = true
|
56
|
+
all_rules = {}
|
56
57
|
|
57
|
-
|
58
|
+
if self.instance_of?(Validation::Validator)
|
59
|
+
# use the normal instance variable
|
60
|
+
all_rules = self.rules
|
61
|
+
|
62
|
+
elsif self.is_a?(Validation::Validator) # inherited "stand-alone" validator
|
63
|
+
# in this case the rules have been defined in the class instance
|
64
|
+
# variable '@rules' since they were defined during the class definition
|
65
|
+
all_rules = self.class.rules
|
66
|
+
end
|
67
|
+
|
68
|
+
all_rules.each_pair do |field, rules|
|
58
69
|
if ! @obj.respond_to?(field)
|
59
70
|
raise InvalidKey
|
60
71
|
end
|
data/lib/validation/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: valid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-06-30 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description:
|
15
15
|
email:
|
@@ -24,6 +24,7 @@ files:
|
|
24
24
|
- lib/validation/rule/not_empty.rb
|
25
25
|
- lib/validation/rule/numeric.rb
|
26
26
|
- lib/validation/rule/phone.rb
|
27
|
+
- lib/validation/rule/uri.rb
|
27
28
|
- lib/validation/validator.rb
|
28
29
|
- lib/validation/version.rb
|
29
30
|
- lib/validation.rb
|