valid 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 08af3476e39855609076f5f0258bebe71805abc2
4
- data.tar.gz: 3a068b16cc74896589d36a182dfae61a53783c14
3
+ metadata.gz: b1334f9de1e7da57f86aec24980f4b69f32ad95b
4
+ data.tar.gz: c941a5983d49df7fefa43035c00fd16f5756208c
5
5
  SHA512:
6
- metadata.gz: 74388c59fc69f8c1c0aed7901e9aec6e993bc8024eeb7a7fc6fd5e59c6a36dbbb6b1e62169a73b0325533c8c8a66cca52a55626518e7a9cafc7582e705484c52
7
- data.tar.gz: 188f7b55a14dc7eb68aa062c3419105ffecff87690f39835f0c74261b9cc2687732b4e0cc6d5b83c68ba8f0369ac611419ce20c1c7a71b90b94811b0a714e161
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]) # 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
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, just put it inside the `Validation::Rule` namespace:
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
- module Validation
35
- module Rule
36
- class MyCustomRule
37
- def error_key
38
- :my_custom_rule
39
- end
40
-
41
- def valid_value?(value)
42
- # Logic for determining the validity of the value
43
- end
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:
@@ -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, rule)
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 rule.respond_to?(:each_pair)
30
- add_parameterized_rule(field, rule)
31
- elsif rule.respond_to?(:each)
32
- rule.each do |r|
33
- if r.respond_to?(:each_pair)
34
- add_parameterized_rule(field, r)
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
- r = Validation::Rule.const_get(camelize(r)).new
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
- rule = Validation::Rule.const_get(camelize(rule)).new
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 parameterized rule to this object
78
- def add_parameterized_rule(field, rule)
79
- rule.each_pair do |key, value|
80
- r = Validation::Rule.const_get(camelize(key)).new(value)
81
- add_object_to_rule(r)
82
- rules[field] << r
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 this validation object to a rule if it can accept it
87
- def add_object_to_rule(rule)
88
- if rule.respond_to?(:obj=)
89
- rule.obj = @obj
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
@@ -1,3 +1,3 @@
1
1
  module Validation
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
3
3
  end
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.1.0
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: 2015-10-01 00:00:00.000000000 Z
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.2.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