validators 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9f181df550b243432c4e517dd043cddbbdc01615
4
- data.tar.gz: c8c9555509201f15b3699c23d371c13ab8083fe2
3
+ metadata.gz: 17b8dea8b888cbb70e92c7c97fdc67135a27121a
4
+ data.tar.gz: c7b932ae83a70238d74764c888a48d5884ef2c1f
5
5
  SHA512:
6
- metadata.gz: 51a00aa0c7ba19e185a8e8cee3a3f41935acd175d586a923b87d82d4cb49b5243c1e18ccf3eb48c172d02d0ef6f20f085f86cc4150251836c3017da99d8943ca
7
- data.tar.gz: 667464a246123492c30054884096f1e865b6bb0826026a4ef20e44f922c741ee1362de6e0896e6a0a8613591b3f6bbdbfb3708349533685e77f9d1279a7492d2
6
+ metadata.gz: ce83811dfd880c856fcee5cace391b438b3b6045b1cee3f456b2e9d70a377bf31ed55b9a55833719da8a1dba35d4de68fd2ed90ed9f9df453edb7e7644294959
7
+ data.tar.gz: 16f6cbd5a1f73bfd6402b005b59b4de83c2084b4dfbd54dec401dfd435b561e579a2d8cf9ed240f2d27e75f8796901ba00b5729a8976c1e0e6c46734bdda2311
data/.rspec CHANGED
@@ -1 +1 @@
1
- --color --format documentation
1
+ --color
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- validators (2.0.0)
4
+ validators (2.1.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -26,6 +26,7 @@ GEM
26
26
  awesome_print (1.2.0)
27
27
  builder (3.1.4)
28
28
  coderay (1.0.9)
29
+ cpf_cnpj (0.2.0)
29
30
  diff-lcs (1.2.5)
30
31
  i18n (0.6.5)
31
32
  method_source (0.8.2)
@@ -67,6 +68,7 @@ PLATFORMS
67
68
 
68
69
  DEPENDENCIES
69
70
  activerecord (>= 3.0)
71
+ cpf_cnpj
70
72
  pry-meta
71
73
  rake
72
74
  rspec
data/README.md ADDED
@@ -0,0 +1,128 @@
1
+ # Validators
2
+
3
+ Add some nice Rails 3 ActiveRecord validators.
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ gem install validators
9
+ ```
10
+
11
+ Then add it to your Gemfile:
12
+
13
+ ```ruby
14
+ gem "validators"
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ### validates_email_format_of
20
+
21
+ ```ruby
22
+ class User < ActiveRecord::Base
23
+ # old fashion
24
+ validates_email_format_of :email
25
+
26
+ # alternative way
27
+ validates :email, :email => true
28
+ end
29
+ ```
30
+
31
+ ### validates_url_format_of
32
+
33
+ ```ruby
34
+ class User < ActiveRecord::Base
35
+ validates_url_format_of :site
36
+ end
37
+ ```
38
+
39
+ ### validates_ownership_of
40
+
41
+ ```ruby
42
+ class Task < ActiveRecord::Base
43
+ belongs_to :user
44
+ belongs_to :category
45
+
46
+ validates_ownership_of :category, :with => :user
47
+ end
48
+
49
+ user = User.find(1)
50
+ another_user = User.find(2)
51
+
52
+ user_category = user.categories.first
53
+ another_user_category = another_user.categories.first
54
+
55
+ task = user.tasks.create(:category => user_category)
56
+ task.valid?
57
+ #=> true
58
+
59
+ task = user.tasks.create(:category => another_user_category)
60
+ task.valid?
61
+ #=> false
62
+ ```
63
+
64
+ ### validates_ip_address
65
+
66
+ ```ruby
67
+ class Server < ActiveRecord::Base
68
+ validates_ip_address :address
69
+ validates_ip_address :address, :only => :v4
70
+ validates_ip_address :address, :only => :v6
71
+ end
72
+ ```
73
+
74
+ ### validates_datetime
75
+
76
+ ```ruby
77
+ class Server < ActiveRecord::Base
78
+ validates_datetime :starts_at
79
+ validates_datetime :ends_at, :after => :starts_at, :if => :starts_at?
80
+ validates_datetime :ends_at, :after => :now
81
+ validates_datetime :ends_at, :before => :today
82
+
83
+ validates :starts_at, :datetime => true
84
+ end
85
+ ```
86
+
87
+ ### validates_cpf_format_of
88
+
89
+ ```ruby
90
+ class User < ActiveRecord::Base
91
+ validates_cpf_format_of :document
92
+ end
93
+ ```
94
+
95
+ ### validates_cnpj_format_of
96
+
97
+ ```ruby
98
+ class User < ActiveRecord::Base
99
+ validates_cnpj_format_of :document
100
+ end
101
+ ```
102
+
103
+ ## Maintainer
104
+
105
+ * [Nando Vieira](http://simplesideias.com.br)
106
+
107
+ ## License
108
+
109
+ (The MIT License)
110
+
111
+ Permission is hereby granted, free of charge, to any person obtaining
112
+ a copy of this software and associated documentation files (the
113
+ 'Software'), to deal in the Software without restriction, including
114
+ without limitation the rights to use, copy, modify, merge, publish,
115
+ distribute, sublicense, and/or sell copies of the Software, and to
116
+ permit persons to whom the Software is furnished to do so, subject to
117
+ the following conditions:
118
+
119
+ The above copyright notice and this permission notice shall be
120
+ included in all copies or substantial portions of the Software.
121
+
122
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
123
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
124
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
125
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
126
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
127
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
128
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class CnpjValidator < EachValidator
4
+ def validate_each(record, attribute, value)
5
+ record.errors.add(attribute, :invalid_cnpj,
6
+ message: options[:message],
7
+ value: value
8
+ ) unless CNPJ.valid?(value)
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ # Validates whether or not the specified CNPJ is valid.
14
+ #
15
+ # class User < ActiveRecord::Base
16
+ # validates_cnpj_format_of :document
17
+ # end
18
+ #
19
+ def validates_cnpj_format_of(*attr_names)
20
+ require "cnpj"
21
+ validates_with CnpjValidator, _merge_attributes(attr_names)
22
+ rescue LoadError
23
+ fail "cpf_cnpj is not part of the bundle. Add it to Gemfile."
24
+ end
25
+
26
+ alias_method :validates_cnpj, :validates_cnpj_format_of
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class CpfValidator < EachValidator
4
+ def validate_each(record, attribute, value)
5
+ record.errors.add(attribute, :invalid_cpf,
6
+ message: options[:message],
7
+ value: value
8
+ ) unless CPF.valid?(value)
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ # Validates whether or not the specified CPF is valid.
14
+ #
15
+ # class User < ActiveRecord::Base
16
+ # validates_cpf_format_of :document
17
+ # end
18
+ #
19
+ def validates_cpf_format_of(*attr_names)
20
+ require "cpf"
21
+ validates_with CpfValidator, _merge_attributes(attr_names)
22
+ rescue LoadError
23
+ fail "cpf_cnpj is not part of the bundle. Add it to Gemfile."
24
+ end
25
+
26
+ alias_method :validates_cpf, :validates_cpf_format_of
27
+ end
28
+ end
29
+ end
@@ -1,7 +1,7 @@
1
1
  module Validators
2
2
  module Version
3
3
  MAJOR = 2
4
- MINOR = 0
4
+ MINOR = 1
5
5
  PATCH = 0
6
6
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
7
  end
data/lib/validators.rb CHANGED
@@ -5,6 +5,8 @@ require "validators/validates_ip_address"
5
5
  require "validators/validates_email_format_of"
6
6
  require "validators/validates_url_format_of"
7
7
  require "validators/validates_ownership_of"
8
+ require "validators/validates_cpf_format_of"
9
+ require "validators/validates_cnpj_format_of"
8
10
 
9
11
  module Validators
10
12
  end
@@ -21,7 +21,8 @@ INVALID_EMAILS = [
21
21
  # corrected in http://www.rfc-editor.org/errata_search.php?rfc=3696
22
22
  'Fred\ Bloggs_@example.com',
23
23
  'Abc\@def+@example.com',
24
- 'Joe.\\Blow@example.com'
24
+ 'Joe.\\Blow@example.com',
25
+ "invalid@invalid.invalid"
25
26
  ]
26
27
 
27
28
  VALID_EMAILS = [
@@ -1,5 +1,5 @@
1
1
  en:
2
- activerecord:
2
+ activerecord: &activerecord
3
3
  errors:
4
4
  messages:
5
5
  record_invalid: "Errors: %{errors}"
@@ -12,12 +12,22 @@ en:
12
12
  invalid_ipv6_address: "is not a valid IPv6 address"
13
13
  invalid_address: "is not a valid IP address"
14
14
  invalid_owner: "is not associated with your user"
15
+ invalid_cpf: "is not a valid CPF"
16
+ invalid_cnpj: "is not a valid CNPJ"
17
+
18
+ activemodel:
19
+ <<: *activerecord
15
20
 
16
21
  pt-BR:
17
- activerecord:
22
+ activerecord: &activerecord
18
23
  errors:
19
24
  messages:
20
25
  record_invalid: "Erros: %{errors}"
21
26
  invalid_email: "não parece ser um e-mail válido"
22
27
  invalid_url: "não parece ser uma URL válida"
23
28
  invalid_owner: "não está associado ao seu usuário"
29
+ invalid_cpf: "não é um CPF válido"
30
+ invalid_cnpj: "não é um CNPJ válido"
31
+
32
+ activemodel:
33
+ <<: *activerecord
@@ -0,0 +1,43 @@
1
+ require "spec_helper"
2
+
3
+ describe ".validates_cnpj_format_of" do
4
+ let(:model) { Class.new {
5
+ def self.name
6
+ "User"
7
+ end
8
+
9
+ include ActiveModel::Model
10
+ validates_cnpj_format_of :document
11
+ attr_accessor :document
12
+ } }
13
+
14
+ it "requires valid CNPJ" do
15
+ record = model.new(document: "invalid")
16
+ record.valid?
17
+
18
+ expect(record.errors[:document]).not_to be_empty
19
+ end
20
+
21
+ it "accepts formatted CNPJ" do
22
+ record = model.new(document: CNPJ.generate(true))
23
+ record.valid?
24
+
25
+ expect(record.errors[:document]).to be_empty
26
+ end
27
+
28
+ it "accepts stripped CNPJ" do
29
+ record = model.new(document: CNPJ.generate)
30
+ record.valid?
31
+
32
+ expect(record.errors[:document]).to be_empty
33
+ end
34
+
35
+ it "sets translated error message" do
36
+ I18n.locale = "pt-BR"
37
+
38
+ record = model.new
39
+ record.valid?
40
+
41
+ expect(record.errors[:document]).to include("não é um CNPJ válido")
42
+ end
43
+ end
@@ -0,0 +1,43 @@
1
+ require "spec_helper"
2
+
3
+ describe ".validates_cpf_format_of" do
4
+ let(:model) { Class.new {
5
+ def self.name
6
+ "User"
7
+ end
8
+
9
+ include ActiveModel::Model
10
+ validates_cpf_format_of :document
11
+ attr_accessor :document
12
+ } }
13
+
14
+ it "requires valid CPF" do
15
+ record = model.new(document: "invalid")
16
+ record.valid?
17
+
18
+ expect(record.errors[:document]).not_to be_empty
19
+ end
20
+
21
+ it "accepts formatted CPF" do
22
+ record = model.new(document: CPF.generate(true))
23
+ record.valid?
24
+
25
+ expect(record.errors[:document]).to be_empty
26
+ end
27
+
28
+ it "accepts stripped CPF" do
29
+ record = model.new(document: CPF.generate)
30
+ record.valid?
31
+
32
+ expect(record.errors[:document]).to be_empty
33
+ end
34
+
35
+ it "sets translated error message" do
36
+ I18n.locale = "pt-BR"
37
+
38
+ record = model.new
39
+ record.valid?
40
+
41
+ expect(record.errors[:document]).to include("não é um CPF válido")
42
+ end
43
+ end
data/validators.gemspec CHANGED
@@ -21,4 +21,5 @@ Gem::Specification.new do |s|
21
21
  s.add_development_dependency "sqlite3-ruby"
22
22
  s.add_development_dependency "rake"
23
23
  s.add_development_dependency "pry-meta"
24
+ s.add_development_dependency "cpf_cnpj"
24
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validators
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - '>='
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: cpf_cnpj
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description: Add some nice ActiveRecord validators.
84
98
  email:
85
99
  - fnando.vieira@gmail.com
@@ -91,11 +105,13 @@ files:
91
105
  - .rspec
92
106
  - Gemfile
93
107
  - Gemfile.lock
94
- - README.rdoc
108
+ - README.md
95
109
  - Rakefile
96
110
  - lib/validators.rb
97
111
  - lib/validators/constants.rb
98
112
  - lib/validators/ip.rb
113
+ - lib/validators/validates_cnpj_format_of.rb
114
+ - lib/validators/validates_cpf_format_of.rb
99
115
  - lib/validators/validates_datetime.rb
100
116
  - lib/validators/validates_email_format_of.rb
101
117
  - lib/validators/validates_ip_address.rb
@@ -111,6 +127,8 @@ files:
111
127
  - spec/support/translations.yml
112
128
  - spec/support/urls.rb
113
129
  - spec/validators/ip_spec.rb
130
+ - spec/validators/validates_cnpj_format_of_spec.rb
131
+ - spec/validators/validates_cpf_format_of_spec.rb
114
132
  - spec/validators/validates_datetime_spec.rb
115
133
  - spec/validators/validates_email_format_of_spec.rb
116
134
  - spec/validators/validates_ip_address_spec.rb
data/README.rdoc DELETED
@@ -1,98 +0,0 @@
1
- = Validators
2
-
3
- Add some nice Rails 3 ActiveRecord validators.
4
-
5
- == Installation
6
-
7
- gem install validators
8
-
9
- Then add it to your Gemfile:
10
-
11
- gem "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_url_format_of :site
29
- end
30
-
31
- === validates_ownership_of
32
-
33
- class Task < ActiveRecord::Base
34
- belongs_to :user
35
- belongs_to :category
36
-
37
- validates_ownership_of :category, :with => :user
38
- end
39
-
40
- user = User.find(1)
41
- another_user = User.find(2)
42
-
43
- user_category = user.categories.first
44
- another_user_category = another_user.categories.first
45
-
46
- task = user.tasks.create(:category => user_category)
47
- task.valid?
48
- #=> true
49
-
50
- task = user.tasks.create(:category => another_user_category)
51
- task.valid?
52
- #=> false
53
-
54
- === validates_ip_address
55
-
56
- class Server < ActiveRecord::Base
57
- validates_ip_address :address
58
- validates_ip_address :address, :only => :v4
59
- validates_ip_address :address, :only => :v6
60
- end
61
-
62
- === validates_datetime
63
-
64
- class Server < ActiveRecord::Base
65
- validates_datetime :starts_at
66
- validates_datetime :ends_at, :after => :starts_at, :if => :starts_at?
67
- validates_datetime :ends_at, :after => :now
68
- validates_datetime :ends_at, :before => :today
69
-
70
- validates :starts_at, :datetime => true
71
- end
72
-
73
- == Maintainer
74
-
75
- * Nando Vieira - http://simplesideias.com.br
76
-
77
- == License
78
-
79
- (The MIT License)
80
-
81
- Permission is hereby granted, free of charge, to any person obtaining
82
- a copy of this software and associated documentation files (the
83
- 'Software'), to deal in the Software without restriction, including
84
- without limitation the rights to use, copy, modify, merge, publish,
85
- distribute, sublicense, and/or sell copies of the Software, and to
86
- permit persons to whom the Software is furnished to do so, subject to
87
- the following conditions:
88
-
89
- The above copyright notice and this permission notice shall be
90
- included in all copies or substantial portions of the Software.
91
-
92
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
93
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
94
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
95
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
96
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
97
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
98
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.