valid 0.3.1 → 0.4.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.md +6 -5
- data/lib/validation/rule/regular_expression.rb +22 -0
- data/lib/validation/validator.rb +3 -13
- data/lib/validation/version.rb +1 -1
- metadata +3 -2
data/README.md
CHANGED
@@ -52,8 +52,8 @@ If you have a custom rule you need to write, just put it inside the `Validation:
|
|
52
52
|
|
53
53
|
A rule class should have the following methods on it:
|
54
54
|
|
55
|
-
- `error_key` a symbol to represent the error. This shows up in the errors hash
|
56
|
-
- `
|
55
|
+
- `error_key` a symbol to represent the error. This shows up in the errors hash. Must be an underscored_version of the class name
|
56
|
+
- `valid_value?(value)` the beef of the rule. This is where you determine if the value is valid or not
|
57
57
|
- `params` the params hash that was passed into the constructor
|
58
58
|
|
59
59
|
### Writing self-contained validators
|
@@ -83,8 +83,9 @@ Now you can use this anywhere in your code:
|
|
83
83
|
Have an improvement? Have an awesome rule you want included? Simple!
|
84
84
|
|
85
85
|
1. Fork the repository
|
86
|
-
2.
|
87
|
-
3.
|
88
|
-
4.
|
86
|
+
2. Create a branch off of the `develop` branch
|
87
|
+
3. Write specs for the change
|
88
|
+
4. Add your change
|
89
|
+
5. Submit a pull request to merge against the `develop` branch
|
89
90
|
|
90
91
|
Don't change any version files or gemspec files in your change.
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Validation
|
2
|
+
module Rule
|
3
|
+
class RegularExpression
|
4
|
+
|
5
|
+
def initialize(params)
|
6
|
+
@params = params
|
7
|
+
end
|
8
|
+
|
9
|
+
def error_key
|
10
|
+
:regular_expression
|
11
|
+
end
|
12
|
+
|
13
|
+
def valid_value?(value)
|
14
|
+
value.nil? || !!@params[:regex].match(value)
|
15
|
+
end
|
16
|
+
|
17
|
+
def params
|
18
|
+
@params
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/validation/validator.rb
CHANGED
@@ -44,7 +44,7 @@ module Validation
|
|
44
44
|
rules[field] << rule
|
45
45
|
end
|
46
46
|
rescue NameError => e
|
47
|
-
raise InvalidRule
|
47
|
+
raise InvalidRule.new(e)
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
@@ -53,19 +53,8 @@ module Validation
|
|
53
53
|
# one error per field
|
54
54
|
def valid?
|
55
55
|
valid = true
|
56
|
-
all_rules = {}
|
57
56
|
|
58
|
-
|
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|
|
57
|
+
rules.each_pair do |field, rules|
|
69
58
|
if ! @obj.respond_to?(field)
|
70
59
|
raise InvalidKey
|
71
60
|
end
|
@@ -120,6 +109,7 @@ module Validation
|
|
120
109
|
include Validation::Rules
|
121
110
|
|
122
111
|
def initialize(obj)
|
112
|
+
@rules = self.class.rules if self.class.is_a?(Validation::Rules)
|
123
113
|
@obj = obj
|
124
114
|
end
|
125
115
|
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.
|
4
|
+
version: 0.4.0
|
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:
|
12
|
+
date: 2014-01-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/regular_expression.rb
|
27
28
|
- lib/validation/rule/uri.rb
|
28
29
|
- lib/validation/validator.rb
|
29
30
|
- lib/validation/version.rb
|