more_validators 0.1.3 → 0.2.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/CHANGELOG +5 -2
- data/MIT-LICENSE +20 -0
- data/README.rdoc +99 -0
- data/init.rb +1 -1
- data/lib/{validates_as_.rb → more_validators.rb} +1 -1
- data/lib/validates_as_cep.rb +11 -8
- data/lib/validates_as_cnpj.rb +60 -53
- data/lib/validates_as_cpf.rb +63 -54
- data/lib/validates_as_email.rb +11 -51
- data/lib/validates_as_phonenumber_br.rb +11 -8
- data/lib/validates_as_uf_br.rb +28 -25
- data/lib/validates_as_website.rb +11 -8
- data/test/validates_as_cep_test.rb +11 -4
- data/test/validates_as_cnpj_test.rb +13 -3
- data/test/validates_as_cpf_test.rb +4 -3
- data/test/validates_as_email_test.rb +8 -11
- data/test/validates_as_phonenumber_br_test.rb +4 -3
- data/test/validates_as_uf_br_test.rb +2 -2
- data/test/validates_as_website_test.rb +4 -3
- metadata +17 -15
- data/LICENSE +0 -5
- data/README +0 -59
data/CHANGELOG
CHANGED
@@ -1,8 +1,11 @@
|
|
1
|
+
0.2.0 - Apr 14th 2011
|
2
|
+
* more_validators version >= 0.2.x is now compatible with Rails 3 ONLY
|
3
|
+
|
1
4
|
0.1.3 - May 14th 2010
|
2
|
-
*
|
5
|
+
* Added allow_blank => true as default behaviour for validators
|
3
6
|
|
4
7
|
0.1.2 - May 13th 2010
|
5
|
-
*
|
8
|
+
* New validators:
|
6
9
|
** validates_as_uf_br
|
7
10
|
|
8
11
|
0.1.0 - May 12th 2010
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (C) 2011 by Infosimples. http://www.infosimples.com.br/
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
== MoreValidators
|
2
|
+
|
3
|
+
MoreValidators is a Rails gem that provides a series of extra validators.
|
4
|
+
|
5
|
+
The latest version of this library provides the following validation methods:
|
6
|
+
|
7
|
+
* validates_as_cep: brazilian postal code validation;
|
8
|
+
* validates_as_cnpj: brazilian CNPJ validation;
|
9
|
+
* validates_as_cpf: brazilizan CPF validation;
|
10
|
+
* validates_as_email: email address validation;
|
11
|
+
* validates_as_phonenumber_br: brazilian phone number validation;
|
12
|
+
* validates_as_uf_br: brazilian state validation;
|
13
|
+
* validates_as_website: website URL validation.
|
14
|
+
|
15
|
+
Contact us if you have any questions:
|
16
|
+
contato@infosimples.com.br
|
17
|
+
|
18
|
+
== Latest version:
|
19
|
+
|
20
|
+
Version: 0.2.0
|
21
|
+
|
22
|
+
== Installation:
|
23
|
+
|
24
|
+
The latest version of MoreValidators is compatible with Rails 3 ONLY. However, it's possible to use this gem with Rails 2; if that's what you need, follow the installation instructions on the Rails 2 section below.
|
25
|
+
|
26
|
+
=== Rails 3:
|
27
|
+
|
28
|
+
You can install the latest version of MoreValidators with:
|
29
|
+
|
30
|
+
gem install more_validators
|
31
|
+
|
32
|
+
You can add it to your Gemfile:
|
33
|
+
|
34
|
+
gem 'more_validators'
|
35
|
+
|
36
|
+
=== Rails 2:
|
37
|
+
|
38
|
+
You can install MoreValidators with:
|
39
|
+
|
40
|
+
gem install more_validators --version=0.1.3
|
41
|
+
|
42
|
+
You can add it to your Gemfile:
|
43
|
+
|
44
|
+
gem 'more_validators', '0.1.3'
|
45
|
+
|
46
|
+
== Usage:
|
47
|
+
|
48
|
+
In your model file do something like:
|
49
|
+
|
50
|
+
class MyClass < ActiveRecord::Base
|
51
|
+
|
52
|
+
# Email validation
|
53
|
+
validates_as_email :email
|
54
|
+
|
55
|
+
# Brazilian postal code validation
|
56
|
+
validates_as_cep :cep, :allow_blank => true
|
57
|
+
|
58
|
+
# Brazilian phone number validation
|
59
|
+
validates_as_phonenumber_br :phonenumber, :allow_nil => true
|
60
|
+
|
61
|
+
# CPF validation
|
62
|
+
validates_as_cpf :cpf
|
63
|
+
|
64
|
+
# CNPJ validation
|
65
|
+
validates_as_cnpj :cnpj :allow_blank => true
|
66
|
+
|
67
|
+
# Brazilian UF validation
|
68
|
+
# Both upcase and downcase are allowed by default
|
69
|
+
validates_as_uf_br :uf
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
== I18n
|
74
|
+
|
75
|
+
The default error message is "is invalid".
|
76
|
+
You can customize this message via Rails I18n defining the key :invalid.
|
77
|
+
|
78
|
+
=== Example
|
79
|
+
|
80
|
+
Your customized en.yaml file would look like this:
|
81
|
+
|
82
|
+
en:
|
83
|
+
activerecord:
|
84
|
+
errors:
|
85
|
+
messages:
|
86
|
+
invalid: "MY CUSTOM MESSAGE"
|
87
|
+
|
88
|
+
== Tests:
|
89
|
+
|
90
|
+
Some tests have been added to each validator.
|
91
|
+
|
92
|
+
=== Maintainers
|
93
|
+
|
94
|
+
* Rafael Barbolo Lopes (http://github.com/barbolo)
|
95
|
+
* Rafael Ivan Garcia (http://github.com/rafaelivan)
|
96
|
+
|
97
|
+
== License:
|
98
|
+
|
99
|
+
MIT License. Copyright (C) 2011 by Infosimples. http://www.infosimples.com.br
|
data/init.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require '
|
1
|
+
require 'more_validators'
|
data/lib/validates_as_cep.rb
CHANGED
@@ -1,15 +1,18 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module Validations
|
3
|
+
|
4
|
+
class CEPValidator < ActiveModel::EachValidator
|
5
|
+
def validate_each(record, attribute, value)
|
6
|
+
return if not (value.is_a?(String) ? value : value.to_s).match(/\A(\d{5})([-]{0,1})(\d{3})\Z/).nil?
|
7
|
+
record.errors.add(attribute,
|
8
|
+
I18n.translate('activerecord.errors.messages.invalid', :default => 'invalid'),
|
9
|
+
options.merge(:value => value))
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
3
13
|
module ClassMethods
|
4
14
|
def validates_as_cep(*attr_names)
|
5
|
-
|
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
|
-
:allow_blank => true }
|
10
|
-
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
|
11
|
-
|
12
|
-
validates_format_of attr_names, configuration
|
15
|
+
validates_with CEPValidator, _merge_attributes(attr_names)
|
13
16
|
end
|
14
17
|
end
|
15
18
|
end
|
data/lib/validates_as_cnpj.rb
CHANGED
@@ -1,71 +1,78 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
module ActiveRecord
|
2
|
+
module Validations
|
3
|
+
|
4
|
+
class CNPJValidator < ActiveModel::EachValidator
|
5
|
+
@@black_list = %w(00000000000000 11111111111111 22222222222222 33333333333333
|
6
|
+
44444444444444 55555555555555 66666666666666 77777777777777
|
7
|
+
88888888888888 99999999999999)
|
7
8
|
|
8
|
-
|
9
|
+
def validate_each(record, attribute, value)
|
10
|
+
return if valid? value
|
11
|
+
record.errors.add(attribute,
|
12
|
+
I18n.translate('activerecord.errors.messages.invalid', :default => 'invalid'),
|
13
|
+
options.merge(:value => value))
|
14
|
+
end
|
15
|
+
|
16
|
+
#
|
17
|
+
# Private methods.
|
18
|
+
#
|
19
|
+
private
|
20
|
+
|
21
|
+
def valid? cnpj
|
22
|
+
begin
|
23
|
+
cnpj = (cnpj.is_a?(String) ? cnpj : cnpj.to_s)
|
24
|
+
rescue Exception => exc
|
25
|
+
return false
|
26
|
+
end
|
9
27
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
28
|
+
# could be 13 or 14 digits or with mask 99.999.999/9999-99
|
29
|
+
if cnpj !~ /^\d{13,14}$|\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/
|
30
|
+
return false
|
31
|
+
end
|
14
32
|
|
15
|
-
|
16
|
-
|
33
|
+
cnpj = cnpj.scan(/\d/).collect(&:to_i)
|
34
|
+
cnpj.unshift(0) if cnpj.length == 13
|
17
35
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
36
|
+
# filter black list
|
37
|
+
if @@black_list.include? cnpj.join
|
38
|
+
return false
|
39
|
+
end
|
22
40
|
|
23
|
-
|
24
|
-
|
41
|
+
# calculate first digit
|
42
|
+
factor = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
|
25
43
|
|
26
|
-
|
27
|
-
|
28
|
-
|
44
|
+
sum = (0..11).inject(0) do |sum, i|
|
45
|
+
sum + cnpj[i] * factor[i]
|
46
|
+
end
|
29
47
|
|
30
|
-
|
31
|
-
|
48
|
+
result = sum % 11
|
49
|
+
result = result < 2 ? 0 : 11 - result
|
32
50
|
|
33
|
-
|
34
|
-
|
35
|
-
|
51
|
+
if result != cnpj[12]
|
52
|
+
return false
|
53
|
+
end
|
36
54
|
|
37
|
-
|
38
|
-
|
55
|
+
# calculate second digit
|
56
|
+
factor.unshift 6
|
39
57
|
|
40
|
-
|
41
|
-
|
42
|
-
|
58
|
+
sum = (0..12).inject(0) do |sum, i|
|
59
|
+
sum + cnpj[i] * factor[i]
|
60
|
+
end
|
43
61
|
|
44
|
-
|
45
|
-
|
62
|
+
result = sum % 11
|
63
|
+
result = result < 2 ? 0 : 11 - result
|
46
64
|
|
47
|
-
|
48
|
-
|
65
|
+
result == cnpj[13]
|
66
|
+
end
|
67
|
+
|
68
|
+
def invalid? cnpj
|
69
|
+
not valid? cnpj
|
70
|
+
end
|
71
|
+
end
|
49
72
|
|
50
|
-
def self.invalid?(cnpj)
|
51
|
-
!valid?(cnpj)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
module ActiveRecord
|
56
|
-
module Validations
|
57
73
|
module ClassMethods
|
58
74
|
def validates_as_cnpj(*attr_names)
|
59
|
-
|
60
|
-
:message => I18n.translate('activerecord.errors.messages.invalid', :default => 'invalid' ),
|
61
|
-
:allow_nil => true,
|
62
|
-
:allow_blank => true }
|
63
|
-
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
|
64
|
-
|
65
|
-
validates_each attr_names, configuration do |record, attribute, value|
|
66
|
-
record.errors.add(attribute, configuration[:message]) if CNPJ.invalid?(value)
|
67
|
-
end
|
68
|
-
|
75
|
+
validates_with CNPJValidator, _merge_attributes(attr_names)
|
69
76
|
end
|
70
77
|
end
|
71
78
|
end
|
data/lib/validates_as_cpf.rb
CHANGED
@@ -1,68 +1,77 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
1
|
+
module ActiveRecord
|
2
|
+
module Validations
|
3
|
+
|
4
|
+
class CPFValidator < ActiveModel::EachValidator
|
5
|
+
@@black_list = %w(12345678909 11111111111 22222222222 33333333333 44444444444
|
6
|
+
55555555555 66666666666 77777777777 88888888888 99999999999
|
7
|
+
00000000000)
|
8
|
+
|
9
|
+
def validate_each(record, attribute, value)
|
10
|
+
return if valid? value
|
11
|
+
record.errors.add(attribute,
|
12
|
+
I18n.translate('activerecord.errors.messages.invalid', :default => 'invalid'),
|
13
|
+
options.merge(:value => value))
|
14
|
+
end
|
15
|
+
|
16
|
+
#
|
17
|
+
# Private methods.
|
18
|
+
#
|
19
|
+
private
|
20
|
+
|
21
|
+
def valid? cpf
|
22
|
+
begin
|
23
|
+
cpf = (cpf.is_a?(String) ? cpf : cpf.to_s)
|
24
|
+
rescue Exception => exc
|
25
|
+
return false
|
26
|
+
end
|
27
|
+
|
28
|
+
# could be 10 or 11 digits or with mask 999.999.999-99
|
29
|
+
if cpf !~ /^\d{10,11}$|\d{3}\.\d{3}\.\d{3}-\d{2}$/
|
30
|
+
return false
|
31
|
+
end
|
16
32
|
|
17
|
-
|
18
|
-
|
19
|
-
return false
|
20
|
-
end
|
33
|
+
cpf = cpf.scan(/\d/).collect(&:to_i)
|
34
|
+
cpf.unshift(0) if cpf.length == 10
|
21
35
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
36
|
+
# filter black list
|
37
|
+
if @@black_list.include? cpf.join
|
38
|
+
return false
|
39
|
+
end
|
26
40
|
|
27
|
-
|
28
|
-
|
41
|
+
# calculate first digit
|
42
|
+
sum = (0..8).inject(0) do |sum, i|
|
43
|
+
sum + cpf[i] * (10 - i)
|
44
|
+
end
|
29
45
|
|
30
|
-
|
31
|
-
|
32
|
-
end
|
46
|
+
result = sum % 11
|
47
|
+
result = result < 2 ? 0 : 11 - result
|
33
48
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
end
|
49
|
+
if result != cpf[9]
|
50
|
+
return false
|
51
|
+
end
|
38
52
|
|
39
|
-
|
53
|
+
# calculate second digit
|
54
|
+
sum = (0..8).inject(0) do |sum, i|
|
55
|
+
sum + cpf[i] * (11 - i)
|
56
|
+
end
|
40
57
|
|
41
|
-
|
42
|
-
result = result < 2 ? 0 : 11 - result
|
58
|
+
sum += cpf[9] * 2
|
43
59
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
def self.invalid?(cpf)
|
48
|
-
!valid?(cpf)
|
49
|
-
end
|
50
|
-
end
|
60
|
+
result = sum % 11
|
61
|
+
result = result < 2 ? 0 : 11 - result
|
51
62
|
|
52
|
-
|
53
|
-
|
63
|
+
result == cpf[10]
|
64
|
+
end
|
65
|
+
|
66
|
+
def invalid? cpf
|
67
|
+
not valid? cpf
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
54
72
|
module ClassMethods
|
55
73
|
def validates_as_cpf(*attr_names)
|
56
|
-
|
57
|
-
:message => I18n.translate('activerecord.errors.messages.invalid', :default => 'invalid' ),
|
58
|
-
:allow_nil => true,
|
59
|
-
:allow_blank => true }
|
60
|
-
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
|
61
|
-
|
62
|
-
validates_each attr_names, configuration do |record, attribute, value|
|
63
|
-
record.errors.add(attribute, configuration[:message]) if CPF.invalid?(value)
|
64
|
-
end
|
65
|
-
|
74
|
+
validates_with CPFValidator, _merge_attributes(attr_names)
|
66
75
|
end
|
67
76
|
end
|
68
77
|
end
|
data/lib/validates_as_email.rb
CHANGED
@@ -1,58 +1,18 @@
|
|
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
1
|
module ActiveRecord
|
45
2
|
module Validations
|
3
|
+
|
4
|
+
class EmailValidator < ActiveModel::EachValidator
|
5
|
+
def validate_each(record, attribute, value)
|
6
|
+
return if not (value.is_a?(String) ? value : value.to_s).match(/\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i).nil?
|
7
|
+
record.errors.add(attribute,
|
8
|
+
I18n.translate('activerecord.errors.messages.invalid', :default => 'invalid'),
|
9
|
+
options.merge(:value => value))
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
46
13
|
module ClassMethods
|
47
14
|
def validates_as_email(*attr_names)
|
48
|
-
|
49
|
-
:message => I18n.translate('activerecord.errors.messages.invalid', :default => 'invalid' ),
|
50
|
-
:with => RFC822::EmailAddress,
|
51
|
-
:allow_nil => true,
|
52
|
-
:allow_blank => true }
|
53
|
-
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
|
54
|
-
|
55
|
-
validates_format_of attr_names, configuration
|
15
|
+
validates_with EmailValidator, _merge_attributes(attr_names)
|
56
16
|
end
|
57
17
|
end
|
58
18
|
end
|
@@ -1,15 +1,18 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module Validations
|
3
|
+
|
4
|
+
class PhoneNumberBrValidator < ActiveModel::EachValidator
|
5
|
+
def validate_each(record, attribute, value)
|
6
|
+
return if not (value.is_a?(String) ? value : value.to_s).match(/\A([1-9]{2})([- ]{0,1})(\d{3}|\d{4})([- ]{0,1})(\d{4})\Z/).nil?
|
7
|
+
record.errors.add(attribute,
|
8
|
+
I18n.translate('activerecord.errors.messages.invalid', :default => 'invalid'),
|
9
|
+
options.merge(:value => value))
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
3
13
|
module ClassMethods
|
4
14
|
def validates_as_phonenumber_br(*attr_names)
|
5
|
-
|
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
|
-
:allow_blank => true }
|
10
|
-
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
|
11
|
-
|
12
|
-
validates_format_of attr_names, configuration
|
15
|
+
validates_with PhoneNumberBrValidator, _merge_attributes(attr_names)
|
13
16
|
end
|
14
17
|
end
|
15
18
|
end
|
data/lib/validates_as_uf_br.rb
CHANGED
@@ -1,32 +1,35 @@
|
|
1
|
-
module UF_BR
|
2
|
-
LIST_UP = %w(AC AL AM AP BA CE DF ES GO MA MG MS MT PA PB PE PI PR RJ RN RO RR RS SC SE SP TO)
|
3
|
-
LIST_DOWN = %w(ac al am ap ba ce df es go ma mg ms mt pa pb pe pi pr rj rn ro rr rs sc se sp to)
|
4
|
-
|
5
|
-
def self.valid?(uf_br, allow_up = true, allow_down = true)
|
6
|
-
(allow_up and LIST_UP.include? uf_br) or (allow_down and LIST_DOWN.include? uf_br)
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.invalid?(uf_br, allow_up = true, allow_down = true)
|
10
|
-
!valid?(uf_br, allow_up, allow_down)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
1
|
module ActiveRecord
|
15
2
|
module Validations
|
3
|
+
|
4
|
+
class UFBRValidator < ActiveModel::EachValidator
|
5
|
+
|
6
|
+
@@ufs = %w(AC AL AM AP BA CE DF ES GO MA MG MS MT PA PB PE PI PR RJ RN RO RR RS SC SE SP TO)
|
7
|
+
|
8
|
+
def validate_each(record, attribute, value)
|
9
|
+
return if valid? value
|
10
|
+
record.errors.add(attribute,
|
11
|
+
I18n.translate('activerecord.errors.messages.invalid', :default => 'invalid'),
|
12
|
+
options.merge(:value => value))
|
13
|
+
end
|
14
|
+
|
15
|
+
#
|
16
|
+
# Private methods.
|
17
|
+
#
|
18
|
+
private
|
19
|
+
|
20
|
+
def valid? uf_br
|
21
|
+
@@ufs.include? (uf_br.is_a?(String) ? uf_br.upcase : uf_br.to_s.upcase)
|
22
|
+
end
|
23
|
+
|
24
|
+
def invalid? uf_br
|
25
|
+
not valid? uf_br
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
16
30
|
module ClassMethods
|
17
31
|
def validates_as_uf_br(*attr_names)
|
18
|
-
|
19
|
-
:message => I18n.translate('activerecord.errors.messages.invalid', :default => 'invalid' ),
|
20
|
-
:allow_nil => true,
|
21
|
-
:allow_blank => true,
|
22
|
-
:allow_down => true,
|
23
|
-
:allow_up => true }
|
24
|
-
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
|
25
|
-
|
26
|
-
validates_each attr_names, configuration do |record, attribute, value|
|
27
|
-
record.errors.add(attribute, configuration[:message]) if UF_BR.invalid?(value, configuration[:allow_up], configuration[:allow_down])
|
28
|
-
end
|
29
|
-
|
32
|
+
validates_with UFBRValidator, _merge_attributes(attr_names)
|
30
33
|
end
|
31
34
|
end
|
32
35
|
end
|
data/lib/validates_as_website.rb
CHANGED
@@ -1,15 +1,18 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module Validations
|
3
|
+
|
4
|
+
class WebsiteValidator < ActiveModel::EachValidator
|
5
|
+
def validate_each(record, attribute, value)
|
6
|
+
return if not (value.is_a?(String) ? value : value.to_s).match(/\A(http|https)[:]\/{2}[^\/.]+([.][^\/.]+)+.*\Z/i).nil?
|
7
|
+
record.errors.add(attribute,
|
8
|
+
I18n.translate('activerecord.errors.messages.invalid', :default => 'invalid'),
|
9
|
+
options.merge(:value => value))
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
3
13
|
module ClassMethods
|
4
14
|
def validates_as_website(*attr_names)
|
5
|
-
|
6
|
-
:message => I18n.translate('activerecord.errors.messages.invalid', :default => 'invalid' ),
|
7
|
-
:with => /\A(http|https)[:]\/{2}[^\/.]+([.][^\/.]+)+.*\Z/i,
|
8
|
-
:allow_nil => true,
|
9
|
-
:allow_blank => true }
|
10
|
-
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
|
11
|
-
|
12
|
-
validates_format_of attr_names, configuration
|
15
|
+
validates_with WebsiteValidator, _merge_attributes(attr_names)
|
13
16
|
end
|
14
17
|
end
|
15
18
|
end
|
@@ -22,11 +22,17 @@ end
|
|
22
22
|
class ValidatesAsCepTest < Test::Unit::TestCase
|
23
23
|
def test_illegal_cep
|
24
24
|
values = [
|
25
|
+
nil,
|
26
|
+
'',
|
27
|
+
'0123456',
|
28
|
+
'12-432300',
|
29
|
+
'-',
|
25
30
|
'1234',
|
26
31
|
'11111111111111111',
|
27
32
|
'name???',
|
28
33
|
'1234567890'
|
29
|
-
|
34
|
+
]
|
35
|
+
|
30
36
|
values.each do |value|
|
31
37
|
assert !TestRecord.new(:cep => value).valid?, "#{value} should be illegal."
|
32
38
|
end
|
@@ -36,9 +42,10 @@ class ValidatesAsCepTest < Test::Unit::TestCase
|
|
36
42
|
values = [
|
37
43
|
'05012-101',
|
38
44
|
'01234-000',
|
39
|
-
'12345-678'
|
40
|
-
|
41
|
-
|
45
|
+
'12345-678',
|
46
|
+
'03124891',
|
47
|
+
'93123763'
|
48
|
+
]
|
42
49
|
|
43
50
|
values.each do |value|
|
44
51
|
assert TestRecord.new(:cep => value).valid?, "#{value} should be legal."
|
@@ -22,6 +22,18 @@ end
|
|
22
22
|
class ValidatesAsCNPJTest < Test::Unit::TestCase
|
23
23
|
def test_illegal_cnpj
|
24
24
|
values = [
|
25
|
+
nil,
|
26
|
+
'',
|
27
|
+
'00000000000000',
|
28
|
+
'11111111111111',
|
29
|
+
'22222222222222',
|
30
|
+
'33333333333333',
|
31
|
+
'44444444444444',
|
32
|
+
'55555555555555',
|
33
|
+
'66666666666666',
|
34
|
+
'77777777777777',
|
35
|
+
'88888888888888',
|
36
|
+
'99999999999999',
|
25
37
|
'1234',
|
26
38
|
'11111111111111111',
|
27
39
|
'1.2.3.4.1',
|
@@ -29,7 +41,7 @@ class ValidatesAsCNPJTest < Test::Unit::TestCase
|
|
29
41
|
'00000000000',
|
30
42
|
'227.566.442-42',
|
31
43
|
'22756644242',
|
32
|
-
'
|
44
|
+
'11.111.111/1111-11'
|
33
45
|
]
|
34
46
|
values.each do |value|
|
35
47
|
assert !TestRecord.new(:cnpj => value).valid?, "#{value} should be illegal."
|
@@ -41,8 +53,6 @@ class ValidatesAsCNPJTest < Test::Unit::TestCase
|
|
41
53
|
'28.261.861/0001-42',
|
42
54
|
'28261861000142']
|
43
55
|
|
44
|
-
values.push('')
|
45
|
-
|
46
56
|
values.each do |value|
|
47
57
|
assert TestRecord.new(:cnpj => value).valid?, "#{value} should be legal."
|
48
58
|
end
|
@@ -22,6 +22,8 @@ end
|
|
22
22
|
class ValidatesAsCpfTest < Test::Unit::TestCase
|
23
23
|
def test_illegal_cpf
|
24
24
|
values = [
|
25
|
+
nil,
|
26
|
+
'',
|
25
27
|
'1234',
|
26
28
|
'11111111111111111',
|
27
29
|
'1.2.3.4.1',
|
@@ -40,9 +42,8 @@ class ValidatesAsCpfTest < Test::Unit::TestCase
|
|
40
42
|
def test_legal_cpf
|
41
43
|
values = [
|
42
44
|
'227.566.442-42',
|
43
|
-
'22756644242'
|
44
|
-
|
45
|
-
values.push('')
|
45
|
+
'22756644242'
|
46
|
+
]
|
46
47
|
|
47
48
|
values.each do |value|
|
48
49
|
assert TestRecord.new(:cpf => value).valid?, "#{value} should be legal."
|
@@ -20,28 +20,25 @@ class TestRecord < ActiveRecord::Base
|
|
20
20
|
end
|
21
21
|
|
22
22
|
class ValidatesAsEmailTest < Test::Unit::TestCase
|
23
|
-
def
|
23
|
+
def test_illegal_email_address
|
24
24
|
addresses = [
|
25
|
+
nil,
|
26
|
+
'',
|
25
27
|
'Max@Job 3:14',
|
26
28
|
'Job@Book of Job',
|
27
|
-
'J. P. \'s-Gravezande, a.k.a. The Hacker!@example.com'
|
28
|
-
|
29
|
+
'J. P. \'s-Gravezande, a.k.a. The Hacker!@example.com'
|
30
|
+
]
|
29
31
|
addresses.each do |address|
|
30
32
|
assert !TestRecord.new(:email => address).valid?, "#{address} should be illegal."
|
31
33
|
end
|
32
34
|
end
|
33
35
|
|
34
|
-
def
|
36
|
+
def test_legal_email_address
|
35
37
|
addresses = [
|
36
|
-
'test@example',
|
37
38
|
'test@example.com',
|
38
39
|
'test@example.co.uk',
|
39
|
-
'
|
40
|
-
|
41
|
-
'someone@123.com',
|
42
|
-
]
|
43
|
-
|
44
|
-
addresses.push('')
|
40
|
+
'someone@123.com'
|
41
|
+
]
|
45
42
|
|
46
43
|
addresses.each do |address|
|
47
44
|
assert TestRecord.new(:email => address).valid?, "#{address} should be legal."
|
@@ -22,6 +22,8 @@ end
|
|
22
22
|
class ValidatesAsPhoneNumberBRTest < Test::Unit::TestCase
|
23
23
|
def test_illegal_phonenumber_br
|
24
24
|
values = [
|
25
|
+
nil,
|
26
|
+
'',
|
25
27
|
'1234',
|
26
28
|
'11111111111111111',
|
27
29
|
'name???',
|
@@ -42,9 +44,8 @@ class ValidatesAsPhoneNumberBRTest < Test::Unit::TestCase
|
|
42
44
|
'11 5555 5555',
|
43
45
|
'11 555 5555',
|
44
46
|
'11-5555 5555',
|
45
|
-
'11 555-5555'
|
46
|
-
|
47
|
-
values.push('')
|
47
|
+
'11 555-5555'
|
48
|
+
]
|
48
49
|
|
49
50
|
values.each do |value|
|
50
51
|
assert TestRecord.new(:phonenumber_br => value).valid?, "#{value} should be legal."
|
@@ -22,6 +22,8 @@ end
|
|
22
22
|
class ValidatesAsUFBRTest < Test::Unit::TestCase
|
23
23
|
def test_illegal_uf_br
|
24
24
|
values = [
|
25
|
+
nil,
|
26
|
+
'',
|
25
27
|
'rh',
|
26
28
|
'cc',
|
27
29
|
'sao paulo',
|
@@ -36,8 +38,6 @@ class ValidatesAsUFBRTest < Test::Unit::TestCase
|
|
36
38
|
values = %w(ac al am ap ba ce df es go ma mg ms mt pa pb pe pi pr rj rn ro rr rs sc se sp to
|
37
39
|
AC AL AM AP BA CE DF ES GO MA MG MS MT PA PB PE PI PR RJ RN RO RR RS SC SE SP TO)
|
38
40
|
|
39
|
-
values.push('')
|
40
|
-
|
41
41
|
values.each do |value|
|
42
42
|
assert TestRecord.new(:uf_br => value).valid?, "#{value} should be legal."
|
43
43
|
end
|
@@ -22,6 +22,8 @@ end
|
|
22
22
|
class ValidatesAsWebsiteTest < Test::Unit::TestCase
|
23
23
|
def test_illegal_website
|
24
24
|
values = [
|
25
|
+
nil,
|
26
|
+
'',
|
25
27
|
'http://',
|
26
28
|
'http:///',
|
27
29
|
'http://www',
|
@@ -39,9 +41,8 @@ class ValidatesAsWebsiteTest < Test::Unit::TestCase
|
|
39
41
|
'https://test.com',
|
40
42
|
'https://test.com/',
|
41
43
|
'http://www.test.com/test/test/test.html',
|
42
|
-
'https://www.test.com/test/test/test.html?test=test'
|
43
|
-
|
44
|
-
values.push('')
|
44
|
+
'https://www.test.com/test/test/test.html?test=test'
|
45
|
+
]
|
45
46
|
|
46
47
|
values.each do |value|
|
47
48
|
assert TestRecord.new(:website => value).valid?, "#{value} should be legal."
|
metadata
CHANGED
@@ -4,25 +4,25 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
|
-
- Rafael Barbolo
|
13
|
-
-
|
14
|
-
- Dan Kubb
|
15
|
-
- Thijs van der Vossen
|
12
|
+
- Rafael Barbolo Lopes
|
13
|
+
- Rafael Ivan Garcia
|
16
14
|
autorequire:
|
17
15
|
bindir: bin
|
18
16
|
cert_chain: []
|
19
17
|
|
20
|
-
date:
|
18
|
+
date: 2011-04-14 00:00:00 -03:00
|
21
19
|
default_executable:
|
22
20
|
dependencies: []
|
23
21
|
|
24
22
|
description: Rails gem/plugin that implements a series of ActiveRecord validation helpers
|
25
|
-
email:
|
23
|
+
email:
|
24
|
+
- rafael.barbolo@infosimples.com.br
|
25
|
+
- rafael.ivan@infosimples.com.br
|
26
26
|
executables: []
|
27
27
|
|
28
28
|
extensions: []
|
@@ -31,11 +31,11 @@ extra_rdoc_files: []
|
|
31
31
|
|
32
32
|
files:
|
33
33
|
- CHANGELOG
|
34
|
-
- LICENSE
|
35
|
-
- README
|
34
|
+
- MIT-LICENSE
|
35
|
+
- README.rdoc
|
36
36
|
- Rakefile
|
37
37
|
- init.rb
|
38
|
-
- lib/
|
38
|
+
- lib/more_validators.rb
|
39
39
|
- lib/validates_as_cep.rb
|
40
40
|
- lib/validates_as_cnpj.rb
|
41
41
|
- lib/validates_as_cpf.rb
|
@@ -51,15 +51,16 @@ files:
|
|
51
51
|
- test/validates_as_website_test.rb
|
52
52
|
- test/validates_as_uf_br_test.rb
|
53
53
|
has_rdoc: true
|
54
|
-
homepage: http://github.com/
|
54
|
+
homepage: http://github.com/infosimples/more_validators
|
55
55
|
licenses: []
|
56
56
|
|
57
|
-
post_install_message: "\n ============================================================\n Thanks for installing MoreValidators!\n ------------------------------------------------------------\n Check it out at http://github.com/
|
57
|
+
post_install_message: "\n ============================================================\n Thanks for installing MoreValidators!\n ------------------------------------------------------------\n Check it out at http://github.com/infosimples/more_validators\n ============================================================\n "
|
58
58
|
rdoc_options: []
|
59
59
|
|
60
60
|
require_paths:
|
61
61
|
- lib
|
62
62
|
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
63
64
|
requirements:
|
64
65
|
- - ">="
|
65
66
|
- !ruby/object:Gem::Version
|
@@ -67,6 +68,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
67
68
|
- 0
|
68
69
|
version: "0"
|
69
70
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
70
72
|
requirements:
|
71
73
|
- - ">="
|
72
74
|
- !ruby/object:Gem::Version
|
@@ -76,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
78
|
requirements: []
|
77
79
|
|
78
80
|
rubyforge_project:
|
79
|
-
rubygems_version: 1.3.
|
81
|
+
rubygems_version: 1.3.7
|
80
82
|
signing_key:
|
81
83
|
specification_version: 3
|
82
84
|
summary: Rails gem/plugin that provides a series of validators
|
data/LICENSE
DELETED
data/README
DELETED
@@ -1,59 +0,0 @@
|
|
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', :lib => 'validates_as_'
|
21
|
-
|
22
|
-
LatestVersion
|
23
|
-
--------------
|
24
|
-
Version: 0.1.3
|
25
|
-
--------------
|
26
|
-
|
27
|
-
Usage:
|
28
|
-
======
|
29
|
-
In your model file do something like:
|
30
|
-
|
31
|
-
class MyClass < ActiveRecord::Base
|
32
|
-
|
33
|
-
# Email validation
|
34
|
-
validates_as_email :email, :message => 'Invalid', :allow_nil => true, :allow_blank => true
|
35
|
-
|
36
|
-
# Brazilian postal code validation
|
37
|
-
validates_as_cep :cep, :message => 'Invalid', :allow_nil => true, :allow_blank => true
|
38
|
-
|
39
|
-
# Brazilian phone number validation
|
40
|
-
validates_as_phonenumber_br :phonenumber, :message => 'Invalid', :allow_nil => true, :allow_blank => true
|
41
|
-
|
42
|
-
# CPF validation
|
43
|
-
validates_as_cpf :cpf, :message => 'Invalid', :allow_nil => true, :allow_blank => true
|
44
|
-
|
45
|
-
# CNPJ validation
|
46
|
-
validates_as_cnpj :cnpj, :message => 'Invalid', :allow_nil => true, :allow_blank => true
|
47
|
-
|
48
|
-
# Brazilian UF validation, both upcase and downcase are allowed by default
|
49
|
-
validates_as_uf_br :uf, :allow_up => true, :allow_down => true
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
Tests:
|
54
|
-
======
|
55
|
-
Some tests have been added to each validator.
|
56
|
-
|
57
|
-
License:
|
58
|
-
========
|
59
|
-
See the LICENSE file.
|