valid8ors 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.md +54 -0
- data/config/reserved.yml +8 -0
- data/lib/valid8ors/reserved.rb +37 -0
- data/lib/valid8ors.rb +1 -0
- data/test/reserved_test.rb +53 -0
- data/valid8ors.gemspec +3 -3
- metadata +10 -4
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -40,6 +40,35 @@ You can translate (or overload) the default message via for e.g. using activerec
|
|
40
40
|
|
41
41
|
cd test
|
42
42
|
ruby blacklist_test.rb
|
43
|
+
|
44
|
+
## Reserved Validator
|
45
|
+
|
46
|
+
### Usage
|
47
|
+
|
48
|
+
Add the following to one of your models:
|
49
|
+
|
50
|
+
validates :name, reserved: true
|
51
|
+
|
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
|
+
### Reserved file
|
57
|
+
|
58
|
+
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.
|
59
|
+
|
60
|
+
### I18n
|
61
|
+
|
62
|
+
If you use I18n, the default key to translate is :reserved. So if you add to your User model:
|
63
|
+
|
64
|
+
validates :name, reserved: true
|
65
|
+
|
66
|
+
You can translate (or overload) the default message via for e.g. using activerecord (in english): "en.activerecord.errors.models.user.attributes.name.reserved"
|
67
|
+
|
68
|
+
### Tests
|
69
|
+
|
70
|
+
cd test
|
71
|
+
ruby reserved_test.rb
|
43
72
|
|
44
73
|
## Email Format Validator
|
45
74
|
|
@@ -70,6 +99,31 @@ You can translate (or overload) the default message via for e.g. (in english): "
|
|
70
99
|
|
71
100
|
Regular Expression tests based on [Comparing E-mail Address Validating Regular Expressions](http://fightingforalostcause.net/misc/2006/compare-email-regex.php)
|
72
101
|
|
102
|
+
## Url Format Validator
|
103
|
+
|
104
|
+
### Usage
|
105
|
+
|
106
|
+
Add the following to one of your models:
|
107
|
+
|
108
|
+
validates :url, url_format: true
|
109
|
+
|
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
|
+
### I18n
|
115
|
+
|
116
|
+
If you use I18n, the default key to translate is :improperly_formatted. So if you add to your User model:
|
117
|
+
|
118
|
+
validates :url, url_format: true
|
119
|
+
|
120
|
+
You can translate (or overload) the default message via for e.g. (in english): "en.activerecord.errors.models.user.attributes.url.improperly_formatted"
|
121
|
+
|
122
|
+
### Tests
|
123
|
+
|
124
|
+
cd test
|
125
|
+
ruby url_format_test.rb
|
126
|
+
|
73
127
|
## Compatibility
|
74
128
|
|
75
129
|
Ruby 1.8 is not supported.
|
data/config/reserved.yml
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
class ReservedValidator < ActiveModel::EachValidator
|
4
|
+
|
5
|
+
def validate_each(record, attribute, value)
|
6
|
+
record.errors.add(attribute, options[:message] || invalid_message(record, attribute)) if reserved?(value)
|
7
|
+
end
|
8
|
+
|
9
|
+
# Lazy load and transform directly words to patterns instead of creating regexps at each matching tests
|
10
|
+
def reserved
|
11
|
+
@reserved ||= YAML.load_file(reserved_file)
|
12
|
+
end
|
13
|
+
|
14
|
+
#######################
|
15
|
+
### Private methods ###
|
16
|
+
#######################
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def invalid_message(record, attribute)
|
21
|
+
I18n.t :reserved,
|
22
|
+
scope: "#{record.class.i18n_scope}.errors.models.#{record.class.model_name.i18n_key}.attributes.#{attribute}",
|
23
|
+
default: "is reserved"
|
24
|
+
end
|
25
|
+
|
26
|
+
def reserved?(str)
|
27
|
+
reserved.any? { |pattern| str == pattern }
|
28
|
+
end
|
29
|
+
|
30
|
+
def reserved_file
|
31
|
+
if defined?(Rails.root) && (reserved_file_path = Rails.root.join("config", "reserved.yml")).exist?
|
32
|
+
return reserved_file_path
|
33
|
+
end
|
34
|
+
File.join(File.dirname(__FILE__), "../../config/reserved.yml")
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
data/lib/valid8ors.rb
CHANGED
@@ -0,0 +1,53 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require_relative 'test_helper'
|
4
|
+
|
5
|
+
class TestUser < TestModel
|
6
|
+
validates :name, reserved: true
|
7
|
+
end
|
8
|
+
|
9
|
+
class TestUserWithMessage < TestModel
|
10
|
+
validates :name, reserved: { message: 'is not part of the whitelist' }
|
11
|
+
end
|
12
|
+
|
13
|
+
class TestBlacklistValidator < MiniTest::Unit::TestCase
|
14
|
+
|
15
|
+
def test_some_reserved_words
|
16
|
+
names_that_should_be_reserved.each do |name|
|
17
|
+
test_user = TestUser.new(name: name)
|
18
|
+
refute test_user.valid?
|
19
|
+
assert test_user.errors[:name]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_some_not_reserved_words
|
24
|
+
names_that_should_be_not_reserved.each { |name| assert TestUser.new(name: name).valid? }
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_default_message_on_error
|
28
|
+
test_user = TestUser.new(name: "about")
|
29
|
+
refute test_user.valid?
|
30
|
+
assert test_user.errors[:name].include?("is reserved")
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_custom_message_on_error
|
34
|
+
test_user = TestUserWithMessage.new(name: "help")
|
35
|
+
refute test_user.valid?
|
36
|
+
assert test_user.errors[:name].include?("is not part of the whitelist")
|
37
|
+
end
|
38
|
+
|
39
|
+
#######################
|
40
|
+
### Private methods ###
|
41
|
+
#######################
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def names_that_should_be_reserved
|
46
|
+
["about", "faq", "help"]
|
47
|
+
end
|
48
|
+
|
49
|
+
def names_that_should_be_not_reserved
|
50
|
+
["daftpunk", "u2", "madonna"]
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
data/valid8ors.gemspec
CHANGED
@@ -3,9 +3,9 @@ $:.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.
|
7
|
-
s.authors = ["Axel Vergult", "Vincent Pochet"]
|
8
|
-
s.email = ["axel@official.fm", "vincent@official.fm"]
|
6
|
+
s.version = "0.0.4"
|
7
|
+
s.authors = ["Axel Vergult", "Vincent Pochet", "Ben Colon"]
|
8
|
+
s.email = ["axel@official.fm", "vincent@official.fm", "ben@official.fm"]
|
9
9
|
s.homepage = ""
|
10
10
|
s.summary = %q{Rails 3 awesome custom validators}
|
11
11
|
s.description = %q{Rails 3 awesome custom validators}
|
metadata
CHANGED
@@ -1,20 +1,21 @@
|
|
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.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Axel Vergult
|
9
9
|
- Vincent Pochet
|
10
|
+
- Ben Colon
|
10
11
|
autorequire:
|
11
12
|
bindir: bin
|
12
13
|
cert_chain: []
|
13
|
-
date:
|
14
|
+
date: 2012-01-17 00:00:00.000000000Z
|
14
15
|
dependencies:
|
15
16
|
- !ruby/object:Gem::Dependency
|
16
17
|
name: activemodel
|
17
|
-
requirement: &
|
18
|
+
requirement: &70192395358060 !ruby/object:Gem::Requirement
|
18
19
|
none: false
|
19
20
|
requirements:
|
20
21
|
- - ! '>='
|
@@ -22,11 +23,12 @@ dependencies:
|
|
22
23
|
version: '0'
|
23
24
|
type: :runtime
|
24
25
|
prerelease: false
|
25
|
-
version_requirements: *
|
26
|
+
version_requirements: *70192395358060
|
26
27
|
description: Rails 3 awesome custom validators
|
27
28
|
email:
|
28
29
|
- axel@official.fm
|
29
30
|
- vincent@official.fm
|
31
|
+
- ben@official.fm
|
30
32
|
executables: []
|
31
33
|
extensions: []
|
32
34
|
extra_rdoc_files: []
|
@@ -37,12 +39,15 @@ files:
|
|
37
39
|
- README.md
|
38
40
|
- Rakefile
|
39
41
|
- config/blacklist.yml
|
42
|
+
- config/reserved.yml
|
40
43
|
- lib/valid8ors.rb
|
41
44
|
- lib/valid8ors/blacklist.rb
|
42
45
|
- lib/valid8ors/email_format.rb
|
46
|
+
- lib/valid8ors/reserved.rb
|
43
47
|
- lib/valid8ors/url_format.rb
|
44
48
|
- test/blacklist_test.rb
|
45
49
|
- test/email_format_test.rb
|
50
|
+
- test/reserved_test.rb
|
46
51
|
- test/test_helper.rb
|
47
52
|
- test/url_format_test.rb
|
48
53
|
- valid8ors.gemspec
|
@@ -73,5 +78,6 @@ summary: Rails 3 awesome custom validators
|
|
73
78
|
test_files:
|
74
79
|
- test/blacklist_test.rb
|
75
80
|
- test/email_format_test.rb
|
81
|
+
- test/reserved_test.rb
|
76
82
|
- test/test_helper.rb
|
77
83
|
- test/url_format_test.rb
|