rails_validators 0.0.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.
Files changed (36) hide show
  1. data/.gitignore +4 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +4 -0
  4. data/Gemfile.lock +61 -0
  5. data/README.rdoc +74 -0
  6. data/Rakefile +5 -0
  7. data/lib/rails_validators.rb +13 -0
  8. data/lib/rails_validators/cep.rb +15 -0
  9. data/lib/rails_validators/cnpj.rb +34 -0
  10. data/lib/rails_validators/constants.rb +33 -0
  11. data/lib/rails_validators/cpf.rb +31 -0
  12. data/lib/rails_validators/validates_cep_format_of.rb +28 -0
  13. data/lib/rails_validators/validates_cnpj_format_of.rb +28 -0
  14. data/lib/rails_validators/validates_cpf_format_of.rb +28 -0
  15. data/lib/rails_validators/validates_email_format_of.rb +29 -0
  16. data/lib/rails_validators/validates_url_format_of.rb +29 -0
  17. data/lib/rails_validators/version.rb +3 -0
  18. data/rails_validators.gemspec +25 -0
  19. data/spec/rails_validators/cep_spec.rb +15 -0
  20. data/spec/rails_validators/cnpj_spec.rb +15 -0
  21. data/spec/rails_validators/cpf_spec.rb +15 -0
  22. data/spec/rails_validators/validates_cep_format_of_spec.rb +51 -0
  23. data/spec/rails_validators/validates_cnpj_format_of_spec.rb +51 -0
  24. data/spec/rails_validators/validates_cpf_format_of_spec.rb +51 -0
  25. data/spec/rails_validators/validates_email_format_of_spec.rb +52 -0
  26. data/spec/rails_validators/validates_url_format_of_spec.rb +41 -0
  27. data/spec/schema.rb +9 -0
  28. data/spec/spec_helper.rb +26 -0
  29. data/spec/support/ceps.rb +10 -0
  30. data/spec/support/cnpjs.rb +27 -0
  31. data/spec/support/cpfs.rb +25 -0
  32. data/spec/support/emails.rb +56 -0
  33. data/spec/support/models.rb +10 -0
  34. data/spec/support/translations.yml +21 -0
  35. data/spec/support/urls.rb +45 -0
  36. metadata +185 -0
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ coverage/*
2
+ pkg/*
3
+ *.gem
4
+ .bundle
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rails_validators.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,61 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rails_validators (0.0.1)
5
+ activerecord (>= 3.0.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ activemodel (3.0.3)
11
+ activesupport (= 3.0.3)
12
+ builder (~> 2.1.2)
13
+ i18n (~> 0.4)
14
+ activerecord (3.0.3)
15
+ activemodel (= 3.0.3)
16
+ activesupport (= 3.0.3)
17
+ arel (~> 2.0.2)
18
+ tzinfo (~> 0.3.23)
19
+ activesupport (3.0.3)
20
+ archive-tar-minitar (0.5.2)
21
+ arel (2.0.6)
22
+ builder (2.1.2)
23
+ columnize (0.3.2)
24
+ diff-lcs (1.1.2)
25
+ i18n (0.5.0)
26
+ linecache19 (0.5.11)
27
+ ruby_core_source (>= 0.1.4)
28
+ rspec (2.4.0)
29
+ rspec-core (~> 2.4.0)
30
+ rspec-expectations (~> 2.4.0)
31
+ rspec-mocks (~> 2.4.0)
32
+ rspec-core (2.4.0)
33
+ rspec-expectations (2.4.0)
34
+ diff-lcs (~> 1.1.2)
35
+ rspec-mocks (2.4.0)
36
+ ruby-debug-base19 (0.11.24)
37
+ columnize (>= 0.3.1)
38
+ linecache19 (>= 0.5.11)
39
+ ruby_core_source (>= 0.1.4)
40
+ ruby-debug19 (0.11.6)
41
+ columnize (>= 0.3.1)
42
+ linecache19 (>= 0.5.11)
43
+ ruby-debug-base19 (>= 0.11.19)
44
+ ruby_core_source (0.1.4)
45
+ archive-tar-minitar (>= 0.5.2)
46
+ simplecov (0.3.7)
47
+ simplecov-html (>= 0.3.7)
48
+ simplecov-html (0.3.9)
49
+ sqlite3-ruby (1.3.2)
50
+ tzinfo (0.3.23)
51
+
52
+ PLATFORMS
53
+ ruby
54
+
55
+ DEPENDENCIES
56
+ activerecord (>= 3.0.0)
57
+ rails_validators!
58
+ rspec (>= 2.0.0)
59
+ ruby-debug19
60
+ simplecov (>= 0.3.7)
61
+ sqlite3-ruby
data/README.rdoc ADDED
@@ -0,0 +1,74 @@
1
+ = RailsValidators
2
+
3
+ Improve your rails 3 application with some ActiveRecord validators.
4
+
5
+ == Installation
6
+
7
+ gem install rails_validators
8
+
9
+ Then add it to your Gemfile:
10
+
11
+ gem "rails_validators"
12
+
13
+ == Usage
14
+
15
+ === validates_email_format_of
16
+
17
+ class User < ActiveRecord::Base
18
+ # old fashion
19
+ validates_email_format_of :email
20
+
21
+ # alternative way
22
+ validates :email, :email => true
23
+ end
24
+
25
+ === validates_url_format_of
26
+
27
+ class User < ActiveRecord::Base
28
+ validates :site, :url => true
29
+ end
30
+
31
+ === validates_cep_format_of
32
+
33
+ class User < ActiveRecord::Base
34
+ validates :zipcode, :cep => true
35
+ end
36
+
37
+ === validates_cnpj_format_of
38
+
39
+ class Company < ActiveRecord::Base
40
+ validates :cnpj, :cnpj => true
41
+ end
42
+
43
+ === validates_cpf_format_of
44
+
45
+ class User < ActiveRecord::Base
46
+ validates :cpf, :cpf => true
47
+ end
48
+
49
+ == Maintainer
50
+
51
+ * Willian Fernandes - http://willianfernandes.com.br
52
+
53
+ == License
54
+
55
+ (The MIT License)
56
+
57
+ Permission is hereby granted, free of charge, to any person obtaining
58
+ a copy of this software and associated documentation files (the
59
+ 'Software'), to deal in the Software without restriction, including
60
+ without limitation the rights to use, copy, modify, merge, publish,
61
+ distribute, sublicense, and/or sell copies of the Software, and to
62
+ permit persons to whom the Software is furnished to do so, subject to
63
+ the following conditions:
64
+
65
+ The above copyright notice and this permission notice shall be
66
+ included in all copies or substantial portions of the Software.
67
+
68
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
69
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
70
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
71
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
72
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
73
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
74
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,13 @@
1
+ require 'active_record'
2
+ require File.dirname(__FILE__) + '/rails_validators/constants'
3
+ require File.dirname(__FILE__) + '/rails_validators/validates_cep_format_of'
4
+ require File.dirname(__FILE__) + '/rails_validators/validates_cnpj_format_of'
5
+ require File.dirname(__FILE__) + '/rails_validators/validates_cpf_format_of'
6
+ require File.dirname(__FILE__) + '/rails_validators/validates_email_format_of'
7
+ require File.dirname(__FILE__) + '/rails_validators/validates_url_format_of'
8
+
9
+ module RailsValidators
10
+ autoload :Cep, 'rails_validators/cep'
11
+ autoload :Cnpj, 'rails_validators/cnpj'
12
+ autoload :Cpf, 'rails_validators/cpf'
13
+ end
@@ -0,0 +1,15 @@
1
+ module RailsValidators
2
+ module Cep
3
+ extend self
4
+
5
+ def valid?(cep_number)
6
+ cep_number.gsub!(/\D+/i, '')
7
+
8
+ return false if cep_number.nil?
9
+ return false if cep_number.strip == ''
10
+ return false if cep_number.size != 8
11
+
12
+ true
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,34 @@
1
+ module RailsValidators
2
+ module Cnpj
3
+ extend self
4
+
5
+ def valid?(cnpj_number)
6
+ cnpj_number.gsub!(/\D+/i, '')
7
+
8
+ return false if cnpj_number.nil?
9
+ return false if cnpj_number.strip == ''
10
+ return false if cnpj_number.size != 14
11
+ return false if %W[00000000000000 11111111111111 22222222222222 33333333333333 44444444444444 55555555555555 66666666666666 77777777777777 88888888888888 99999999999999].include? cnpj_number
12
+
13
+ cnpj = cnpj_number.slice(0, 12)
14
+
15
+ first_digit = calculate_digit(cnpj, :first)
16
+ second_digit = calculate_digit("#{cnpj}#{first_digit}", :second)
17
+ cnpj = "#{cnpj}#{first_digit}#{second_digit}"
18
+
19
+ cnpj == cnpj_number
20
+ end
21
+
22
+ private
23
+ def calculate_digit(numbers, type)
24
+ digits = { :first => [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2],
25
+ :second => [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2] }
26
+
27
+ digit = 0
28
+ (0...numbers.size).each { |i| digit = digit + numbers[i].to_i * digits[type.to_sym][i] }
29
+ digit = (digit % 11) < 2 ? 0 : 11 - (digit % 11)
30
+
31
+ digit
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,33 @@
1
+ module RailsValidators
2
+ module Constants
3
+ EMAIL_REGEX = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
4
+
5
+ # Source: https://github.com/henrik/validates_url_format_of
6
+ # Improves: https://github.com/fnando/validators
7
+ IPv4_PART = /\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]/ # 0-255
8
+
9
+ if RUBY_VERSION >= "1.9"
10
+ URL_REGEX = %r[
11
+ \A
12
+ https?:// # http:// or https://
13
+ ([^\s:@]+:[^\s:@]*@)? # optional username:pw@
14
+ ( (([^\W_]+\.)*xn--)?[^\W_]+([-.][^\W_]+)*\.[a-z]{2,6}\.? | # domain (including Punycode/IDN)...
15
+ #{IPv4_PART}(\.#{IPv4_PART}){3} ) # or IPv4
16
+ (:\d{1,5})? # optional port
17
+ ([/?]\S*)? # optional /whatever or ?whatever
18
+ \Z
19
+ ]ixs
20
+ else
21
+ URL_REGEX = %r[
22
+ \A
23
+ https?:// # http:// or https://
24
+ ([^\s:@]+:[^\s:@]*@)? # optional username:pw@
25
+ ( (([^\W_]+\.)*xn--)?[^\W_]+([-.][^\W_]+)*\.[a-z]{2,6}\.? | # domain (including Punycode/IDN)...
26
+ #{IPv4_PART}(\.#{IPv4_PART}){3} ) # or IPv4
27
+ (:\d{1,5})? # optional port
28
+ ([/?]\S*)? # optional /whatever or ?whatever
29
+ \Z
30
+ ]ixu
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,31 @@
1
+ module RailsValidators
2
+ module Cpf
3
+ extend self
4
+
5
+ def valid?(cpf_number)
6
+ cpf_number.gsub!(/\D+/i, '')
7
+
8
+ return false if cpf_number.nil?
9
+ return false if cpf_number.strip == ''
10
+ return false if cpf_number.size != 11
11
+ return false if %W[00000000000 11111111111 22222222222 33333333333 44444444444 55555555555 66666666666 77777777777 88888888888 99999999999].include? cpf_number
12
+
13
+ cpf = cpf_number.slice(0, 9)
14
+
15
+ first_digit = calculate_digit(cpf, 10)
16
+ second_digit = calculate_digit("#{cpf}#{first_digit}", 11)
17
+ cpf = "#{cpf}#{first_digit}#{second_digit}"
18
+
19
+ cpf == cpf_number
20
+ end
21
+
22
+ private
23
+ def calculate_digit(numbers, first_number)
24
+ digit = 0
25
+ (0...first_number-1).each { |i| digit = digit + numbers[i].to_i * (first_number-i) }
26
+ digit = (digit % 11) < 2 ? 0 : 11 - (digit % 11)
27
+
28
+ digit
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,28 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class CepValidator < EachValidator
4
+ def validate_each(record, attribute, value)
5
+ unless RailsValidators::Cep.valid?(value.to_s)
6
+ record.errors.add(
7
+ attribute, :invalid_cep,
8
+ :message => options[:message], :value => value
9
+ )
10
+ end
11
+ end
12
+ end
13
+
14
+ module ClassMethods
15
+ # Validates whether or not the specified CEP is valid.
16
+ #
17
+ # class Company < ActiveRecord::Base
18
+ # validates_cep_format_of :cep
19
+ # end
20
+ #
21
+ def validates_cep_format_of(*attr_names)
22
+ validates_with CepValidator, _merge_attributes(attr_names)
23
+ end
24
+
25
+ alias_method :validates_cep, :validates_cep_format_of
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class CnpjValidator < EachValidator
4
+ def validate_each(record, attribute, value)
5
+ unless RailsValidators::Cnpj.valid?(value.to_s)
6
+ record.errors.add(
7
+ attribute, :invalid_cnpj,
8
+ :message => options[:message], :value => value
9
+ )
10
+ end
11
+ end
12
+ end
13
+
14
+ module ClassMethods
15
+ # Validates whether or not the specified CNPJ is valid.
16
+ #
17
+ # class Company < ActiveRecord::Base
18
+ # validates_cnpj_format_of :cnpj
19
+ # end
20
+ #
21
+ def validates_cnpj_format_of(*attr_names)
22
+ validates_with CnpjValidator, _merge_attributes(attr_names)
23
+ end
24
+
25
+ alias_method :validates_cnpj, :validates_cnpj_format_of
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class CpfValidator < EachValidator
4
+ def validate_each(record, attribute, value)
5
+ unless RailsValidators::Cpf.valid?(value.to_s)
6
+ record.errors.add(
7
+ attribute, :invalid_cpf,
8
+ :message => options[:message], :value => value
9
+ )
10
+ end
11
+ end
12
+ end
13
+
14
+ module ClassMethods
15
+ # Validates whether or not the specified CPF is valid.
16
+ #
17
+ # class User < ActiveRecord::Base
18
+ # validates_cpf_format_of :cpf
19
+ # end
20
+ #
21
+ def validates_cpf_format_of(*attr_names)
22
+ validates_with CpfValidator, _merge_attributes(attr_names)
23
+ end
24
+
25
+ alias_method :validates_cpf, :validates_cpf_format_of
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,29 @@
1
+ # Source: https://github.com/fnando/validators
2
+ module ActiveModel
3
+ module Validations
4
+ class EmailValidator < EachValidator
5
+ def validate_each(record, attribute, value)
6
+ if value.to_s !~ RailsValidators::Constants::EMAIL_REGEX
7
+ record.errors.add(
8
+ attribute, :invalid_email,
9
+ :message => options[:message], :value => value
10
+ )
11
+ end
12
+ end
13
+ end
14
+
15
+ module ClassMethods
16
+ # Validates whether or not the specified e-mail address is valid.
17
+ #
18
+ # class User < ActiveRecord::Base
19
+ # validates_email_format_of :email
20
+ # end
21
+ #
22
+ def validates_email_format_of(*attr_names)
23
+ validates_with EmailValidator, _merge_attributes(attr_names)
24
+ end
25
+
26
+ alias_method :validates_email, :validates_email_format_of
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ # Source: https://github.com/fnando/validators
2
+ module ActiveModel
3
+ module Validations
4
+ class UrlValidator < EachValidator
5
+ def validate_each(record, attribute, value)
6
+ if value.to_s !~ RailsValidators::Constants::URL_REGEX
7
+ record.errors.add(
8
+ attribute, :invalid_url,
9
+ :message => options[:message], :value => value
10
+ )
11
+ end
12
+ end
13
+ end
14
+
15
+ module ClassMethods
16
+ # Validates whether or not the specified URL is valid.
17
+ #
18
+ # class User < ActiveRecord::Base
19
+ # validates_url_format_of :site
20
+ # end
21
+ #
22
+ def validates_url_format_of(*attr_names)
23
+ validates_with UrlValidator, _merge_attributes(attr_names)
24
+ end
25
+
26
+ alias_method :validates_url, :validates_url_format_of
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module RailsValidators
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'rails_validators/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'rails_validators'
7
+ s.version = RailsValidators::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Willian Fernandes']
10
+ s.email = ['willian@willianfernandes.com.br']
11
+ s.homepage = ''
12
+ s.summary = %q{Some ActiveRecord validators.}
13
+ s.description = %q{Improve your rails 3 application with some ActiveRecord validators.}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ['lib']
19
+
20
+ s.add_dependency 'activerecord', '>= 3.0.0'
21
+ s.add_development_dependency 'rspec', '>= 2.0.0'
22
+ s.add_development_dependency 'sqlite3-ruby'
23
+ s.add_development_dependency 'ruby-debug19'
24
+ s.add_development_dependency 'simplecov', '>= 0.3.7'
25
+ end
@@ -0,0 +1,15 @@
1
+ require "spec_helper"
2
+
3
+ describe "RailsValidators::Cep" do
4
+ VALID_CEPS.each do |cep_number|
5
+ it "should accept #{cep_number} as a valid cep" do
6
+ RailsValidators::Cep.valid?(cep_number).should be_true
7
+ end
8
+ end
9
+
10
+ INVALID_CEPS.each do |cep_number|
11
+ it "should reject #{cep_number} as a valid cep" do
12
+ RailsValidators::Cep.valid?(cep_number).should be_false
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require "spec_helper"
2
+
3
+ describe "RailsValidators::Cnpj" do
4
+ VALID_CNPJS.each do |cnpj_number|
5
+ it "should accept #{cnpj_number} as a valid cnpj" do
6
+ RailsValidators::Cnpj.valid?(cnpj_number).should be_true
7
+ end
8
+ end
9
+
10
+ INVALID_CNPJS.each do |cnpj_number|
11
+ it "should reject #{cnpj_number} as a valid cnpj" do
12
+ RailsValidators::Cnpj.valid?(cnpj_number).should be_false
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require "spec_helper"
2
+
3
+ describe "RailsValidators::Cpf" do
4
+ VALID_CPFS.each do |cpf_number|
5
+ it "should accept #{cpf_number} as a valid cpf" do
6
+ RailsValidators::Cpf.valid?(cpf_number).should be_true
7
+ end
8
+ end
9
+
10
+ INVALID_CPFS.each do |cpf_number|
11
+ it "should reject #{cpf_number} as a valid cpf" do
12
+ RailsValidators::Cpf.valid?(cpf_number).should be_false
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe '.validates_cep_format_of' do
5
+ before do
6
+ User.validates :zipcode, :cep => true
7
+ end
8
+
9
+ VALID_CEPS.each do |cep|
10
+ it "should accept #{cep.inspect} as a valid cep" do
11
+ user = User.new(:zipcode => cep)
12
+ user.should be_valid
13
+ end
14
+ end
15
+
16
+ INVALID_CEPS.each do |cep|
17
+ it "should reject #{cep.inspect} as a valid cep" do
18
+ user = User.new(:zipcode => cep)
19
+ user.should_not be_valid
20
+ end
21
+ end
22
+
23
+ it 'should use default error message' do
24
+ user = User.new(:zipcode => INVALID_CEPS.first)
25
+ user.should_not be_valid
26
+ user.errors[:zipcode].should == ['is not a valid cep number']
27
+ end
28
+
29
+ it 'should reject nil value' do
30
+ user = User.new(:zipcode => nil)
31
+ user.should_not be_valid
32
+ user.errors[:zipcode].should_not be_empty
33
+ end
34
+
35
+ it 'should reject empty value' do
36
+ user = User.new(:zipcode => '')
37
+ user.should_not be_valid
38
+ user.errors[:zipcode].should_not be_empty
39
+ end
40
+
41
+ it 'should use I18n string as error message [pt-BR]' do
42
+ I18n.locale = :'pt-BR'
43
+ user = User.new(:zipcode => INVALID_CEPS.first)
44
+ user.should_not be_valid
45
+ user.errors[:zipcode].should == ['não é um CEP válido']
46
+ end
47
+
48
+ it 'should have alias method' do
49
+ User.should respond_to(:validates_cep)
50
+ end
51
+ end
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe '.validates_cnpj_format_of' do
5
+ before do
6
+ Company.validates :cnpj, :cnpj => true
7
+ end
8
+
9
+ VALID_CNPJS.each do |cnpj|
10
+ it "should accept #{cnpj.inspect} as a valid cnpj" do
11
+ user = Company.new(:cnpj => cnpj)
12
+ user.should be_valid
13
+ end
14
+ end
15
+
16
+ INVALID_CNPJS.each do |cnpj|
17
+ it "should reject #{cnpj.inspect} as a valid cnpj" do
18
+ user = Company.new(:cnpj => cnpj)
19
+ user.should_not be_valid
20
+ end
21
+ end
22
+
23
+ it 'should use default error message' do
24
+ user = Company.new(:cnpj => INVALID_CNPJS.first)
25
+ user.should_not be_valid
26
+ user.errors[:cnpj].should == ['is not a valid cnpj number']
27
+ end
28
+
29
+ it 'should reject nil value' do
30
+ user = Company.new(:cnpj => nil)
31
+ user.should_not be_valid
32
+ user.errors[:cnpj].should_not be_empty
33
+ end
34
+
35
+ it 'should reject empty value' do
36
+ user = Company.new(:cnpj => '')
37
+ user.should_not be_valid
38
+ user.errors[:cnpj].should_not be_empty
39
+ end
40
+
41
+ it 'should use I18n string as error message [pt-BR]' do
42
+ I18n.locale = :'pt-BR'
43
+ user = Company.new(:cnpj => INVALID_CNPJS.first)
44
+ user.should_not be_valid
45
+ user.errors[:cnpj].should == ['não é um CNPJ válido']
46
+ end
47
+
48
+ it 'should have alias method' do
49
+ Company.should respond_to(:validates_cnpj)
50
+ end
51
+ end
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe '.validates_cpf_format_of' do
5
+ before do
6
+ User.validates :cpf, :cpf => true
7
+ end
8
+
9
+ VALID_CPFS.each do |cpf|
10
+ it "should accept #{cpf.inspect} as a valid cpf" do
11
+ user = User.new(:cpf => cpf)
12
+ user.should be_valid
13
+ end
14
+ end
15
+
16
+ INVALID_CPFS.each do |cpf|
17
+ it "should reject #{cpf.inspect} as a valid cpf" do
18
+ user = User.new(:cpf => cpf)
19
+ user.should_not be_valid
20
+ end
21
+ end
22
+
23
+ it 'should use default error message' do
24
+ user = User.new(:cpf => INVALID_CPFS.first)
25
+ user.should_not be_valid
26
+ user.errors[:cpf].should == ['is not a valid cpf number']
27
+ end
28
+
29
+ it 'should reject nil value' do
30
+ user = User.new(:cpf => nil)
31
+ user.should_not be_valid
32
+ user.errors[:cpf].should_not be_empty
33
+ end
34
+
35
+ it 'should reject empty value' do
36
+ user = User.new(:cpf => '')
37
+ user.should_not be_valid
38
+ user.errors[:cpf].should_not be_empty
39
+ end
40
+
41
+ it 'should use I18n string as error message [pt-BR]' do
42
+ I18n.locale = :'pt-BR'
43
+ user = User.new(:cpf => INVALID_CPFS.first)
44
+ user.should_not be_valid
45
+ user.errors[:cpf].should == ['não é um CPF válido']
46
+ end
47
+
48
+ it 'should have alias method' do
49
+ User.should respond_to(:validates_cpf)
50
+ end
51
+ end
@@ -0,0 +1,52 @@
1
+ # encoding: utf-8
2
+ # Source: https://github.com/fnando/validators
3
+ require "spec_helper"
4
+
5
+ describe ".validates_email_format_of" do
6
+ before do
7
+ User.validates :email, :email => true
8
+ end
9
+
10
+ VALID_EMAILS.each do |email|
11
+ it "should accept #{email.inspect} as a valid email" do
12
+ user = User.new(:email => email)
13
+ user.should be_valid
14
+ end
15
+ end
16
+
17
+ INVALID_EMAILS.each do |email|
18
+ it "should reject #{email.inspect} as a valid email" do
19
+ user = User.new(:email => "invalid")
20
+ user.should_not be_valid
21
+ end
22
+ end
23
+
24
+ it "should use default error message" do
25
+ user = User.new(:email => "invalid")
26
+ user.should_not be_valid
27
+ user.errors[:email].should == ["is not a valid address"]
28
+ end
29
+
30
+ it "should reject nil value" do
31
+ user = User.new(:email => nil)
32
+ user.should_not be_valid
33
+ user.errors[:email].should_not be_empty
34
+ end
35
+
36
+ it "should reject empty value" do
37
+ user = User.new(:email => "")
38
+ user.should_not be_valid
39
+ user.errors[:email].should_not be_empty
40
+ end
41
+
42
+ it "should use I18n string as error message [pt-BR]" do
43
+ I18n.locale = :'pt-BR'
44
+ user = User.new(:email => "invalid")
45
+ user.should_not be_valid
46
+ user.errors[:email].should == ["não parece ser um e-mail válido"]
47
+ end
48
+
49
+ it "should have alias method" do
50
+ User.should respond_to(:validates_email)
51
+ end
52
+ end
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+ # Source: https://github.com/fnando/validators
3
+
4
+ require "spec_helper"
5
+
6
+ describe ".validates_url_format_of" do
7
+ before do
8
+ Company.validates_url_format_of :url, :allow_blank => false
9
+ end
10
+
11
+ VALID_URLS.each do |url|
12
+ it "should accept #{url.inspect} as a valid url" do
13
+ user = Company.new(:url => url)
14
+ user.should be_valid
15
+ end
16
+ end
17
+
18
+ INVALID_URLS.each do |url|
19
+ it "should reject #{url.inspect} as a valid url" do
20
+ user = Company.new(:url => url)
21
+ user.should_not be_valid
22
+ end
23
+ end
24
+
25
+ it "should have alias method" do
26
+ Company.should respond_to(:validates_url)
27
+ end
28
+
29
+ it "should use default error message" do
30
+ user = Company.new(:url => "invalid")
31
+ user.should_not be_valid
32
+ user.errors[:url].should == ["is not a valid address"]
33
+ end
34
+
35
+ it "should use I18n string as error message [pt-BR]" do
36
+ I18n.locale = :'pt-BR'
37
+ user = Company.new(:url => "invalid")
38
+ user.should_not be_valid
39
+ user.errors[:url].should == ["não parece ser uma URL válida"]
40
+ end
41
+ end
data/spec/schema.rb ADDED
@@ -0,0 +1,9 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :users do |t|
3
+ t.string :email, :cpf, :zipcode
4
+ end
5
+
6
+ create_table :companies do |t|
7
+ t.string :cnpj, :url
8
+ end
9
+ end
@@ -0,0 +1,26 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter 'spec'
4
+ end
5
+
6
+ require "rails_validators"
7
+
8
+ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
9
+
10
+ ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
11
+ load "schema.rb"
12
+
13
+ I18n.load_path << File.dirname(__FILE__) + "/support/translations.yml"
14
+
15
+ RSpec.configure do |config|
16
+ config.before do
17
+ I18n.locale = :en
18
+
19
+ ActiveRecord::Base.descendants.each do |model|
20
+ model.delete_all
21
+ Object.class_eval { remove_const model.name if const_defined?(model.name) }
22
+ end
23
+
24
+ load File.dirname(__FILE__) + "/support/models.rb"
25
+ end
26
+ end
@@ -0,0 +1,10 @@
1
+ VALID_CEPS = [
2
+ '12246001',
3
+ '12246-001'
4
+ ]
5
+
6
+ INVALID_CEPS = [
7
+ '212246001',
8
+ '212246-001',
9
+ '12246-0013'
10
+ ]
@@ -0,0 +1,27 @@
1
+ INVALID_CNPJS = [
2
+ '00000000000000',
3
+ '11111111111111',
4
+ '22222222222222',
5
+ '33333333333333',
6
+ '44444444444444',
7
+ '55555555555555',
8
+ '66666666666666',
9
+ '77777777777777',
10
+ '88888888888888',
11
+ '99999999999999',
12
+ '9346929245293',
13
+ '93469292452937',
14
+ '934692926451037',
15
+ '93.692.926/0001-45'
16
+ ]
17
+
18
+ VALID_CNPJS = [
19
+ '08.427.092/0001-00',
20
+ '08427092000100',
21
+ '29318294837205',
22
+ '93473627485640',
23
+ '19828947558513',
24
+ '87476324653220',
25
+ '02384877000159',
26
+ '00001714000114'
27
+ ]
@@ -0,0 +1,25 @@
1
+ INVALID_CPFS = [
2
+ '00000000000',
3
+ '11111111111',
4
+ '22222222222',
5
+ '33333333333',
6
+ '44444444444',
7
+ '55555555555',
8
+ '66666666666',
9
+ '77777777777',
10
+ '88888888888',
11
+ '99999999999',
12
+ '9346929245',
13
+ '93469292645',
14
+ '934.692.926-45'
15
+ ]
16
+
17
+ VALID_CPFS = [
18
+ '34439699847',
19
+ '34795775800',
20
+ '93846273058',
21
+ '26374628179',
22
+ '19273547303',
23
+ '47384950201',
24
+ '12345678909'
25
+ ]
@@ -0,0 +1,56 @@
1
+ # Source: https://github.com/fnando/validators
2
+ INVALID_EMAILS = [
3
+ 'invalid@example-com',
4
+ # period can not start local part
5
+ '.invalid@example.com',
6
+ # period can not end local part
7
+ 'invalid.@example.com',
8
+ # period can not appear twice consecutively in local part
9
+ 'invali..d@example.com',
10
+ # should not allow underscores in domain names
11
+ 'invalid@ex_mple.com',
12
+ 'invalid@example.com.',
13
+ 'invalid@example.com_',
14
+ 'invalid@example.com-',
15
+ 'invalid-example.com',
16
+ 'invalid@example.b#r.com',
17
+ 'invalid@example.c',
18
+ 'invali d@example.com',
19
+ 'invalidexample.com',
20
+ 'invalid@example.',
21
+ # from http://tools.ietf.org/html/rfc3696, page 5
22
+ # corrected in http://www.rfc-editor.org/errata_search.php?rfc=3696
23
+ 'Fred\ Bloggs_@example.com',
24
+ 'Abc\@def+@example.com',
25
+ 'Joe.\\Blow@example.com'
26
+ ]
27
+
28
+ VALID_EMAILS = [
29
+ 'valid@example.com',
30
+ 'Valid@test.example.com',
31
+ 'valid+valid123@test.example.com',
32
+ 'valid_valid123@test.example.com',
33
+ 'valid-valid+123@test.example.co.uk',
34
+ 'valid-valid+1.23@test.example.com.au',
35
+ 'valid@example.co.uk',
36
+ 'v@example.com',
37
+ 'valid@example.ca',
38
+ 'valid_@example.com',
39
+ 'valid123.456@example.org',
40
+ 'valid123.456@example.travel',
41
+ 'valid123.456@example.museum',
42
+ 'valid@example.mobi',
43
+ 'valid@example.info',
44
+ 'valid-@example.com',
45
+ # from RFC 3696, page 6
46
+ 'customer/department=shipping@example.com',
47
+ '$A12345@example.com',
48
+ '!def!xyz%abc@example.com',
49
+ '_somename@example.com',
50
+ # apostrophes
51
+ "test'test@example.com",
52
+ # '"Abc\@def"@example.com',
53
+ # from http://www.rfc-editor.org/errata_search.php?rfc=3696
54
+ # '"Fred\ Bloggs"@example.com',
55
+ '"Joe.\\Blow"@example.com'
56
+ ]
@@ -0,0 +1,10 @@
1
+ class User < ActiveRecord::Base
2
+ # validates :email, :email => true
3
+ # validates :cpf, :cpf => true
4
+ # validates :zipcode, :cep => true
5
+ end
6
+
7
+ class Company < ActiveRecord::Base
8
+ # validates :cnpj, :cnpj => true
9
+ # validates :url, :url => true
10
+ end
@@ -0,0 +1,21 @@
1
+ en:
2
+ activerecord:
3
+ errors:
4
+ messages:
5
+ record_invalid: "Errors: %{errors}"
6
+ invalid_email: 'is not a valid address'
7
+ invalid_url: 'is not a valid address'
8
+ invalid_cep: 'is not a valid cep number'
9
+ invalid_cnpj: 'is not a valid cnpj number'
10
+ invalid_cpf: 'is not a valid cpf number'
11
+
12
+ pt-BR:
13
+ activerecord:
14
+ errors:
15
+ messages:
16
+ record_invalid: "Erros: %{errors}"
17
+ invalid_email: 'não parece ser um e-mail válido'
18
+ invalid_url: 'não parece ser uma URL válida'
19
+ invalid_cep: 'não é um CEP válido'
20
+ invalid_cnpj: 'não é um CNPJ válido'
21
+ invalid_cpf: 'não é um CPF válido'
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+ # Source: https://github.com/fnando/validators
3
+
4
+ VALID_URLS = [
5
+ 'http://example.com',
6
+ 'http://example.com/',
7
+ 'http://www.example.com/',
8
+ 'http://sub.domain.example.com/',
9
+ 'http://bbc.co.uk',
10
+ 'http://example.com?foo',
11
+ 'http://example.com?url=http://example.com',
12
+ 'http://example.com:8000',
13
+ 'http://www.sub.example.com/page.html?foo=bar&baz=%23#anchor',
14
+ 'http://user:pass@example.com',
15
+ 'http://user:@example.com',
16
+ 'http://example.com/~user',
17
+ 'http://example.xy', # Not a real TLD, but we're fine with anything of 2-6 chars
18
+ 'http://example.museum',
19
+ 'http://1.0.255.249',
20
+ 'http://1.2.3.4:80',
21
+ 'HttP://example.com',
22
+ 'https://example.com',
23
+ # 'http://räksmörgås.nu', # IDN
24
+ 'http://xn--rksmrgs-5wao1o.nu', # Punycode
25
+ 'http://www.xn--rksmrgs-5wao1o.nu',
26
+ 'http://foo.bar.xn--rksmrgs-5wao1o.nu',
27
+ 'http://example.com.', # Explicit TLD root period
28
+ 'http://example.com./foo'
29
+ ]
30
+
31
+ INVALID_URLS = [
32
+ "url",
33
+ "www.example.com",
34
+ "http://ex ample.com",
35
+ "http://example.com/foo bar",
36
+ 'http://256.0.0.1',
37
+ 'http://u:u:u@example.com',
38
+ 'http://r?ksmorgas.com',
39
+
40
+ # These can all be valid local URLs, but should not be considered valid
41
+ # for public consumption.
42
+ "http://example",
43
+ "http://example.c",
44
+ 'http://example.toolongtld'
45
+ ]
metadata ADDED
@@ -0,0 +1,185 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_validators
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Willian Fernandes
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-05 00:00:00 -02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activerecord
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 3
30
+ - 0
31
+ - 0
32
+ version: 3.0.0
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 2
45
+ - 0
46
+ - 0
47
+ version: 2.0.0
48
+ type: :development
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: sqlite3-ruby
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: ruby-debug19
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ type: :development
75
+ version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ name: simplecov
78
+ prerelease: false
79
+ requirement: &id005 !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ segments:
85
+ - 0
86
+ - 3
87
+ - 7
88
+ version: 0.3.7
89
+ type: :development
90
+ version_requirements: *id005
91
+ description: Improve your rails 3 application with some ActiveRecord validators.
92
+ email:
93
+ - willian@willianfernandes.com.br
94
+ executables: []
95
+
96
+ extensions: []
97
+
98
+ extra_rdoc_files: []
99
+
100
+ files:
101
+ - .gitignore
102
+ - .rspec
103
+ - Gemfile
104
+ - Gemfile.lock
105
+ - README.rdoc
106
+ - Rakefile
107
+ - lib/rails_validators.rb
108
+ - lib/rails_validators/cep.rb
109
+ - lib/rails_validators/cnpj.rb
110
+ - lib/rails_validators/constants.rb
111
+ - lib/rails_validators/cpf.rb
112
+ - lib/rails_validators/validates_cep_format_of.rb
113
+ - lib/rails_validators/validates_cnpj_format_of.rb
114
+ - lib/rails_validators/validates_cpf_format_of.rb
115
+ - lib/rails_validators/validates_email_format_of.rb
116
+ - lib/rails_validators/validates_url_format_of.rb
117
+ - lib/rails_validators/version.rb
118
+ - rails_validators.gemspec
119
+ - spec/rails_validators/cep_spec.rb
120
+ - spec/rails_validators/cnpj_spec.rb
121
+ - spec/rails_validators/cpf_spec.rb
122
+ - spec/rails_validators/validates_cep_format_of_spec.rb
123
+ - spec/rails_validators/validates_cnpj_format_of_spec.rb
124
+ - spec/rails_validators/validates_cpf_format_of_spec.rb
125
+ - spec/rails_validators/validates_email_format_of_spec.rb
126
+ - spec/rails_validators/validates_url_format_of_spec.rb
127
+ - spec/schema.rb
128
+ - spec/spec_helper.rb
129
+ - spec/support/ceps.rb
130
+ - spec/support/cnpjs.rb
131
+ - spec/support/cpfs.rb
132
+ - spec/support/emails.rb
133
+ - spec/support/models.rb
134
+ - spec/support/translations.yml
135
+ - spec/support/urls.rb
136
+ has_rdoc: true
137
+ homepage: ""
138
+ licenses: []
139
+
140
+ post_install_message:
141
+ rdoc_options: []
142
+
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ segments:
151
+ - 0
152
+ version: "0"
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ segments:
159
+ - 0
160
+ version: "0"
161
+ requirements: []
162
+
163
+ rubyforge_project:
164
+ rubygems_version: 1.3.7
165
+ signing_key:
166
+ specification_version: 3
167
+ summary: Some ActiveRecord validators.
168
+ test_files:
169
+ - spec/rails_validators/cep_spec.rb
170
+ - spec/rails_validators/cnpj_spec.rb
171
+ - spec/rails_validators/cpf_spec.rb
172
+ - spec/rails_validators/validates_cep_format_of_spec.rb
173
+ - spec/rails_validators/validates_cnpj_format_of_spec.rb
174
+ - spec/rails_validators/validates_cpf_format_of_spec.rb
175
+ - spec/rails_validators/validates_email_format_of_spec.rb
176
+ - spec/rails_validators/validates_url_format_of_spec.rb
177
+ - spec/schema.rb
178
+ - spec/spec_helper.rb
179
+ - spec/support/ceps.rb
180
+ - spec/support/cnpjs.rb
181
+ - spec/support/cpfs.rb
182
+ - spec/support/emails.rb
183
+ - spec/support/models.rb
184
+ - spec/support/translations.yml
185
+ - spec/support/urls.rb