missing_validators 0.3.0 → 0.4.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
@@ -50,6 +50,29 @@ RSpec matcher is also available for your convenience:
50
50
  it { should ensure_valid_email_format_of(:email) }
51
51
  end
52
52
 
53
+ ### UrlValidator
54
+
55
+ With an ActiveRecord model:
56
+
57
+ class User < ActiveRecord::Base
58
+ attr_accessor :blog, :name
59
+ validates :blog, url: true
60
+ end
61
+
62
+ Or any ruby class:
63
+
64
+ class User
65
+ include ActiveModel::Validations
66
+ attr_accessor :blog, :name
67
+ validates :blog, url: true
68
+ end
69
+
70
+ RSpec matcher is also available for your convenience:
71
+
72
+ describe User do
73
+ it { should ensure_valid_url_format_of(:url) }
74
+ end
75
+
53
76
  ### InequalityValidator
54
77
 
55
78
  With an ActiveRecord model:
@@ -10,32 +10,19 @@ class EmailValidator < ActiveModel::EachValidator
10
10
  #
11
11
  # @param [Object] record object to validate
12
12
  # @param [String] attribute name of the object attribute to validate
13
- # @param [Object] attribute attribute value
13
+ # @param [Object] value attribute value
14
14
  def validate_each(record, attribute, value)
15
- domains = Array.wrap options[:domain]
16
-
17
15
  allow_blank = options[:allow_blank] || false
16
+ return if allow_blank && value.blank?
18
17
 
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
- return
25
- end
18
+ domains = Array.wrap(options[:domain])
19
+ email = value && value.downcase
20
+ in_valid_domain = domains.empty? ? true : domains.any? { |domain| email.end_with?(".#{domain.downcase}") }
26
21
 
27
- valid_domain = true
28
-
29
- valid_format = !!(value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i)
30
- if domains.any?
31
- valid_domain = domains.inject (false) do |acc, domain|
32
- acc || value.end_with?(".#{domain}")
33
- end
34
- end
22
+ has_valid_format = !!(value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i)
35
23
 
36
- unless valid_format && valid_domain
37
- message = options[:message] || I18n.t('errors.messages.email')
38
- record.errors[attribute] << message
24
+ unless in_valid_domain && has_valid_format
25
+ record.errors[attribute] << (options[:message] || I18n.t('errors.messages.email'))
39
26
  end
40
27
  end
41
- end
28
+ end
@@ -11,7 +11,7 @@ class InequalityValidator < ActiveModel::EachValidator
11
11
  #
12
12
  # @param [Object] record object to validate
13
13
  # @param [String] attribute name of the object attribute to validate
14
- # @param [Object] attribute attribute value
14
+ # @param [Object] value attribute value
15
15
  def validate_each(record, attribute, value)
16
16
  unequal_to_attr = options[:to]
17
17
 
@@ -11,18 +11,16 @@ class UrlValidator < ActiveModel::EachValidator
11
11
  #
12
12
  # @param [Object] record object to validate
13
13
  # @param [String] attribute name of the object attribute to validate
14
- # @param [Object] attribute attribute value
14
+ # @param [Object] value attribute value
15
15
  def validate_each(record, attribute, value)
16
- begin
17
- uri = URI.parse value
18
- valid_format = uri.kind_of? URI::HTTP
19
- rescue
20
- valid_format = false
21
- end
16
+ uri = URI.parse(value)
17
+ raise URI::InvalidURIError unless uri.kind_of?(URI::HTTP)
22
18
 
23
- unless valid_format
24
- message = options[:message] || I18n.t('errors.messages.url')
25
- record.errors[attribute] << message
26
- end
19
+ domains = Array.wrap(options[:domain])
20
+ host = uri.host.downcase
21
+ in_valid_domain = domains.empty? || domains.any? { |domain| host.end_with?(".#{domain.downcase}") }
22
+ raise URI::InvalidURIError unless in_valid_domain
23
+ rescue URI::InvalidURIError
24
+ record.errors[attribute] << (options[:message] || I18n.t('errors.messages.url'))
27
25
  end
28
26
  end
@@ -1,5 +1,5 @@
1
1
  # Provides a collection of custom validators that are often required in Rails applications.
2
2
  module MissingValidators
