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 CHANGED
@@ -1,6 +1,9 @@
1
1
  # MissingValidators
2
2
 
3
- TODO: Write a gem description
3
+ [![Build Status](https://travis-ci.org/andrewgr/missing_validators.png)](https://travis-ci.org/andrewgr/missing_validators)
4
+ [![Code Climate](https://codeclimate.com/badge.png)](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
- TODO: Write usage instructions here
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
- unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
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
 
@@ -1,3 +1,5 @@
1
+ # Provides a collection of custom validators that are often required in Rails applications.
1
2
  module MissingValidators
2
- VERSION = "0.1.1"
3
+ # Gem version.
4
+ VERSION = "0.2.0"
3
5
  end
@@ -16,6 +16,7 @@ Gem::Specification.new do |gem|
16
16
  gem.version = MissingValidators::VERSION
17
17
 
18
18
  gem.add_development_dependency 'rspec'
19
+ gem.add_development_dependency 'shoulda-matchers'
19
20
 
20
21
  gem.add_dependency 'activemodel', '> 3.0.0'
21
22
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'missing_validators'
2
+ require 'shoulda-matchers'
2
3
 
3
4
  I18n.load_path << File.expand_path("../../config/locales/en.yml", __FILE__)
4
5
 
@@ -1,31 +1,71 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe EmailValidator do
4
- class User
5
- include ActiveModel::Validations
6
- attr_accessor :email, :name
7
- validates :email, email: true
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
- context do
11
- subject(:user){ User.new }
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
- it { should ensure_valid_email_format_of(:email) }
14
- it { should_not ensure_valid_email_format_of(:name) }
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
- specify "is valid with a valid email address" do
17
- user.email = "super.user@example.com"
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
- specify "is valid with a valid email address" do
22
- user.email = "user@example.com"
23
- expect(user).to be_valid
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
- specify "fields have different value" do
27
- user.email = "user_example.com"
28
- expect(user).to be_invalid
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
- class Flight
5
- include ActiveModel::Validations
6
- attr_accessor :origin, :destination, :airline
7
- validates :origin, inequality: { to: :destination }
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
- context do
11
- subject(:flight){ Flight.new }
12
+ subject(:model){ klass.new }
12
13
 
13
- it { should ensure_inequality_of(:origin).to(:destination) }
14
- it { should_not ensure_inequality_of(:origin).to(:airline) }
14
+ it { should ensure_inequality_of(:origin).to(:destination) }
15
+ it { should_not ensure_inequality_of(:origin).to(:airline) }
15
16
 
16
- specify "both fields have same values" do
17
- flight.origin = flight.destination = "MOW"
18
- expect(flight).to be_invalid
19
- end
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
- specify "fields have different value" do
22
- flight.origin = "NYC"
23
- flight.destination = "MOW"
24
- expect(flight).to be_valid
25
- end
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
- specify "first field has value, the second is nil" do
28
- flight.origin = "NYC"
29
- flight.destination = nil
30
- expect(flight).to be_valid
31
- end
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
- specify "first field is nil, the second has value" do
34
- flight.origin = nil
35
- flight.destination = "NYC"
36
- expect(flight).to be_valid
37
- end
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
- specify "both fields are nil" do
40
- flight.origin = flight.destination = nil
41
- expect(flight).to be_invalid
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.1.1
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-01-28 00:00:00.000000000 Z
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: &2153570960 !ruby/object:Gem::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: *2153570960
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: &2153569660 !ruby/object:Gem::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: *2153569660
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: