more_validators 0.1.1
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/CHANGELOG +7 -0
- data/LICENSE +5 -0
- data/README +51 -0
- data/Rakefile +22 -0
- data/init.rb +1 -0
- data/lib/validates_as_.rb +6 -0
- data/lib/validates_as_cep.rb +15 -0
- data/lib/validates_as_cnpj.rb +70 -0
- data/lib/validates_as_cpf.rb +68 -0
- data/lib/validates_as_email.rb +58 -0
- data/lib/validates_as_phonenumber_br.rb +15 -0
- data/lib/validates_as_website.rb +15 -0
- data/test/validates_as_cep_test.rb +45 -0
- data/test/validates_as_cnpj_test.rb +48 -0
- data/test/validates_as_cpf_test.rb +49 -0
- data/test/validates_as_email_test.rb +47 -0
- data/test/validates_as_phonenumber_br_test.rb +51 -0
- data/test/validates_as_website_test.rb +48 -0
- metadata +87 -0
data/CHANGELOG
ADDED
data/LICENSE
ADDED
data/README
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
MoreValidators
|
2
|
+
==============
|
3
|
+
|
4
|
+
Rails gem/plugin is a series of validators provided by me or other people:
|
5
|
+
|
6
|
+
ValidateAsEmail: http://github.com/gbdev/validates_as_email
|
7
|
+
|
8
|
+
It's released under Creative Commons Attribution-Share Alike 3.0 Unported License.
|
9
|
+
|
10
|
+
Talk to me if you have any questions:
|
11
|
+
Rafael Barbolo: rafael@barbolo.com.br
|
12
|
+
http://twitter.com/barbolo
|
13
|
+
|
14
|
+
Installation:
|
15
|
+
=============
|
16
|
+
Install the gem(s):
|
17
|
+
gem install more_validators
|
18
|
+
|
19
|
+
Add to environment.rb initializer block:
|
20
|
+
config.gem 'more_validators', :version => '>=0.1.0', :lib => 'validates_as_'
|
21
|
+
|
22
|
+
Usage:
|
23
|
+
======
|
24
|
+
In your model file do something like:
|
25
|
+
|
26
|
+
class MyClass < ActiveRecord::Base
|
27
|
+
|
28
|
+
# Email validation
|
29
|
+
validates_as_email :email, :message => 'Invalid', :allow_nil => true
|
30
|
+
|
31
|
+
# Brazilian postal code validation
|
32
|
+
validates_as_cep :cep, :message => 'Invalid', :allow_nil => true
|
33
|
+
|
34
|
+
# Brazilian phone number validation
|
35
|
+
validates_as_phonenumber_br :phonenumber, :message => 'Invalid', :allow_nil => true
|
36
|
+
|
37
|
+
# CPF validation
|
38
|
+
validates_as_cpf :cpf, :message => 'Invalid', :allow_nil => true
|
39
|
+
|
40
|
+
# CNPJ validation
|
41
|
+
validates_as_cnpj :cnpj, :message => 'Invalid', :allow_nil => true
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
Tests:
|
46
|
+
======
|
47
|
+
Some tests have been added to each validator.
|
48
|
+
|
49
|
+
License:
|
50
|
+
========
|
51
|
+
See the LICENSE file.
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the more_validators plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the more_validators plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'MoreValidators'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'validates_as_'
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Validations
|
3
|
+
module ClassMethods
|
4
|
+
def validates_as_cep(*attr_names)
|
5
|
+
configuration = {
|
6
|
+
:message => I18n.translate('activerecord.errors.messages.invalid', :default => 'invalid' ),
|
7
|
+
:with => /\A(\d{5})([-]{0,1})(\d{3})\Z/,
|
8
|
+
:allow_nil => true }
|
9
|
+
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
|
10
|
+
|
11
|
+
validates_format_of attr_names, configuration
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module CNPJ
|
2
|
+
BLACK_LIST = %w(00000000000000 11111111111111 22222222222222 33333333333333
|
3
|
+
44444444444444 55555555555555 66666666666666 77777777777777
|
4
|
+
88888888888888 99999999999999)
|
5
|
+
|
6
|
+
def self.valid?(cnpj)
|
7
|
+
cnpj = cnpj.to_s
|
8
|
+
|
9
|
+
# could be 13 or 14 digits or with mask 99.999.999/9999-99
|
10
|
+
if cnpj !~ /^\d{13,14}$|\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/
|
11
|
+
return false
|
12
|
+
end
|
13
|
+
|
14
|
+
cnpj = cnpj.scan(/\d/).collect(&:to_i)
|
15
|
+
cnpj.unshift(0) if cnpj.length == 13
|
16
|
+
|
17
|
+
# filter black list
|
18
|
+
if BLACK_LIST.include? cnpj.join
|
19
|
+
return false
|
20
|
+
end
|
21
|
+
|
22
|
+
# calculate first digit
|
23
|
+
factor = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
|
24
|
+
|
25
|
+
sum = (0..11).inject(0) do |sum, i|
|
26
|
+
sum + cnpj[i] * factor[i]
|
27
|
+
end
|
28
|
+
|
29
|
+
result = sum % 11
|
30
|
+
result = result < 2 ? 0 : 11 - result
|
31
|
+
|
32
|
+
if result != cnpj[12]
|
33
|
+
return false
|
34
|
+
end
|
35
|
+
|
36
|
+
# calculate second digit
|
37
|
+
factor.unshift 6
|
38
|
+
|
39
|
+
sum = (0..12).inject(0) do |sum, i|
|
40
|
+
sum + cnpj[i] * factor[i]
|
41
|
+
end
|
42
|
+
|
43
|
+
result = sum % 11
|
44
|
+
result = result < 2 ? 0 : 11 - result
|
45
|
+
|
46
|
+
result == cnpj[13]
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.invalid?(cnpj)
|
50
|
+
!valid?(cnpj)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
module ActiveRecord
|
55
|
+
module Validations
|
56
|
+
module ClassMethods
|
57
|
+
def validates_as_cnpj(*attr_names)
|
58
|
+
configuration = {
|
59
|
+
:message => I18n.translate('activerecord.errors.messages.invalid', :default => 'invalid' ),
|
60
|
+
:allow_nil => true }
|
61
|
+
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
|
62
|
+
|
63
|
+
validates_each attr_names, configuration do |record, attribute, value|
|
64
|
+
record.errors.add(attribute, configuration[:message]) if CNPJ.invalid?(value)
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module CPF
|
2
|
+
BLACK_LIST = %w(12345678909 11111111111 22222222222 33333333333 44444444444
|
3
|
+
55555555555 66666666666 77777777777 88888888888 99999999999
|
4
|
+
00000000000)
|
5
|
+
|
6
|
+
def self.valid?(cpf)
|
7
|
+
cpf = cpf.to_s
|
8
|
+
|
9
|
+
# could be 10 or 11 digits or with mask 999.999.999-99
|
10
|
+
if cpf !~ /^\d{10,11}$|\d{3}\.\d{3}\.\d{3}-\d{2}$/
|
11
|
+
return false
|
12
|
+
end
|
13
|
+
|
14
|
+
cpf = cpf.scan(/\d/).collect(&:to_i)
|
15
|
+
cpf.unshift(0) if cpf.length == 10
|
16
|
+
|
17
|
+
# filter black list
|
18
|
+
if BLACK_LIST.include? cpf.join
|
19
|
+
return false
|
20
|
+
end
|
21
|
+
|
22
|
+
# calculate first digit
|
23
|
+
sum = (0..8).inject(0) do |sum, i|
|
24
|
+
sum + cpf[i] * (10 - i)
|
25
|
+
end
|
26
|
+
|
27
|
+
result = sum % 11
|
28
|
+
result = result < 2 ? 0 : 11 - result
|
29
|
+
|
30
|
+
if result != cpf[9]
|
31
|
+
return false
|
32
|
+
end
|
33
|
+
|
34
|
+
# calculate second digit
|
35
|
+
sum = (0..8).inject(0) do |sum, i|
|
36
|
+
sum + cpf[i] * (11 - i)
|
37
|
+
end
|
38
|
+
|
39
|
+
sum += cpf[9] * 2
|
40
|
+
|
41
|
+
result = sum % 11
|
42
|
+
result = result < 2 ? 0 : 11 - result
|
43
|
+
|
44
|
+
result == cpf[10]
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.invalid?(cpf)
|
48
|
+
!valid?(cpf)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
module ActiveRecord
|
53
|
+
module Validations
|
54
|
+
module ClassMethods
|
55
|
+
def validates_as_cpf(*attr_names)
|
56
|
+
configuration = {
|
57
|
+
:message => I18n.translate('activerecord.errors.messages.invalid', :default => 'invalid' ),
|
58
|
+
:allow_nil => true }
|
59
|
+
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
|
60
|
+
|
61
|
+
validates_each attr_names, configuration do |record, attribute, value|
|
62
|
+
record.errors.add(attribute, configuration[:message]) if CPF.invalid?(value)
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
#
|
2
|
+
# RFC822 Email Address Regex
|
3
|
+
# --------------------------
|
4
|
+
#
|
5
|
+
# Originally written by Cal Henderson
|
6
|
+
# c.f. http://iamcal.com/publish/articles/php/parsing_email/
|
7
|
+
#
|
8
|
+
# Translated to Ruby by Tim Fletcher, with changes suggested by Dan Kubb.
|
9
|
+
#
|
10
|
+
# Licensed under a Creative Commons Attribution-ShareAlike 2.5 License
|
11
|
+
# http://creativecommons.org/licenses/by-sa/2.5/
|
12
|
+
#
|
13
|
+
module RFC822
|
14
|
+
EmailAddress = begin
|
15
|
+
qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
|
16
|
+
dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'
|
17
|
+
atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-' +
|
18
|
+
'\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
|
19
|
+
quoted_pair = '\\x5c[\\x00-\\x7f]'
|
20
|
+
domain_literal = "\\x5b(?:#{dtext}|#{quoted_pair})*\\x5d"
|
21
|
+
quoted_string = "\\x22(?:#{qtext}|#{quoted_pair})*\\x22"
|
22
|
+
domain_ref = atom
|
23
|
+
sub_domain = "(?:#{domain_ref}|#{domain_literal})"
|
24
|
+
word = "(?:#{atom}|#{quoted_string})"
|
25
|
+
domain = "#{sub_domain}(?:\\x2e#{sub_domain})*"
|
26
|
+
local_part = "#{word}(?:\\x2e#{word})*"
|
27
|
+
addr_spec = "#{local_part}\\x40#{domain}"
|
28
|
+
pattern = /\A#{addr_spec}\z/
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Validation helper for ActiveRecord derived objects that cleanly and simply
|
33
|
+
# allows the model to check if the given string is a syntactically valid email
|
34
|
+
# address (by using the RFC822 module above).
|
35
|
+
#
|
36
|
+
# Original code by Ximon Eighteen <ximon.eightee@int.greenpeace.org> which was
|
37
|
+
# heavily based on code I can no longer find on the net, my apologies to the
|
38
|
+
# author!
|
39
|
+
#
|
40
|
+
# Huge credit goes to Dan Kubb <dan.kubb@autopilotmarketing.com> for
|
41
|
+
# submitting a patch to massively simplify this code and thereby instruct me
|
42
|
+
# in the ways of Rails too! I reflowed the patch a little to keep the line
|
43
|
+
# length to a maximum of 78 characters, an old habit.
|
44
|
+
module ActiveRecord
|
45
|
+
module Validations
|
46
|
+
module ClassMethods
|
47
|
+
def validates_as_email(*attr_names)
|
48
|
+
configuration = {
|
49
|
+
:message => I18n.translate('activerecord.errors.messages.invalid', :default => 'invalid' ),
|
50
|
+
:with => RFC822::EmailAddress,
|
51
|
+
:allow_nil => true }
|
52
|
+
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
|
53
|
+
|
54
|
+
validates_format_of attr_names, configuration
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Validations
|
3
|
+
module ClassMethods
|
4
|
+
def validates_as_phonenumber_br(*attr_names)
|
5
|
+
configuration = {
|
6
|
+
:message => I18n.translate('activerecord.errors.messages.invalid', :default => 'invalid' ),
|
7
|
+
:with => /\A([1-9]{2})([- ]{0,1})(\d{3}|\d{4})([- ]{0,1})(\d{4})\Z/,
|
8
|
+
:allow_nil => true }
|
9
|
+
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
|
10
|
+
|
11
|
+
validates_format_of attr_names, configuration
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Validations
|
3
|
+
module ClassMethods
|
4
|
+
def validates_as_website(*attr_names)
|
5
|
+
configuration = {
|
6
|
+
:message => I18n.translate('activerecord.errors.messages.invalid', :default => 'invalid' ),
|
7
|
+
:with => /\A(http|https)[:]\/{2}[^\/.]+([.][^\/.]+)+.*\Z/i,
|
8
|
+
:allow_nil => true }
|
9
|
+
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
|
10
|
+
|
11
|
+
validates_format_of attr_names, configuration
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require File.dirname(__FILE__) + '/../../../../config/boot'
|
5
|
+
require 'active_record'
|
6
|
+
require 'i18n'
|
7
|
+
require 'validates_as_cep'
|
8
|
+
rescue LoadError
|
9
|
+
require 'rubygems'
|
10
|
+
require 'active_record'
|
11
|
+
ActiveRecord::ActiveRecordError # work-around to solve problems with ActiveRecord::Validations
|
12
|
+
require 'i18n'
|
13
|
+
require File.dirname(__FILE__) + '/../lib/validates_as_cep'
|
14
|
+
end
|
15
|
+
|
16
|
+
class TestRecord < ActiveRecord::Base
|
17
|
+
def self.columns; []; end
|
18
|
+
attr_accessor :cep
|
19
|
+
validates_as_cep :cep
|
20
|
+
end
|
21
|
+
|
22
|
+
class ValidatesAsCepTest < Test::Unit::TestCase
|
23
|
+
def test_illegal_cep
|
24
|
+
values = [
|
25
|
+
'1234',
|
26
|
+
'11111111111111111',
|
27
|
+
'name???',
|
28
|
+
'1234567890'
|
29
|
+
]
|
30
|
+
values.each do |value|
|
31
|
+
assert !TestRecord.new(:cep => value).valid?, "#{value} should be illegal."
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_legal_cep
|
36
|
+
values = [
|
37
|
+
'05012-101',
|
38
|
+
'01234-000',
|
39
|
+
'12345-678']
|
40
|
+
|
41
|
+
values.each do |value|
|
42
|
+
assert TestRecord.new(:cep => value).valid?, "#{value} should be legal."
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require File.dirname(__FILE__) + '/../../../../config/boot'
|
5
|
+
require 'active_record'
|
6
|
+
require 'i18n'
|
7
|
+
require 'validates_as_cnpj'
|
8
|
+
rescue LoadError
|
9
|
+
require 'rubygems'
|
10
|
+
require 'active_record'
|
11
|
+
ActiveRecord::ActiveRecordError # work-around to solve problems with ActiveRecord::Validations
|
12
|
+
require 'i18n'
|
13
|
+
require File.dirname(__FILE__) + '/../lib/validates_as_cnpj'
|
14
|
+
end
|
15
|
+
|
16
|
+
class TestRecord < ActiveRecord::Base
|
17
|
+
def self.columns; []; end
|
18
|
+
attr_accessor :cnpj
|
19
|
+
validates_as_cnpj :cnpj
|
20
|
+
end
|
21
|
+
|
22
|
+
class ValidatesAsCNPJTest < Test::Unit::TestCase
|
23
|
+
def test_illegal_cnpj
|
24
|
+
values = [
|
25
|
+
'1234',
|
26
|
+
'11111111111111111',
|
27
|
+
'1.2.3.4.1',
|
28
|
+
'name???',
|
29
|
+
'00000000000',
|
30
|
+
'227.566.442-42',
|
31
|
+
'22756644242',
|
32
|
+
'11111111111111'
|
33
|
+
]
|
34
|
+
values.each do |value|
|
35
|
+
assert !TestRecord.new(:cnpj => value).valid?, "#{value} should be illegal."
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_legal_cnpj
|
40
|
+
values = [
|
41
|
+
'28.261.861/0001-42',
|
42
|
+
'28261861000142']
|
43
|
+
|
44
|
+
values.each do |value|
|
45
|
+
assert TestRecord.new(:cnpj => value).valid?, "#{value} should be legal."
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require File.dirname(__FILE__) + '/../../../../config/boot'
|
5
|
+
require 'active_record'
|
6
|
+
require 'i18n'
|
7
|
+
require 'validates_as_cpf'
|
8
|
+
rescue LoadError
|
9
|
+
require 'rubygems'
|
10
|
+
require 'active_record'
|
11
|
+
ActiveRecord::ActiveRecordError # work-around to solve problems with ActiveRecord::Validations
|
12
|
+
require 'i18n'
|
13
|
+
require File.dirname(__FILE__) + '/../lib/validates_as_cpf'
|
14
|
+
end
|
15
|
+
|
16
|
+
class TestRecord < ActiveRecord::Base
|
17
|
+
def self.columns; []; end
|
18
|
+
attr_accessor :cpf
|
19
|
+
validates_as_cpf :cpf
|
20
|
+
end
|
21
|
+
|
22
|
+
class ValidatesAsCpfTest < Test::Unit::TestCase
|
23
|
+
def test_illegal_cpf
|
24
|
+
values = [
|
25
|
+
'1234',
|
26
|
+
'11111111111111111',
|
27
|
+
'1.2.3.4.1',
|
28
|
+
'name???',
|
29
|
+
'36192118299',
|
30
|
+
'227.556.44242',
|
31
|
+
'227556.442-42',
|
32
|
+
'227556442-42',
|
33
|
+
'00000000000'
|
34
|
+
]
|
35
|
+
values.each do |value|
|
36
|
+
assert !TestRecord.new(:cpf => value).valid?, "#{value} should be illegal."
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_legal_cpf
|
41
|
+
values = [
|
42
|
+
'227.566.442-42',
|
43
|
+
'22756644242']
|
44
|
+
|
45
|
+
values.each do |value|
|
46
|
+
assert TestRecord.new(:cpf => value).valid?, "#{value} should be legal."
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require File.dirname(__FILE__) + '/../../../../config/boot'
|
5
|
+
require 'active_record'
|
6
|
+
require 'i18n'
|
7
|
+
require 'validates_as_email'
|
8
|
+
rescue LoadError
|
9
|
+
require 'rubygems'
|
10
|
+
require 'active_record'
|
11
|
+
ActiveRecord::ActiveRecordError # work-around to solve problems with ActiveRecord::Validations
|
12
|
+
require 'i18n'
|
13
|
+
require File.dirname(__FILE__) + '/../lib/validates_as_email'
|
14
|
+
end
|
15
|
+
|
16
|
+
class TestRecord < ActiveRecord::Base
|
17
|
+
def self.columns; []; end
|
18
|
+
attr_accessor :email
|
19
|
+
validates_as_email :email
|
20
|
+
end
|
21
|
+
|
22
|
+
class ValidatesAsEmailTest < Test::Unit::TestCase
|
23
|
+
def test_illegal_rfc822_email_address
|
24
|
+
addresses = [
|
25
|
+
'Max@Job 3:14',
|
26
|
+
'Job@Book of Job',
|
27
|
+
'J. P. \'s-Gravezande, a.k.a. The Hacker!@example.com',
|
28
|
+
]
|
29
|
+
addresses.each do |address|
|
30
|
+
assert !TestRecord.new(:email => address).valid?, "#{address} should be illegal."
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_legal_rfc822_email_address
|
35
|
+
addresses = [
|
36
|
+
'test@example',
|
37
|
+
'test@example.com',
|
38
|
+
'test@example.co.uk',
|
39
|
+
'"J. P. \'s-Gravezande, a.k.a. The Hacker!"@example.com',
|
40
|
+
'me@[187.223.45.119]',
|
41
|
+
'someone@123.com',
|
42
|
+
]
|
43
|
+
addresses.each do |address|
|
44
|
+
assert TestRecord.new(:email => address).valid?, "#{address} should be legal."
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require File.dirname(__FILE__) + '/../../../../config/boot'
|
5
|
+
require 'active_record'
|
6
|
+
require 'i18n'
|
7
|
+
require 'validates_as_phonenumber_br'
|
8
|
+
rescue LoadError
|
9
|
+
require 'rubygems'
|
10
|
+
require 'active_record'
|
11
|
+
ActiveRecord::ActiveRecordError # work-around to solve problems with ActiveRecord::Validations
|
12
|
+
require 'i18n'
|
13
|
+
require File.dirname(__FILE__) + '/../lib/validates_as_phonenumber_br'
|
14
|
+
end
|
15
|
+
|
16
|
+
class TestRecord < ActiveRecord::Base
|
17
|
+
def self.columns; []; end
|
18
|
+
attr_accessor :phonenumber_br
|
19
|
+
validates_as_phonenumber_br :phonenumber_br
|
20
|
+
end
|
21
|
+
|
22
|
+
class ValidatesAsPhoneNumberBRTest < Test::Unit::TestCase
|
23
|
+
def test_illegal_phonenumber_br
|
24
|
+
values = [
|
25
|
+
'1234',
|
26
|
+
'11111111111111111',
|
27
|
+
'name???',
|
28
|
+
'11/5555-1111',
|
29
|
+
'11555511115'
|
30
|
+
]
|
31
|
+
values.each do |value|
|
32
|
+
assert !TestRecord.new(:phonenumber_br => value).valid?, "#{value} should be illegal."
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_legal_phonenumber_br
|
37
|
+
values = [
|
38
|
+
'11-5555-5555',
|
39
|
+
'11-555-5555',
|
40
|
+
'1155555555',
|
41
|
+
'115555555',
|
42
|
+
'11 5555 5555',
|
43
|
+
'11 555 5555',
|
44
|
+
'11-5555 5555',
|
45
|
+
'11 555-5555']
|
46
|
+
|
47
|
+
values.each do |value|
|
48
|
+
assert TestRecord.new(:phonenumber_br => value).valid?, "#{value} should be legal."
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require File.dirname(__FILE__) + '/../../../../config/boot'
|
5
|
+
require 'active_record'
|
6
|
+
require 'i18n'
|
7
|
+
require 'validates_as_website'
|
8
|
+
rescue LoadError
|
9
|
+
require 'rubygems'
|
10
|
+
require 'active_record'
|
11
|
+
ActiveRecord::ActiveRecordError # work-around to solve problems with ActiveRecord::Validations
|
12
|
+
require 'i18n'
|
13
|
+
require File.dirname(__FILE__) + '/../lib/validates_as_website'
|
14
|
+
end
|
15
|
+
|
16
|
+
class TestRecord < ActiveRecord::Base
|
17
|
+
def self.columns; []; end
|
18
|
+
attr_accessor :website
|
19
|
+
validates_as_website :website
|
20
|
+
end
|
21
|
+
|
22
|
+
class ValidatesAsWebsiteTest < Test::Unit::TestCase
|
23
|
+
def test_illegal_website
|
24
|
+
values = [
|
25
|
+
'http://',
|
26
|
+
'http:///',
|
27
|
+
'http://www',
|
28
|
+
'http://www.'
|
29
|
+
]
|
30
|
+
values.each do |value|
|
31
|
+
assert !TestRecord.new(:website => value).valid?, "#{value} should be illegal."
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_legal_website
|
36
|
+
values = [
|
37
|
+
'http://www.test.com',
|
38
|
+
'http://www.test.com/',
|
39
|
+
'https://test.com',
|
40
|
+
'https://test.com/',
|
41
|
+
'http://www.test.com/test/test/test.html',
|
42
|
+
'https://www.test.com/test/test/test.html?test=test']
|
43
|
+
|
44
|
+
values.each do |value|
|
45
|
+
assert TestRecord.new(:website => value).valid?, "#{value} should be legal."
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: more_validators
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Rafael Barbolo
|
13
|
+
- Ximon Eighteen
|
14
|
+
- Dan Kubb
|
15
|
+
- Thijs van der Vossen
|
16
|
+
autorequire:
|
17
|
+
bindir: bin
|
18
|
+
cert_chain: []
|
19
|
+
|
20
|
+
date: 2010-05-12 00:00:00 -03:00
|
21
|
+
default_executable:
|
22
|
+
dependencies: []
|
23
|
+
|
24
|
+
description: Rails gem/plugin that implements a series of ActiveRecord validation helpers
|
25
|
+
email: rafael@barbolo.com.br
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files: []
|
31
|
+
|
32
|
+
files:
|
33
|
+
- CHANGELOG
|
34
|
+
- LICENSE
|
35
|
+
- README
|
36
|
+
- Rakefile
|
37
|
+
- init.rb
|
38
|
+
- lib/validates_as_.rb
|
39
|
+
- lib/validates_as_cep.rb
|
40
|
+
- lib/validates_as_cnpj.rb
|
41
|
+
- lib/validates_as_cpf.rb
|
42
|
+
- lib/validates_as_email.rb
|
43
|
+
- lib/validates_as_phonenumber_br.rb
|
44
|
+
- lib/validates_as_website.rb
|
45
|
+
- test/validates_as_cep_test.rb
|
46
|
+
- test/validates_as_cnpj_test.rb
|
47
|
+
- test/validates_as_cpf_test.rb
|
48
|
+
- test/validates_as_email_test.rb
|
49
|
+
- test/validates_as_phonenumber_br_test.rb
|
50
|
+
- test/validates_as_website_test.rb
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: http://github.com/barbolo/more_validators
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message: "\n ============================================================\n Thanks for installing MoreValidators!\n ------------------------------------------------------------\n Check it out at http://github.com/barbolo/more_validators\n ============================================================\n "
|
56
|
+
rdoc_options: []
|
57
|
+
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
requirements: []
|
75
|
+
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.3.6
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: Rails gem/plugin that provides a series of validators
|
81
|
+
test_files:
|
82
|
+
- test/validates_as_cep_test.rb
|
83
|
+
- test/validates_as_cnpj_test.rb
|
84
|
+
- test/validates_as_cpf_test.rb
|
85
|
+
- test/validates_as_email_test.rb
|
86
|
+
- test/validates_as_phonenumber_br_test.rb
|
87
|
+
- test/validates_as_website_test.rb
|