3
3
  # Gem version.
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
@@ -18,5 +18,6 @@ Gem::Specification.new do |gem|
18
18
  gem.add_development_dependency 'rspec'
19
19
  gem.add_development_dependency 'shoulda-matchers'
20
20
 
21
- gem.add_dependency 'activemodel', '> 3.0.0'
21
+ gem.add_dependency 'activemodel', '~> 3.0.0'
22
+ gem.add_dependency 'activesupport', '~> 3.0.0'
22
23
  end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe EmailValidator do
4
- subject(:model){ klass.new }
4
+ subject { klass.new }
5
5
 
6
6
  context "email has valid format" do
7
7
  let(:klass) do
@@ -16,12 +16,11 @@ describe EmailValidator do
16
16
  it { should allow_value("super+user@example.com").for(:email) }
17
17
  it { should_not allow_value("user_example.com").for(:email) }
18
18
 
19
-
20
19
  it { should ensure_valid_email_format_of(:email) }
21
20
  it { should_not ensure_valid_email_format_of(:name) }
22
21
  end
23
22
 
24
- context "email is in a specific domain" do
23
+ context "email is in the specific domain" do
25
24
  context "email domain specified as string" do
26
25
  let(:klass) do
27
26
  Class.new do
@@ -35,30 +34,19 @@ describe EmailValidator do
35
34
  it { should_not allow_value("user@example.com").for(:email) }
36
35
  end
37
36
 
38
- context "email domain specified as symbol" do
39
- let(:klass) do
40
- Class.new do
41
- include ActiveModel::Validations
42
- attr_accessor :email
43
- validates :email, email: { domain: :edu }
44
- end
45
- end
46
-
47
- it { should allow_value("user@example.edu").for(:email) }
48
- it { should_not allow_value("user@example.com").for(:email) }
49
- end
50
-
51
37
  context "email set as an array of strings and symbols" do
52
38
  let(:klass) do
53
39
  Class.new do
54
40
  include ActiveModel::Validations
55
41
  attr_accessor :email
56
- validates :email, email: { domain: ['com', :edu] }
42
+ validates :email, email: { domain: ['com', :edu, 'Com.Au'] }
57
43
  end
58
44
  end
59
45
 
60
46
  it { should allow_value("user@example.com").for(:email) }
61
47
  it { should allow_value("user@example.edu").for(:email) }
48
+ it { should allow_value("user@example.com.au").for(:email) }
49
+ it { should allow_value("user@example.Com.Au").for(:email) }
62
50
  it { should_not allow_value("user@example.org").for(:email) }
63
51
  end
64
52
  end
@@ -1,35 +1,72 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe UrlValidator do
4
- let(:klass) do
5
- Class.new do
6
- include ActiveModel::Validations
7
- attr_accessor :url, :name
8
- validates :url, url: true
4
+ context "url has valid format" do
5
+ let(:klass) do
6
+ Class.new do
7
+ include ActiveModel::Validations
8
+ attr_accessor :url, :name
9
+ validates :url, url: true
10
+ end
9
11
  end
12
+
13
+ subject { klass.new }
14
+
15
+ it { should ensure_valid_url_format_of(:url) }
16
+ it { should_not ensure_valid_url_format_of(:name) }
17
+
18
+ it { should allow_value("http://example.com").for(:url) }
19
+ it { should allow_value("http://FooBar.cOm").for(:url) }
20
+ it { should allow_value("http://foo.bar.baz.com").for(:url) }
21
+ it { should allow_value("http://123.com").for(:url) }
22
+ it { should allow_value("http://www.example.ru").for(:url) }
23
+ it { should allow_value("http://user-example.co.uk").for(:url) }
24
+ it { should allow_value("https://example.com").for(:url) }
25
+ it { should allow_value("http://example.org/").for(:url) }
26
+ it { should allow_value("https://example.net/index.html").for(:url) }
27
+ it { should allow_value("http://example.net/login.php").for(:url) }
28
+ it { should allow_value("https://example.travel/").for(:url) }
29
+ it { should allow_value("http://example.aero").for(:url) }
30
+ it { should allow_value("http://example.aero?foo=bar").for(:url) }
31
+
32
+ it { should_not allow_value("example").for(:url) }
33
+ it { should_not allow_value("http://user_examplecom").for(:url) }
34
+ it { should_not allow_value("http://user_example.com").for(:url) }
35
+ it { should_not allow_value("http://user_example.a").for(:url) }
10
36
  end
11
37
 
12
- subject(:model){ klass.new }
13
-
14
- it { should ensure_valid_url_format_of(:url) }
15
- it { should_not ensure_valid_url_format_of(:name) }
16
-
17
- it { should allow_value("http://example.com").for(:url) }
18
- it { should allow_value("http://FooBar.cOm").for(:url) }
19
- it { should allow_value("http://foo.bar.baz.com").for(:url) }
20
- it { should allow_value("http://123.com").for(:url) }
21
- it { should allow_value("http://www.example.ru").for(:url) }
22
- it { should allow_value("http://user-example.co.uk").for(:url) }
23
- it { should allow_value("https://example.com").for(:url) }
24
- it { should allow_value("http://example.org/").for(:url) }
25
- it { should allow_value("https://example.net/index.html").for(:url) }
26
- it { should allow_value("http://example.net/login.php").for(:url) }
27
- it { should allow_value("https://example.travel/").for(:url) }
28
- it { should allow_value("http://example.aero").for(:url) }
29
- it { should allow_value("http://example.aero?foo=bar").for(:url) }
30
-
31
- it { should_not allow_value("example").for(:url) }
32
- it { should_not allow_value("http://user_examplecom").for(:url) }
33
- it { should_not allow_value("http://user_example.com").for(:url) }
34
- it { should_not allow_value("http://user_example.a").for(:url) }
38
+ context "url is in the specified domain" do
39
+ let(:klass) do
40
+ Class.new do
41
+ include ActiveModel::Validations
42
+ attr_accessor :url, :name
43
+ validates :url, url: { domain: :org }
44
+ end
45
+ end
46
+
47
+ subject { klass.new }
48
+
49
+ it { should ensure_valid_url_format_of(:url) }
50
+ it { should allow_value("http://example.org").for(:url) }
51
+ it { should_not allow_value("http://example.com").for(:url) }
52
+ end
53
+
54
+ context "email set as an array of strings and symbols" do
55
+ let(:klass) do
56
+ Class.new do
57
+ include ActiveModel::Validations
58
+ attr_accessor :url, :name
59
+ validates :url, url: { domain: [:org, 'edu', 'Com.Au'] }
60
+ end
61
+ end
62
+
63
+ subject { klass.new }
64
+
65
+ it { should ensure_valid_url_format_of(:url) }
66
+ it { should allow_value("http://example.org").for(:url) }
67
+ it { should allow_value("http://example.edu").for(:url) }
68
+ it { should allow_value("http://example.com.au").for(:url) }
69
+ it { should allow_value("http://example.Com.Au").for(:url) }
70
+ it { should_not allow_value("http://example.com").for(:url) }
71
+ end
35
72
  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.3.0
4
+ version: 0.4.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-03-01 00:00:00.000000000 Z
12
+ date: 2013-09-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &2156739940 !ruby/object:Gem::Requirement
16
+ requirement: &70241132959780 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2156739940
24
+ version_requirements: *70241132959780
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: shoulda-matchers
27
- requirement: &2156739000 !ruby/object:Gem::Requirement
27
+ requirement: &70241132957560 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,18 +32,29 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2156739000
35
+ version_requirements: *70241132957560
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activemodel
38
- requirement: &2156737100 !ruby/object:Gem::Requirement
38
+ requirement: &70241132955500 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
- - - ! '>'
41
+ - - ~>
42
42
  - !ruby/object:Gem::Version
43
43
  version: 3.0.0
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *2156737100
46
+ version_requirements: *70241132955500
47
+ - !ruby/object:Gem::Dependency
48
+ name: activesupport
49
+ requirement: &70241132954740 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *70241132954740
47
58
  description: Adds some handy validators.
48
59
  email:
49
60
  - andrew.gridnev@gmail.com
@@ -101,4 +112,3 @@ test_files:
101
112
  - spec/validators/email_validator_spec.rb
102
113
  - spec/validators/inequality_validator_spec.rb
103
114
  - spec/validators/url_validator_spec.rb
104
- has_rdoc: