missing_validators 0.1.1 → 0.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.
- data/README.md +57 -2
- data/lib/missing_validators/validators/email_validator.rb +33 -1
- data/lib/missing_validators/validators/inequality_validator.rb +13 -0
- data/lib/missing_validators/version.rb +3 -1
- data/missing_validators.gemspec +1 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/validators/email_validator_spec.rb +57 -17
- data/spec/validators/inequality_validator_spec.rb +31 -31
- metadata +18 -6
data/README.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
# MissingValidators
|
2
2
|
|
3
|
-
|
3
|
+
[](https://travis-ci.org/andrewgr/missing_validators)
|
4
|
+
[](https://codeclimate.com/github/andrewgr/missing_validators)
|
5
|
+
|
6
|
+
MissingValidators is a collection of custom validators that are often required in Rails applications plus shoulda-style RSpec matchers to test the validation rules.
|
4
7
|
|
5
8
|
## Installation
|
6
9
|
|
@@ -18,10 +21,62 @@ Or install it yourself as:
|
|
18
21
|
|
19
22
|
## Usage
|
20
23
|
|
21
|
-
|
24
|
+
### EmailValidator
|
25
|
+
|
26
|
+
With an ActiveRecord model:
|
27
|
+
|
28
|
+
class User < ActiveRecord::Base
|
29
|
+
attr_accessor :email, :name
|
30
|
+
validates :email, email: true
|
31
|
+
end
|
32
|
+
|
33
|
+
Or any ruby class:
|
34
|
+
|
35
|
+
class User
|
36
|
+
include ActiveModel::Validations
|
37
|
+
attr_accessor :email, :name
|
38
|
+
validates :email, email: true
|
39
|
+
end
|
40
|
+
|
41
|
+
You can specify domains to which the email domain should belong in one of the folowing ways:
|
42
|
+
|
43
|
+
validates :email, email: { domains: 'com' }
|
44
|
+
validates :email, email: { domains: :com }
|
45
|
+
validates :email, email: { domains: [:com, 'edu'] }
|
46
|
+
|
47
|
+
RSpec matcher is also available for your convenience:
|
48
|
+
|
49
|
+
describe User do
|
50
|
+
it { should ensure_valid_email_format_of(:email) }
|
51
|
+
end
|
52
|
+
|
53
|
+
### InequalityValidator
|
54
|
+
|
55
|
+
With an ActiveRecord model:
|
56
|
+
|
57
|
+
class Flight < ActiveRecord::Base
|
58
|
+
attr_accessor :origin, :destination
|
59
|
+
validates :origin, inequality: { to: :destination }
|
60
|
+
end
|
61
|
+
|
62
|
+
Or any ruby class:
|
63
|
+
|
64
|
+
class Flight
|
65
|
+
include ActiveModel::Validations
|
66
|
+
attr_accessor :origin, :destination
|
67
|
+
validates :origin, inequality: { to: :destination }
|
68
|
+
end
|
69
|
+
|
70
|
+
RSpec matcher is also available for your convenience:
|
71
|
+
|
72
|
+
describe Flight do
|
73
|
+
it { should ensure_inequality_of(:origin).to(:destination) }
|
74
|
+
end
|
22
75
|
|
23
76
|
## Contributing
|
24
77
|
|
78
|
+
Your contribution is welcome.
|
79
|
+
|
25
80
|
1. Fork it
|
26
81
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
82
|
3. Commit your changes (`git commit -am 'Added some feature'`)
|
@@ -1,6 +1,38 @@
|
|
1
|
+
# Allows to check if the value of a specific attribute is a valid email address.
|
2
|
+
#
|
3
|
+
# @example Validate that the user email is a valid email address.
|
4
|
+
# class User << ActiveRecord::Base
|
5
|
+
# attr_accessor :email, :name
|
6
|
+
# validates :email, email: true
|
7
|
+
# end
|
1
8
|
class EmailValidator < ActiveModel::EachValidator
|
9
|
+
# Checks if an attribute value is a valid email address.
|
10
|
+
#
|
11
|
+
# @param [Object] record object to validate
|
12
|
+
# @param [String] attribute name of the object attribute to validate
|
13
|
+
# @param [Object] attribute attribute value
|
2
14
|
def validate_each(record, attribute, value)
|
3
|
-
|
15
|
+
domains = Array.wrap options[:domain]
|
16
|
+
|
17
|
+
allow_blank = options[:allow_blank] || false
|
18
|
+
|
19
|
+
value = "" if allow_blank && !value
|
20
|
+
|
21
|
+
if !allow_blank && !value
|
22
|
+
message = options[:message] || I18n.t('errors.messages.email')
|
23
|
+
record.errors[attribute] << message
|
24
|
+
end
|
25
|
+
|
26
|
+
valid_domain = true
|
27
|
+
|
28
|
+
valid_format = !!(value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i)
|
29
|
+
if domains.any?
|
30
|
+
valid_domain = domains.inject (false) do |acc, domain|
|
31
|
+
acc || value.end_with?(".#{domain}")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
unless valid_format && valid_domain
|
4
36
|
message = options[:message] || I18n.t('errors.messages.email')
|
5
37
|
record.errors[attribute] << message
|
6
38
|
end
|
@@ -1,4 +1,17 @@
|
|
1
|
+
# Allows to check if the value of a specific attribute is not equal to
|
2
|
+
# the value of another attribute of an object.
|
3
|
+
#
|
4
|
+
# @example Validate that flight origin is not the same as its destination.
|
5
|
+
# class Flight << ActiveRecord::Base
|
6
|
+
# attr_accessor :origin, :destination
|
7
|
+
# validates :origin, inequality: { to: :destination }
|
8
|
+
# end
|
1
9
|
class InequalityValidator < ActiveModel::EachValidator
|
10
|
+
# Checks if an attribute value is unequal to another attrubute value.
|
11
|
+
#
|
12
|
+
# @param [Object] record object to validate
|
13
|
+
# @param [String] attribute name of the object attribute to validate
|
14
|
+
# @param [Object] attribute attribute value
|
2
15
|
def validate_each(record, attribute, value)
|
3
16
|
unequal_to_attr = options[:to]
|
4
17
|
|
data/missing_validators.gemspec
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -1,31 +1,71 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe EmailValidator do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
let(:klass) do
|
5
|
+
Class.new do
|
6
|
+
include ActiveModel::Validations
|
7
|
+
attr_accessor :email, :name
|
8
|
+
validates :email, email: true
|
9
|
+
end
|
8
10
|
end
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
+
subject(:model){ klass.new }
|
13
|
+
|
14
|
+
it { should ensure_valid_email_format_of(:email) }
|
15
|
+
it { should_not ensure_valid_email_format_of(:name) }
|
16
|
+
|
17
|
+
context "email has valid format" do
|
18
|
+
let(:klass) do
|
19
|
+
Class.new do
|
20
|
+
include ActiveModel::Validations
|
21
|
+
attr_accessor :email, :name
|
22
|
+
validates :email, email: true
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
it { should allow_value("super.user@example.com").for(:email) }
|
27
|
+
it { should_not allow_value("user_example.com").for(:email) }
|
28
|
+
end
|
12
29
|
|
13
|
-
|
14
|
-
|
30
|
+
context "email is in a specific domain" do
|
31
|
+
context "email domain specified as string" do
|
32
|
+
let(:klass) do
|
33
|
+
Class.new do
|
34
|
+
include ActiveModel::Validations
|
35
|
+
attr_accessor :email
|
36
|
+
validates :email, email: { domain: "edu" }
|
37
|
+
end
|
38
|
+
end
|
15
39
|
|
16
|
-
|
17
|
-
|
18
|
-
expect(user).to be_valid
|
40
|
+
it { should allow_value("user@example.edu").for(:email) }
|
41
|
+
it { should_not allow_value("user@example.com").for(:email) }
|
19
42
|
end
|
20
43
|
|
21
|
-
|
22
|
-
|
23
|
-
|
44
|
+
context "email domain specified as symbol" do
|
45
|
+
let(:klass) do
|
46
|
+
Class.new do
|
47
|
+
include ActiveModel::Validations
|
48
|
+
attr_accessor :email
|
49
|
+
validates :email, email: { domain: :edu }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
it { should allow_value("user@example.edu").for(:email) }
|
54
|
+
it { should_not allow_value("user@example.com").for(:email) }
|
24
55
|
end
|
25
56
|
|
26
|
-
|
27
|
-
|
28
|
-
|
57
|
+
context "email set as an array of strings and symbols" do
|
58
|
+
let(:klass) do
|
59
|
+
Class.new do
|
60
|
+
include ActiveModel::Validations
|
61
|
+
attr_accessor :email
|
62
|
+
validates :email, email: { domain: ['com', :edu] }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
it { should allow_value("user@example.com").for(:email) }
|
67
|
+
it { should allow_value("user@example.edu").for(:email) }
|
68
|
+
it { should_not allow_value("user@example.org").for(:email) }
|
29
69
|
end
|
30
70
|
end
|
31
71
|
end
|
@@ -1,44 +1,44 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe InequalityValidator do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
let(:klass) do
|
5
|
+
Class.new do
|
6
|
+
include ActiveModel::Validations
|
7
|
+
attr_accessor :origin, :destination, :airline
|
8
|
+
validates :origin, inequality: { to: :destination }
|
9
|
+
end
|
8
10
|
end
|
9
11
|
|
10
|
-
|
11
|
-
subject(:flight){ Flight.new }
|
12
|
+
subject(:model){ klass.new }
|
12
13
|
|
13
|
-
|
14
|
-
|
14
|
+
it { should ensure_inequality_of(:origin).to(:destination) }
|
15
|
+
it { should_not ensure_inequality_of(:origin).to(:airline) }
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
17
|
+
specify "both fields have same values" do
|
18
|
+
model.origin = model.destination = "MOW"
|
19
|
+
expect(model).to be_invalid
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
specify "fields have different value" do
|
23
|
+
model.origin = "NYC"
|
24
|
+
model.destination = "MOW"
|
25
|
+
expect(model).to be_valid
|
26
|
+
end
|
26
27
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
28
|
+
specify "first field has value, the second is nil" do
|
29
|
+
model.origin = "NYC"
|
30
|
+
model.destination = nil
|
31
|
+
expect(model).to be_valid
|
32
|
+
end
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
34
|
+
specify "first field is nil, the second has value" do
|
35
|
+
model.origin = nil
|
36
|
+
model.destination = "NYC"
|
37
|
+
expect(model).to be_valid
|
38
|
+
end
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
end
|
40
|
+
specify "both fields are nil" do
|
41
|
+
model.origin = model.destination = nil
|
42
|
+
expect(model).to be_invalid
|
43
43
|
end
|
44
44
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: missing_validators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-02-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &2153717420 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,21 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2153717420
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: shoulda-matchers
|
27
|
+
requirement: &2153732080 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2153732080
|
25
36
|
- !ruby/object:Gem::Dependency
|
26
37
|
name: activemodel
|
27
|
-
requirement: &
|
38
|
+
requirement: &2153729920 !ruby/object:Gem::Requirement
|
28
39
|
none: false
|
29
40
|
requirements:
|
30
41
|
- - ! '>'
|
@@ -32,7 +43,7 @@ dependencies:
|
|
32
43
|
version: 3.0.0
|
33
44
|
type: :runtime
|
34
45
|
prerelease: false
|
35
|
-
version_requirements: *
|
46
|
+
version_requirements: *2153729920
|
36
47
|
description: Adds some handy validators.
|
37
48
|
email:
|
38
49
|
- andrew.gridnev@gmail.com
|
@@ -86,3 +97,4 @@ test_files:
|
|
86
97
|
- spec/spec_helper.rb
|
87
98
|
- spec/validators/email_validator_spec.rb
|
88
99
|
- spec/validators/inequality_validator_spec.rb
|
100
|
+
has_rdoc:
|