valid 0.2.0 → 0.2.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/README.md +88 -0
- data/lib/validation/rule/email.rb +5 -3
- data/lib/validation/rule/length.rb +13 -0
- data/lib/validation/rule/matches.rb +9 -0
- data/lib/validation/rule/not_empty.rb +4 -0
- data/lib/validation/rule/numeric.rb +4 -0
- data/lib/validation/validator.rb +27 -0
- data/lib/validation/version.rb +1 -1
- metadata +7 -6
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# Validator
|
2
|
+
|
3
|
+
Validator is a simple ruby validation class. You don't use it directly inside your classes like just about every other ruby validation class out there. I chose to implement it in this way so I didn't automatically pollute the namespace of the objects I wanted to validate.
|
4
|
+
|
5
|
+
This also solves the problem of validating forms very nicely. Frequently you will have a form that represents many different data objects in your system, and you can pre-validate everything before doing any saving.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
Validator is useful for validating the state of any existing ruby object.
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
object = OpenStruct.new(:email => 'foo@bar.com', :password => 'foobar')
|
13
|
+
validator = Validation::Validator.new(object)
|
14
|
+
validator.rule(:email, [:email, :not_empty]) # multiple rules in one line
|
15
|
+
validator.rule(:password, :not_empty) # a single rule on a line
|
16
|
+
validator.rule(:password, :length => {:minimum => 3}) # a rule that takes parameters
|
17
|
+
|
18
|
+
if validator.valid?
|
19
|
+
# save the data somewhere
|
20
|
+
else
|
21
|
+
@errors = validator.errors
|
22
|
+
end
|
23
|
+
```
|
24
|
+
|
25
|
+
The first paramater can be any message that the object responds to.
|
26
|
+
|
27
|
+
### Writing your own rules
|
28
|
+
|
29
|
+
If you have a custom rule you need to write, just put it inside the `Validation::Rule` namespace:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
module Validation
|
33
|
+
module Rule
|
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
|
+
{}
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
A rule class should have the following methods on it:
|
52
|
+
|
53
|
+
- `error_key` a symbol to represent the error. This shows up in the errors hash
|
54
|
+
- `valid_values?(value)` the beef of the rule. This is where you determine if the value is valid or not
|
55
|
+
- `params` the params hash that was passed into the constructor
|
56
|
+
|
57
|
+
### Writing self-contained validators
|
58
|
+
|
59
|
+
You can also create self-contained validation classes if you don't like the dynamic creation approach:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
require 'validation'
|
63
|
+
require 'validation/rule/not_empty'
|
64
|
+
|
65
|
+
class MyFormValidator < Validation::Validator
|
66
|
+
include Validation
|
67
|
+
|
68
|
+
rule :email, :not_empty
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
72
|
+
Now you can use this anywhere in your code:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
form_validator = MyFormValidator.new(OpenStruct.new(params))
|
76
|
+
form_validator.valid?
|
77
|
+
```
|
78
|
+
|
79
|
+
# Contributing
|
80
|
+
|
81
|
+
Have an improvement? Have an awesome rule you want included? Simple!
|
82
|
+
|
83
|
+
1. Fork the repository
|
84
|
+
2. Write specs for the change
|
85
|
+
3. Add your change
|
86
|
+
4. Submit a pull request
|
87
|
+
|
88
|
+
Don't change any version files or gemspec files in your change.
|
@@ -1,8 +1,8 @@
|
|
1
1
|
module Validation
|
2
2
|
module Rule
|
3
|
-
class
|
3
|
+
# Email rule class. This rule was adapted from https://github.com/emmanuel/aequitas/blob/master/lib/aequitas/rule/format/email_address.rb
|
4
4
|
|
5
|
-
|
5
|
+
class Email
|
6
6
|
EMAIL_ADDRESS = begin
|
7
7
|
letter = 'a-zA-Z'
|
8
8
|
digit = '0-9'
|
@@ -28,15 +28,17 @@ module Validation
|
|
28
28
|
pattern = /\A#{addr_spec}\z/u
|
29
29
|
end
|
30
30
|
|
31
|
+
# Determines if value is a valid email
|
31
32
|
def valid_value?(value)
|
32
33
|
!!EMAIL_ADDRESS.match(value)
|
33
34
|
end
|
34
35
|
|
35
|
-
|
36
|
+
# The error key for this rule
|
36
37
|
def error_key
|
37
38
|
:email
|
38
39
|
end
|
39
40
|
|
41
|
+
# This rule has no params
|
40
42
|
def params
|
41
43
|
{}
|
42
44
|
end
|
@@ -1,14 +1,27 @@
|
|
1
1
|
module Validation
|
2
2
|
module Rule
|
3
|
+
# Length rule
|
3
4
|
class Length
|
5
|
+
# params can be any of the following:
|
6
|
+
#
|
7
|
+
# - :minimum - at least this many chars
|
8
|
+
# - :maximum - at most this many chars
|
9
|
+
# - :exact - exactly this many chars
|
10
|
+
#
|
11
|
+
# Example:
|
12
|
+
#
|
13
|
+
# {:minimum => 3, :maximum => 10}
|
14
|
+
# {:exact => 10}
|
4
15
|
def initialize(params)
|
5
16
|
@params = params
|
6
17
|
end
|
7
18
|
|
19
|
+
# returns the params given in the constructor
|
8
20
|
def params
|
9
21
|
@params
|
10
22
|
end
|
11
23
|
|
24
|
+
# determines if value is valid according to the constructor params
|
12
25
|
def valid_value?(value)
|
13
26
|
valid = true
|
14
27
|
|
@@ -1,20 +1,29 @@
|
|
1
1
|
module Validation
|
2
2
|
module Rule
|
3
|
+
# Matches rule
|
3
4
|
class Matches
|
4
5
|
attr_writer :obj
|
5
6
|
|
7
|
+
# This class should take the field to match with in the constructor:
|
8
|
+
#
|
9
|
+
# rule = Validation::Rule::Matches(:password)
|
10
|
+
# rule.obj = OpenStruct.new(:password => 'foo')
|
11
|
+
# rule.valid_value?('foo')
|
6
12
|
def initialize(matcher_field)
|
7
13
|
@matcher_field = matcher_field
|
8
14
|
end
|
9
15
|
|
16
|
+
# The error key for this rule
|
10
17
|
def error_key
|
11
18
|
:matches
|
12
19
|
end
|
13
20
|
|
21
|
+
# Params is the matcher_field given in the constructor
|
14
22
|
def params
|
15
23
|
@matcher_field
|
16
24
|
end
|
17
25
|
|
26
|
+
# Determines if value matches the field given in the constructor
|
18
27
|
def valid_value?(value)
|
19
28
|
matcher_value = @obj.send(@matcher_field)
|
20
29
|
matcher_value == value
|
@@ -1,14 +1,18 @@
|
|
1
1
|
module Validation
|
2
2
|
module Rule
|
3
|
+
# Rule for not empty
|
3
4
|
class NotEmpty
|
5
|
+
# This rule has no params
|
4
6
|
def params
|
5
7
|
{}
|
6
8
|
end
|
7
9
|
|
10
|
+
# Determines if value is empty or not. In this rule, nil is empty
|
8
11
|
def valid_value?(value)
|
9
12
|
! (value.nil? || value.empty?)
|
10
13
|
end
|
11
14
|
|
15
|
+
# The error key for this field
|
12
16
|
def error_key
|
13
17
|
:not_empty
|
14
18
|
end
|
@@ -1,14 +1,18 @@
|
|
1
1
|
module Validation
|
2
2
|
module Rule
|
3
|
+
# rule for numeric values
|
3
4
|
class Numeric
|
5
|
+
# Determines if value is numeric. It can only contain whole numbers
|
4
6
|
def valid_value?(value)
|
5
7
|
!!/^[0-9]+$/.match(value.to_s)
|
6
8
|
end
|
7
9
|
|
10
|
+
# The error key for this rule
|
8
11
|
def error_key
|
9
12
|
:numeric
|
10
13
|
end
|
11
14
|
|
15
|
+
# this rule has no params
|
12
16
|
def params
|
13
17
|
{}
|
14
18
|
end
|
data/lib/validation/validator.rb
CHANGED
@@ -1,13 +1,24 @@
|
|
1
1
|
module Validation
|
2
2
|
module Rules
|
3
|
+
# A hash of rules for this object
|
3
4
|
def rules
|
4
5
|
@rules ||= {}
|
5
6
|
end
|
6
7
|
|
8
|
+
# A hash of errors for this object
|
7
9
|
def errors
|
8
10
|
@errors ||= {}
|
9
11
|
end
|
10
12
|
|
13
|
+
# Define a rule for this object
|
14
|
+
#
|
15
|
+
# The rule parameter can be one of the following:
|
16
|
+
#
|
17
|
+
# * a symbol that matches to a class in the Validation::Rule namespace
|
18
|
+
# * e.g. rule(:field, :not_empty)
|
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})
|
21
|
+
# * an array combining the two previous types
|
11
22
|
def rule(field, rule)
|
12
23
|
field = field.to_sym
|
13
24
|
if rules[field].nil?
|
@@ -37,6 +48,9 @@ module Validation
|
|
37
48
|
end
|
38
49
|
end
|
39
50
|
|
51
|
+
# Determines if this object is valid. When a rule fails for a field,
|
52
|
+
# this will stop processing further rules. In this way, you'll only get
|
53
|
+
# one error per field
|
40
54
|
def valid?
|
41
55
|
valid = true
|
42
56
|
|
@@ -59,6 +73,7 @@ module Validation
|
|
59
73
|
|
60
74
|
protected
|
61
75
|
|
76
|
+
# Adds a parameterized rule to this object
|
62
77
|
def add_parameterized_rule(field, rule)
|
63
78
|
rule.each_pair do |key, value|
|
64
79
|
r = Validation::Rule.const_get(camelize(key)).new(value)
|
@@ -67,12 +82,14 @@ module Validation
|
|
67
82
|
end
|
68
83
|
end
|
69
84
|
|
85
|
+
# Adds this validation object to a rule if it can accept it
|
70
86
|
def add_object_to_rule(rule)
|
71
87
|
if rule.respond_to?(:obj=)
|
72
88
|
rule.obj = @obj
|
73
89
|
end
|
74
90
|
end
|
75
91
|
|
92
|
+
# Converts a symbol to a class name, taken from rails
|
76
93
|
def camelize(term)
|
77
94
|
string = term.to_s
|
78
95
|
string = string.sub(/^[a-z\d]*/) { $&.capitalize }
|
@@ -80,6 +97,14 @@ module Validation
|
|
80
97
|
end
|
81
98
|
end
|
82
99
|
|
100
|
+
# Validator is a simple ruby validation class. You don't use it directly
|
101
|
+
# inside your classes like just about every other ruby validation class out
|
102
|
+
# there. I chose to implement it in this way so I didn't automatically
|
103
|
+
# pollute the namespace of the objects I wanted to validate.
|
104
|
+
#
|
105
|
+
# This also solves the problem of validating forms very nicely. Frequently
|
106
|
+
# you will have a form that represents many different data objects in your
|
107
|
+
# system, and you can pre-validate everything before doing any saving.
|
83
108
|
class Validator
|
84
109
|
include Validation::Rules
|
85
110
|
|
@@ -88,9 +113,11 @@ module Validation
|
|
88
113
|
end
|
89
114
|
end
|
90
115
|
|
116
|
+
# InvalidKey is raised if a rule is added to a field that doesn't exist
|
91
117
|
class InvalidKey < RuntimeError
|
92
118
|
end
|
93
119
|
|
120
|
+
# InvalidRule is raised if a rule is added that doesn't exist
|
94
121
|
class InvalidRule < RuntimeError
|
95
122
|
end
|
96
123
|
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.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -18,15 +18,16 @@ executables: []
|
|
18
18
|
extensions: []
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
|
-
- lib/validation.rb
|
22
21
|
- lib/validation/rule/email.rb
|
23
|
-
- lib/validation/rule/
|
22
|
+
- lib/validation/rule/length.rb
|
24
23
|
- lib/validation/rule/matches.rb
|
24
|
+
- lib/validation/rule/not_empty.rb
|
25
25
|
- lib/validation/rule/numeric.rb
|
26
|
-
- lib/validation/rule/length.rb
|
27
26
|
- lib/validation/validator.rb
|
28
27
|
- lib/validation/version.rb
|
29
|
-
|
28
|
+
- lib/validation.rb
|
29
|
+
- README.md
|
30
|
+
homepage: https://github.com/zombor/Validator
|
30
31
|
licenses: []
|
31
32
|
post_install_message:
|
32
33
|
rdoc_options: []
|
@@ -46,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
46
47
|
version: '0'
|
47
48
|
requirements: []
|
48
49
|
rubyforge_project:
|
49
|
-
rubygems_version: 1.8.
|
50
|
+
rubygems_version: 1.8.24
|
50
51
|
signing_key:
|
51
52
|
specification_version: 3
|
52
53
|
summary: A standalone, generic object validator for ruby
|