valid 1.1.0 → 1.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 +29 -19
- data/lib/validation/validator.rb +35 -28
- data/lib/validation/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1334f9de1e7da57f86aec24980f4b69f32ad95b
|
4
|
+
data.tar.gz: c941a5983d49df7fefa43035c00fd16f5756208c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 350ca99bcea57a6b3e9c5ddd4329e6d3509fd59d9624738717d16f982603b2ce6403765bafd18b39991abc90f0de94612034070abaf93b242189269587175445
|
7
|
+
data.tar.gz: 33fff341d03a984e5726ccc1e6105057e47e8c1a0cfaa9b355b1c54d1c80ecbb059870efc47d979d1827f2c4304fbc4441b7808770aba7f29ece973ec0ba74ff
|
data/README.md
CHANGED
@@ -13,9 +13,9 @@ Validator is useful for validating the state of any existing ruby object.
|
|
13
13
|
```ruby
|
14
14
|
object = OpenStruct.new(:email => 'foo@bar.com', :password => 'foobar')
|
15
15
|
validator = Validation::Validator.new(object)
|
16
|
-
validator.rule(:email, [:email, :not_empty])
|
17
|
-
validator.rule(:password, :not_empty)
|
18
|
-
validator.rule(:password, :length => {:minimum => 3})
|
16
|
+
validator.rule(:email, [:email, :not_empty]) # multiple rules in one line
|
17
|
+
validator.rule(:password, :not_empty) # a single rule on a line
|
18
|
+
validator.rule(:password, :length => { :minimum => 3 }) # a rule that takes parameters
|
19
19
|
|
20
20
|
if validator.valid?
|
21
21
|
# save the data somewhere
|
@@ -28,24 +28,20 @@ The first paramater can be any message that the object responds to.
|
|
28
28
|
|
29
29
|
### Writing your own rules
|
30
30
|
|
31
|
-
If you have a custom rule you need to write,
|
31
|
+
If you have a custom rule you need to write, you can create a custom rule class for it:
|
32
32
|
|
33
33
|
```ruby
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
def params
|
46
|
-
{}
|
47
|
-
end
|
48
|
-
end
|
34
|
+
class MyCustomRule
|
35
|
+
def error_key
|
36
|
+
:my_custom_rule
|
37
|
+
end
|
38
|
+
|
39
|
+
def valid_value?(value)
|
40
|
+
# Logic for determining the validity of the value
|
41
|
+
end
|
42
|
+
|
43
|
+
def params
|
44
|
+
{}
|
49
45
|
end
|
50
46
|
end
|
51
47
|
```
|
@@ -56,6 +52,20 @@ A rule class should have the following methods on it:
|
|
56
52
|
- `valid_value?(value)` the beef of the rule. This is where you determine if the value is valid or not
|
57
53
|
- `params` the params hash that was passed into the constructor
|
58
54
|
|
55
|
+
If you add your custom rule class to the `Validation::Rule` namespace, you can reference it using a symbol:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
validator.rule(:field, :my_custom_rule) # resolves to Validation::Rule::MyCustomRule
|
59
|
+
validator.rule(:field, :my_custom_rule => { :param => :value })
|
60
|
+
```
|
61
|
+
|
62
|
+
Otherwise, just pass in the rule class itself:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
validator.rule(:field, MyProject::CustomRule)
|
66
|
+
validator.rule(:field, MyProject::CustomRule => { :param => :value })
|
67
|
+
```
|
68
|
+
|
59
69
|
### Writing self-contained validators
|
60
70
|
|
61
71
|
You can also create self-contained validation classes if you don't like the dynamic creation approach:
|
data/lib/validation/validator.rb
CHANGED
@@ -17,31 +17,25 @@ module Validation
|
|
17
17
|
# * a symbol that matches to a class in the Validation::Rule namespace
|
18
18
|
# * e.g. rule(:field, :not_empty)
|
19
19
|
# * a hash containing the rule as the key and it's parameters as the values
|
20
|
-
# * e.g. rule(:field, :length => {:minimum => 3, :maximum => 5})
|
20
|
+
# * e.g. rule(:field, :length => { :minimum => 3, :maximum => 5 })
|
21
21
|
# * an array combining the two previous types
|
22
|
-
def rule(field,
|
22
|
+
def rule(field, definition)
|
23
23
|
field = field.to_sym
|
24
|
-
if rules[field].nil?
|
25
|
-
rules[field] = []
|
26
|
-
end
|
24
|
+
rules[field] = [] if rules[field].nil?
|
27
25
|
|
28
26
|
begin
|
29
|
-
if
|
30
|
-
|
31
|
-
elsif
|
32
|
-
|
33
|
-
if
|
34
|
-
|
27
|
+
if definition.respond_to?(:each_pair)
|
28
|
+
add_parameterized_rules(field, definition)
|
29
|
+
elsif definition.respond_to?(:each)
|
30
|
+
definition.each do |item|
|
31
|
+
if item.respond_to?(:each_pair)
|
32
|
+
add_parameterized_rules(field, item)
|
35
33
|
else
|
36
|
-
|
37
|
-
add_object_to_rule(r)
|
38
|
-
rules[field] << r
|
34
|
+
add_single_rule(field, item)
|
39
35
|
end
|
40
36
|
end
|
41
37
|
else
|
42
|
-
|
43
|
-
add_object_to_rule(rule)
|
44
|
-
rules[field] << rule
|
38
|
+
add_single_rule(field, definition)
|
45
39
|
end
|
46
40
|
rescue NameError => e
|
47
41
|
raise InvalidRule.new(e)
|
@@ -57,7 +51,7 @@ module Validation
|
|
57
51
|
|
58
52
|
rules.each_pair do |field, rules|
|
59
53
|
if ! @obj.respond_to?(field)
|
60
|
-
raise InvalidKey
|
54
|
+
raise InvalidKey, "cannot validate non-existent field '#{field}'"
|
61
55
|
end
|
62
56
|
|
63
57
|
rules.each do |r|
|
@@ -74,22 +68,35 @@ module Validation
|
|
74
68
|
|
75
69
|
protected
|
76
70
|
|
77
|
-
# Adds a
|
78
|
-
def
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
71
|
+
# Adds a single rule to this object
|
72
|
+
def add_single_rule(field, key_or_klass, params = nil)
|
73
|
+
klass = if key_or_klass.respond_to?(:new)
|
74
|
+
key_or_klass
|
75
|
+
else
|
76
|
+
get_rule_class_by_name(key_or_klass)
|
83
77
|
end
|
78
|
+
|
79
|
+
args = [params].compact
|
80
|
+
rule = klass.new(*args)
|
81
|
+
rule.obj = @obj if rule.respond_to?(:obj=)
|
82
|
+
rules[field] << rule
|
84
83
|
end
|
85
84
|
|
86
|
-
# Adds
|
87
|
-
def
|
88
|
-
|
89
|
-
|
85
|
+
# Adds a set of parameterized rules to this object
|
86
|
+
def add_parameterized_rules(field, rules)
|
87
|
+
rules.each_pair do |key, params|
|
88
|
+
add_single_rule(field, key, params)
|
90
89
|
end
|
91
90
|
end
|
92
91
|
|
92
|
+
# Resolves the specified rule name to a rule class
|
93
|
+
def get_rule_class_by_name(klass)
|
94
|
+
klass = camelize(klass)
|
95
|
+
Validation::Rule.const_get(klass)
|
96
|
+
rescue NameError => e
|
97
|
+
raise InvalidRule.new(e)
|
98
|
+
end
|
99
|
+
|
93
100
|
# Converts a symbol to a class name, taken from rails
|
94
101
|
def camelize(term)
|
95
102
|
string = term.to_s
|
data/lib/validation/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: valid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Bush
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-06-27 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -50,7 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
50
50
|
version: '0'
|
51
51
|
requirements: []
|
52
52
|
rubyforge_project:
|
53
|
-
rubygems_version: 2.
|
53
|
+
rubygems_version: 2.5.1
|
54
54
|
signing_key:
|
55
55
|
specification_version: 4
|
56
56
|
summary: A standalone, generic object validator for ruby
|