missing_validators 0.2.1 → 0.3.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/config/locales/en.yml +5 -1
- data/lib/missing_validators.rb +2 -0
- data/lib/missing_validators/matchers/ensure_valid_url_format_of.rb +20 -0
- data/lib/missing_validators/validators/url_validator.rb +28 -0
- data/lib/missing_validators/version.rb +1 -1
- data/spec/validators/email_validator_spec.rb +5 -11
- data/spec/validators/url_validator_spec.rb +35 -0
- metadata +12 -8
data/config/locales/en.yml
CHANGED
@@ -3,6 +3,7 @@ en:
|
|
3
3
|
messages:
|
4
4
|
inequality: "can't be equal to %{attr}"
|
5
5
|
email: "isn't a valid email address"
|
6
|
+
url: "isn't a valid URL"
|
6
7
|
missing_validators:
|
7
8
|
matchers:
|
8
9
|
ensure_inequality_of:
|
@@ -10,4 +11,7 @@ en:
|
|
10
11
|
failure_message_for_should_not: "#{model} should not ensure inequality on attribute #{attr}"
|
11
12
|
ensure_valid_email_format_of:
|
12
13
|
failure_message_for_should: "#{model} should ensure valid email format of attribute #{attr}"
|
13
|
-
failure_message_for_should_not: "#{model} should not ensure valid email format of attribute #{attr}"
|
14
|
+
failure_message_for_should_not: "#{model} should not ensure valid email format of attribute #{attr}"
|
15
|
+
ensure_valid_url_format_of:
|
16
|
+
failure_message_for_should: "#{model} should ensure valid URL format of attribute #{attr}"
|
17
|
+
failure_message_for_should_not: "#{model} should not ensure valid URL format of attribute #{attr}"
|
data/lib/missing_validators.rb
CHANGED
@@ -4,3 +4,5 @@ require 'missing_validators/validators/inequality_validator'
|
|
4
4
|
require 'missing_validators/matchers/ensure_inequality_of_matcher' if defined?(RSpec)
|
5
5
|
require 'missing_validators/validators/email_validator'
|
6
6
|
require 'missing_validators/matchers/ensure_valid_email_format_of' if defined?(RSpec)
|
7
|
+
require 'missing_validators/validators/url_validator'
|
8
|
+
require 'missing_validators/matchers/ensure_valid_url_format_of' if defined?(RSpec)
|
@@ -0,0 +1,20 @@
|
|
1
|
+
RSpec::Matchers.define :ensure_valid_url_format_of do |attribute|
|
2
|
+
match do |model|
|
3
|
+
model.send("#{attribute}=", ".")
|
4
|
+
model.valid?
|
5
|
+
|
6
|
+
if model.errors.has_key?(attribute)
|
7
|
+
model.errors[attribute].include?(I18n.t('errors.messages.url'))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message_for_should do |model|
|
12
|
+
I18n.t 'missing_validators.matchers.ensure_valid_url_format_of.failure_message_for_should',
|
13
|
+
model: model.class
|
14
|
+
end
|
15
|
+
|
16
|
+
failure_message_for_should_not do |model|
|
17
|
+
I18n.t 'missing_validators.matchers.ensure_valid_url_format_of.failure_message_for_should_not',
|
18
|
+
model: model.class
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'uri'
|
2
|
+
# Allows to check if the value of a specific attribute is a valid URL.
|
3
|
+
#
|
4
|
+
# @example Validate that the user's blog URL is valid.
|
5
|
+
# class User << ActiveRecord::Base
|
6
|
+
# attr_accessor :blog, :name
|
7
|
+
# validates :blog, url: true
|
8
|
+
# end
|
9
|
+
class UrlValidator < ActiveModel::EachValidator
|
10
|
+
# Checks if an attribute value is a valid URL.
|
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
|
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
|
22
|
+
|
23
|
+
unless valid_format
|
24
|
+
message = options[:message] || I18n.t('errors.messages.url')
|
25
|
+
record.errors[attribute] << message
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -1,19 +1,8 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe EmailValidator do
|
4
|
-
let(:klass) do
|
5
|
-
Class.new do
|
6
|
-
include ActiveModel::Validations
|
7
|
-
attr_accessor :email, :name
|
8
|
-
validates :email, email: true
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
4
|
subject(:model){ klass.new }
|
13
5
|
|
14
|
-
it { should ensure_valid_email_format_of(:email) }
|
15
|
-
it { should_not ensure_valid_email_format_of(:name) }
|
16
|
-
|
17
6
|
context "email has valid format" do
|
18
7
|
let(:klass) do
|
19
8
|
Class.new do
|
@@ -24,7 +13,12 @@ describe EmailValidator do
|
|
24
13
|
end
|
25
14
|
|
26
15
|
it { should allow_value("super.user@example.com").for(:email) }
|
16
|
+
it { should allow_value("super+user@example.com").for(:email) }
|
27
17
|
it { should_not allow_value("user_example.com").for(:email) }
|
18
|
+
|
19
|
+
|
20
|
+
it { should ensure_valid_email_format_of(:email) }
|
21
|
+
it { should_not ensure_valid_email_format_of(:name) }
|
28
22
|
end
|
29
23
|
|
30
24
|
context "email is in a specific domain" do
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
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
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
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) }
|
35
|
+
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.3.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-03-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &2156739940 !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: *
|
24
|
+
version_requirements: *2156739940
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: shoulda-matchers
|
27
|
-
requirement: &
|
27
|
+
requirement: &2156739000 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2156739000
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: activemodel
|
38
|
-
requirement: &
|
38
|
+
requirement: &2156737100 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>'
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: 3.0.0
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2156737100
|
47
47
|
description: Adds some handy validators.
|
48
48
|
email:
|
49
49
|
- andrew.gridnev@gmail.com
|
@@ -62,13 +62,16 @@ files:
|
|
62
62
|
- lib/missing_validators.rb
|
63
63
|
- lib/missing_validators/matchers/ensure_inequality_of_matcher.rb
|
64
64
|
- lib/missing_validators/matchers/ensure_valid_email_format_of.rb
|
65
|
+
- lib/missing_validators/matchers/ensure_valid_url_format_of.rb
|
65
66
|
- lib/missing_validators/validators/email_validator.rb
|
66
67
|
- lib/missing_validators/validators/inequality_validator.rb
|
68
|
+
- lib/missing_validators/validators/url_validator.rb
|
67
69
|
- lib/missing_validators/version.rb
|
68
70
|
- missing_validators.gemspec
|
69
71
|
- spec/spec_helper.rb
|
70
72
|
- spec/validators/email_validator_spec.rb
|
71
73
|
- spec/validators/inequality_validator_spec.rb
|
74
|
+
- spec/validators/url_validator_spec.rb
|
72
75
|
homepage: ''
|
73
76
|
licenses: []
|
74
77
|
post_install_message:
|
@@ -97,4 +100,5 @@ test_files:
|
|
97
100
|
- spec/spec_helper.rb
|
98
101
|
- spec/validators/email_validator_spec.rb
|
99
102
|
- spec/validators/inequality_validator_spec.rb
|
103
|
+
- spec/validators/url_validator_spec.rb
|
100
104
|
has_rdoc:
|