valid8ors 0.0.7 → 0.0.8
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 +29 -16
- data/lib/valid8ors/blacklist.rb +1 -1
- data/lib/valid8ors/email_format.rb +13 -3
- data/lib/valid8ors/password_strength.rb +26 -0
- data/lib/valid8ors/reserved.rb +1 -1
- data/lib/valid8ors/url_format.rb +1 -1
- data/lib/valid8ors.rb +1 -0
- data/test/blacklist_test.rb +0 -10
- data/test/email_format_test.rb +27 -14
- data/test/password_strength_test.rb +75 -0
- data/test/reserved_test.rb +1 -11
- data/test/url_format_test.rb +0 -10
- data/valid8ors.gemspec +1 -1
- metadata +9 -6
data/README.md
CHANGED
@@ -20,10 +20,6 @@ Add the following to one of your models:
|
|
20
20
|
|
21
21
|
validates :name, blacklist: true
|
22
22
|
|
23
|
-
You can also modify the default message ("is blacklisted") if validation fails:
|
24
|
-
|
25
|
-
validates :name, blacklist: { message: "is not part of the whitelist" }
|
26
|
-
|
27
23
|
### Blacklist file
|
28
24
|
|
29
25
|
You can create a blacklist.yml file in the config directory of your Rails application if you need to overload the one used by this gem.
|
@@ -40,7 +36,7 @@ You can translate (or overload) the default message via for e.g. using activerec
|
|
40
36
|
|
41
37
|
cd test
|
42
38
|
ruby blacklist_test.rb
|
43
|
-
|
39
|
+
|
44
40
|
## Reserved Validator
|
45
41
|
|
46
42
|
### Usage
|
@@ -49,10 +45,6 @@ Add the following to one of your models:
|
|
49
45
|
|
50
46
|
validates :name, reserved: true
|
51
47
|
|
52
|
-
You can also modify the default message ("is reserved") if validation fails:
|
53
|
-
|
54
|
-
validates :name, reserved: { message: "is not part of the whitelist" }
|
55
|
-
|
56
48
|
### Reserved file
|
57
49
|
|
58
50
|
You can create a reserved.yml file in the config directory of your Rails application if you need to overload the one used by this gem.
|
@@ -78,13 +70,14 @@ Add the following to one of your models:
|
|
78
70
|
|
79
71
|
validates :email, email_format: true
|
80
72
|
|
81
|
-
|
73
|
+
Restrict your validation to accept emails from specific domains only:
|
82
74
|
|
83
|
-
validates :email, email_format: {
|
75
|
+
validates :email, email_format: { domains: ['gmail.com', 'live.com'] }
|
84
76
|
|
85
77
|
### I18n
|
86
78
|
|
87
|
-
|
79
|
+
Default keys to translate are :improperly_formatted (when not an email format) and :invalid_domain (when email domain not listed in "domains" option).
|
80
|
+
So if you add to your User model:
|
88
81
|
|
89
82
|
validates :email, email_format: true
|
90
83
|
|
@@ -107,10 +100,6 @@ Add the following to one of your models:
|
|
107
100
|
|
108
101
|
validates :url, url_format: true
|
109
102
|
|
110
|
-
You can also modify the default message ("is improperly formatted") if validation fails:
|
111
|
-
|
112
|
-
validates :url, url_format: { message: "is not well formatted" }
|
113
|
-
|
114
103
|
### I18n
|
115
104
|
|
116
105
|
If you use I18n, the default key to translate is :improperly_formatted. So if you add to your User model:
|
@@ -124,6 +113,30 @@ You can translate (or overload) the default message via for e.g. (in english): "
|
|
124
113
|
cd test
|
125
114
|
ruby url_format_test.rb
|
126
115
|
|
116
|
+
## Password Strength Validator
|
117
|
+
|
118
|
+
Check if a password contains at least a lower case letter, an upper case letter and a digit.
|
119
|
+
Password length validation is not included here as you can use Rails' builtin "LengthValidator".
|
120
|
+
|
121
|
+
### Usage
|
122
|
+
|
123
|
+
Add the following to one of your models:
|
124
|
+
|
125
|
+
validates :password, password_strength: true
|
126
|
+
|
127
|
+
### I18n
|
128
|
+
|
129
|
+
The default key to translate is :insecure. So if you add to your User model:
|
130
|
+
|
131
|
+
validates :password, password_strength: true
|
132
|
+
|
133
|
+
You can translate (or overload) the default message via for e.g. (in english): "en.activerecord.errors.models.user.attributes.password.insecure"
|
134
|
+
|
135
|
+
### Tests
|
136
|
+
|
137
|
+
cd test
|
138
|
+
ruby password_strength_test.rb
|
139
|
+
|
127
140
|
## Compatibility
|
128
141
|
|
129
142
|
Ruby 1.8 is not supported.
|
data/lib/valid8ors/blacklist.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
class BlacklistValidator < ActiveModel::EachValidator
|
4
4
|
|
5
5
|
def validate_each(record, attribute, value)
|
6
|
-
record.errors.add(attribute,
|
6
|
+
record.errors.add(attribute, invalid_message(record, attribute)) if blacklisted?(value)
|
7
7
|
end
|
8
8
|
|
9
9
|
# Lazy load and transform directly words to patterns instead of creating regexps at each matching tests
|
@@ -2,11 +2,15 @@
|
|
2
2
|
|
3
3
|
class EmailFormatValidator < ActiveModel::EachValidator
|
4
4
|
|
5
|
-
EMAIL_PATTERN = /\A[\w\-\+_\.]+@((?:[\w\-\.]+\.)+[a-z]{2,})\Z/i
|
5
|
+
EMAIL_PATTERN = /\A[\w\-\+_\.]+@(?<domain>(?:[\w\-\.]+\.)+[a-z]{2,})\Z/i
|
6
6
|
|
7
7
|
def validate_each(record, attribute, value)
|
8
|
-
|
9
|
-
|
8
|
+
if matching = EMAIL_PATTERN.match(value)
|
9
|
+
if options[:domains] && !options[:domains].include?(matching[:domain])
|
10
|
+
record.errors.add(attribute, invalid_domain_message(record, attribute))
|
11
|
+
end
|
12
|
+
else
|
13
|
+
record.errors.add(attribute, invalid_message(record, attribute))
|
10
14
|
end
|
11
15
|
end
|
12
16
|
|
@@ -22,4 +26,10 @@ class EmailFormatValidator < ActiveModel::EachValidator
|
|
22
26
|
default: "is improperly formatted"
|
23
27
|
end
|
24
28
|
|
29
|
+
def invalid_domain_message(record, attribute)
|
30
|
+
I18n.t :invalid_domain,
|
31
|
+
scope: "#{record.class.i18n_scope}.errors.models.#{record.class.model_name.i18n_key}.attributes.#{attribute}",
|
32
|
+
default: "can't be from this domain"
|
33
|
+
end
|
34
|
+
|
25
35
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
class PasswordStrengthValidator < ActiveModel::EachValidator
|
4
|
+
|
5
|
+
# It must contain a lower case letter, an upper case letter and a digit
|
6
|
+
PASSWORD_PATTERN = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$/
|
7
|
+
|
8
|
+
def validate_each(record, attribute, value)
|
9
|
+
unless value =~ PASSWORD_PATTERN
|
10
|
+
record.errors.add(attribute, invalid_message(record, attribute))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
#######################
|
15
|
+
### Private methods ###
|
16
|
+
#######################
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def invalid_message(record, attribute)
|
21
|
+
I18n.t :insecure,
|
22
|
+
scope: "#{record.class.i18n_scope}.errors.models.#{record.class.model_name.i18n_key}.attributes.#{attribute}",
|
23
|
+
default: "is not strong enough"
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
data/lib/valid8ors/reserved.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
class ReservedValidator < ActiveModel::EachValidator
|
4
4
|
|
5
5
|
def validate_each(record, attribute, value)
|
6
|
-
record.errors.add(attribute,
|
6
|
+
record.errors.add(attribute, invalid_message(record, attribute)) if reserved?(value)
|
7
7
|
end
|
8
8
|
|
9
9
|
# Lazy load and transform directly words to patterns instead of creating regexps at each matching tests
|
data/lib/valid8ors/url_format.rb
CHANGED
@@ -6,7 +6,7 @@ class UrlFormatValidator < ActiveModel::EachValidator
|
|
6
6
|
|
7
7
|
def validate_each(record, attribute, value)
|
8
8
|
unless value =~ URL_PATTERN
|
9
|
-
record.errors.add(attribute,
|
9
|
+
record.errors.add(attribute, invalid_message(record, attribute))
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
data/lib/valid8ors.rb
CHANGED
data/test/blacklist_test.rb
CHANGED
@@ -6,10 +6,6 @@ class TestUser < TestModel
|
|
6
6
|
validates :name, blacklist: true
|
7
7
|
end
|
8
8
|
|
9
|
-
class TestUserWithMessage < TestModel
|
10
|
-
validates :name, blacklist: { message: 'is not part of the whitelist' }
|
11
|
-
end
|
12
|
-
|
13
9
|
class TestBlacklistValidator < MiniTest::Unit::TestCase
|
14
10
|
|
15
11
|
def test_some_blacklisted_words
|
@@ -30,12 +26,6 @@ class TestBlacklistValidator < MiniTest::Unit::TestCase
|
|
30
26
|
assert test_user.errors[:name].include?("is blacklisted")
|
31
27
|
end
|
32
28
|
|
33
|
-
def test_custom_message_on_error
|
34
|
-
test_user = TestUserWithMessage.new(name: "Fuck it up")
|
35
|
-
refute test_user.valid?
|
36
|
-
assert test_user.errors[:name].include?("is not part of the whitelist")
|
37
|
-
end
|
38
|
-
|
39
29
|
#######################
|
40
30
|
### Private methods ###
|
41
31
|
#######################
|
data/test/email_format_test.rb
CHANGED
@@ -6,16 +6,16 @@ class TestUser < TestModel
|
|
6
6
|
validates :email, email_format: true
|
7
7
|
end
|
8
8
|
|
9
|
-
class
|
10
|
-
validates :email, email_format: {
|
9
|
+
class TestUserWithEmailDomains < TestModel
|
10
|
+
validates :email, email_format: { domains: ['mail.com', 'subdomain.mail.com'] }
|
11
11
|
end
|
12
12
|
|
13
|
-
class
|
14
|
-
validates :email, email_format: { allow_nil:
|
13
|
+
class TestUserAllowsNilEmailToTrue < TestModel
|
14
|
+
validates :email, email_format: { allow_nil: true }
|
15
15
|
end
|
16
16
|
|
17
|
-
class
|
18
|
-
validates :email, email_format: {
|
17
|
+
class TestUserAllowsNilEmailToFalse < TestModel
|
18
|
+
validates :email, email_format: { allow_nil: false }
|
19
19
|
end
|
20
20
|
|
21
21
|
class TestEmailFormatValidator < MiniTest::Unit::TestCase
|
@@ -28,16 +28,29 @@ class TestEmailFormatValidator < MiniTest::Unit::TestCase
|
|
28
28
|
invalid_emails.each { |email| refute TestUser.new(email: email).valid? }
|
29
29
|
end
|
30
30
|
|
31
|
-
def
|
32
|
-
|
31
|
+
def test_email_when_domains_option
|
32
|
+
valid_emails_with_correct_domains = ['user@mail.com', 'user@subdomain.mail.com']
|
33
|
+
valid_emails_with_incorrect_domains = ['user@gmail.com', 'user@subdomain.gmail.com']
|
34
|
+
|
35
|
+
valid_emails_with_correct_domains.each do |email|
|
36
|
+
assert TestUserWithEmailDomains.new(email: email).valid?
|
37
|
+
end
|
38
|
+
|
39
|
+
valid_emails_with_incorrect_domains.each do |email|
|
40
|
+
refute TestUserWithEmailDomains.new(email: email).valid?
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_default_invalid_domain_message_on_error
|
45
|
+
test_user = TestUserWithEmailDomains.new(email: "user@gmail.com")
|
33
46
|
refute test_user.valid?
|
34
|
-
assert test_user.errors[:email].include?("
|
47
|
+
assert test_user.errors[:email].include?("can't be from this domain")
|
35
48
|
end
|
36
49
|
|
37
|
-
def
|
38
|
-
test_user =
|
50
|
+
def test_default_message_on_error
|
51
|
+
test_user = TestUser.new(email: "invalid_email@")
|
39
52
|
refute test_user.valid?
|
40
|
-
assert test_user.errors[:email].include?("is
|
53
|
+
assert test_user.errors[:email].include?("is improperly formatted")
|
41
54
|
end
|
42
55
|
|
43
56
|
def test_nil_email_when_allow_nil_option_is_not_set
|
@@ -45,11 +58,11 @@ class TestEmailFormatValidator < MiniTest::Unit::TestCase
|
|
45
58
|
end
|
46
59
|
|
47
60
|
def test_nil_email_when_allow_nil_option_is_set_to_true
|
48
|
-
assert
|
61
|
+
assert TestUserAllowsNilEmailToTrue.new(email: nil).valid?
|
49
62
|
end
|
50
63
|
|
51
64
|
def test_nil_email_when_allow_nil_option_is_set_to_false
|
52
|
-
refute
|
65
|
+
refute TestUserAllowsNilEmailToFalse.new(email: nil).valid?
|
53
66
|
end
|
54
67
|
|
55
68
|
#######################
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require_relative 'test_helper'
|
4
|
+
|
5
|
+
class TestUser < TestModel
|
6
|
+
validates :password, password_strength: true
|
7
|
+
end
|
8
|
+
|
9
|
+
class TestUserAllowsNilPasswordToTrue < TestModel
|
10
|
+
validates :password, password_strength: { allow_nil: true }
|
11
|
+
end
|
12
|
+
|
13
|
+
class TestUserAllowsNilPasswordToFalse < TestModel
|
14
|
+
validates :password, password_strength: { allow_nil: false }
|
15
|
+
end
|
16
|
+
|
17
|
+
class TestPasswordFormatValidator < MiniTest::Unit::TestCase
|
18
|
+
|
19
|
+
def test_valid_passwords
|
20
|
+
valid_passwords.each { |password| assert TestUser.new(password: password).valid? }
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_invalid_passwords
|
24
|
+
invalid_passwords.each { |password| refute TestUser.new(password: password).valid? }
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_default_message_on_error
|
28
|
+
test_user = TestUser.new(password: "invalid_password")
|
29
|
+
refute test_user.valid?
|
30
|
+
assert test_user.errors[:password].include?("is not strong enough")
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_nil_password_when_allow_nil_option_is_not_set
|
34
|
+
refute TestUser.new(password: nil).valid?
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_nil_password_when_allow_nil_option_is_set_to_true
|
38
|
+
assert TestUserAllowsNilPasswordToTrue.new(password: nil).valid?
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_nil_password_when_allow_nil_option_is_set_to_false
|
42
|
+
refute TestUserAllowsNilPasswordToFalse.new(password: nil).valid?
|
43
|
+
end
|
44
|
+
|
45
|
+
#######################
|
46
|
+
### Private methods ###
|
47
|
+
#######################
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def valid_passwords
|
52
|
+
[
|
53
|
+
'aA1',
|
54
|
+
'Hey!Co0l',
|
55
|
+
'SmA$H1nG!_PUmP!',
|
56
|
+
'L0c@l'
|
57
|
+
]
|
58
|
+
end
|
59
|
+
|
60
|
+
def invalid_passwords
|
61
|
+
[
|
62
|
+
'aaa',
|
63
|
+
'AAA',
|
64
|
+
'123',
|
65
|
+
'aAa',
|
66
|
+
'a1a',
|
67
|
+
'1A1',
|
68
|
+
' aA1',
|
69
|
+
'aA1 ',
|
70
|
+
' aA1 ',
|
71
|
+
'aA 12c'
|
72
|
+
]
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
data/test/reserved_test.rb
CHANGED
@@ -6,10 +6,6 @@ class TestUser < TestModel
|
|
6
6
|
validates :name, reserved: true
|
7
7
|
end
|
8
8
|
|
9
|
-
class TestUserWithMessage < TestModel
|
10
|
-
validates :name, reserved: { message: 'is not part of the whitelist' }
|
11
|
-
end
|
12
|
-
|
13
9
|
class TestReservedValidator < MiniTest::Unit::TestCase
|
14
10
|
|
15
11
|
def test_some_reserved_words
|
@@ -23,7 +19,7 @@ class TestReservedValidator < MiniTest::Unit::TestCase
|
|
23
19
|
def test_some_not_reserved_words
|
24
20
|
names_that_should_be_not_reserved.each { |name| assert TestUser.new(name: name).valid? }
|
25
21
|
end
|
26
|
-
|
22
|
+
|
27
23
|
def test_with_nil_attribute
|
28
24
|
test_user = TestUser.new(name: nil)
|
29
25
|
assert test_user.valid?
|
@@ -35,12 +31,6 @@ class TestReservedValidator < MiniTest::Unit::TestCase
|
|
35
31
|
assert test_user.errors[:name].include?("is reserved")
|
36
32
|
end
|
37
33
|
|
38
|
-
def test_custom_message_on_error
|
39
|
-
test_user = TestUserWithMessage.new(name: "hElp")
|
40
|
-
refute test_user.valid?
|
41
|
-
assert test_user.errors[:name].include?("is not part of the whitelist")
|
42
|
-
end
|
43
|
-
|
44
34
|
#######################
|
45
35
|
### Private methods ###
|
46
36
|
#######################
|
data/test/url_format_test.rb
CHANGED
@@ -14,10 +14,6 @@ class TestSiteAllowsNilToFalse < TestModel
|
|
14
14
|
validates :url, url_format: { allow_nil: false }
|
15
15
|
end
|
16
16
|
|
17
|
-
class TestSiteWithMessage < TestModel
|
18
|
-
validates :url, url_format: { message: 'is not well formatted' }
|
19
|
-
end
|
20
|
-
|
21
17
|
class TestUrlFormatValidator < MiniTest::Unit::TestCase
|
22
18
|
|
23
19
|
def test_valid_url
|
@@ -34,12 +30,6 @@ class TestUrlFormatValidator < MiniTest::Unit::TestCase
|
|
34
30
|
assert test_site.errors[:url].include?("is improperly formatted")
|
35
31
|
end
|
36
32
|
|
37
|
-
def test_custom_message_on_error
|
38
|
-
test_site = TestSiteWithMessage.new(url: "invalid_url")
|
39
|
-
refute test_site.valid?
|
40
|
-
assert test_site.errors[:url].include?("is not well formatted")
|
41
|
-
end
|
42
|
-
|
43
33
|
def test_nil_url_when_allow_nil_option_is_not_set
|
44
34
|
refute TestSite.new(url: nil).valid?
|
45
35
|
end
|
data/valid8ors.gemspec
CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "valid8ors"
|
6
|
-
s.version = "0.0.
|
6
|
+
s.version = "0.0.8"
|
7
7
|
s.authors = ["Axel Vergult", "Vincent Pochet", "Ben Colon"]
|
8
8
|
s.email = ["axel@official.fm", "vincent@official.fm", "ben@official.fm"]
|
9
9
|
s.homepage = ""
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: valid8ors
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,11 +11,11 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2012-
|
14
|
+
date: 2012-07-02 00:00:00.000000000Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activemodel
|
18
|
-
requirement: &
|
18
|
+
requirement: &70180450534040 !ruby/object:Gem::Requirement
|
19
19
|
none: false
|
20
20
|
requirements:
|
21
21
|
- - ! '>='
|
@@ -23,7 +23,7 @@ dependencies:
|
|
23
23
|
version: '0'
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
|
-
version_requirements: *
|
26
|
+
version_requirements: *70180450534040
|
27
27
|
description: Rails 3 awesome custom validators
|
28
28
|
email:
|
29
29
|
- axel@official.fm
|
@@ -43,10 +43,12 @@ files:
|
|
43
43
|
- lib/valid8ors.rb
|
44
44
|
- lib/valid8ors/blacklist.rb
|
45
45
|
- lib/valid8ors/email_format.rb
|
46
|
+
- lib/valid8ors/password_strength.rb
|
46
47
|
- lib/valid8ors/reserved.rb
|
47
48
|
- lib/valid8ors/url_format.rb
|
48
49
|
- test/blacklist_test.rb
|
49
50
|
- test/email_format_test.rb
|
51
|
+
- test/password_strength_test.rb
|
50
52
|
- test/reserved_test.rb
|
51
53
|
- test/test_helper.rb
|
52
54
|
- test/url_format_test.rb
|
@@ -65,7 +67,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
65
67
|
version: '0'
|
66
68
|
segments:
|
67
69
|
- 0
|
68
|
-
hash:
|
70
|
+
hash: 2205064151455909401
|
69
71
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
72
|
none: false
|
71
73
|
requirements:
|
@@ -74,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
74
76
|
version: '0'
|
75
77
|
segments:
|
76
78
|
- 0
|
77
|
-
hash:
|
79
|
+
hash: 2205064151455909401
|
78
80
|
requirements: []
|
79
81
|
rubyforge_project:
|
80
82
|
rubygems_version: 1.8.10
|
@@ -84,6 +86,7 @@ summary: Rails 3 awesome custom validators
|
|
84
86
|
test_files:
|
85
87
|
- test/blacklist_test.rb
|
86
88
|
- test/email_format_test.rb
|
89
|
+
- test/password_strength_test.rb
|
87
90
|
- test/reserved_test.rb
|
88
91
|
- test/test_helper.rb
|
89
92
|
- test/url_format_test.rb
|