validates_cpf 2.0.0.rc1 → 3.1.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.
- checksums.yaml +7 -0
- data/.rubocop.yml +17 -0
- data/.ruby-version +1 -1
- data/.travis.yml +34 -14
- data/Gemfile +3 -1
- data/{LICENSE → LICENSE.md} +0 -0
- data/README.md +25 -8
- data/Rakefile +9 -4
- data/gemfiles/Gemfile.rails4 +1 -1
- data/gemfiles/Gemfile.rails5 +5 -0
- data/gemfiles/Gemfile.rails6 +5 -0
- data/lib/validates_cpf.rb +5 -3
- data/lib/validates_cpf/cpf.rb +15 -13
- data/lib/validates_cpf/cpf_validator.rb +20 -0
- data/lib/{require_a_valid_cpf_matcher.rb → validates_cpf/require_a_valid_cpf_matcher.rb} +9 -4
- data/lib/validates_cpf/version.rb +4 -2
- data/spec/cpf_validator_spec.rb +51 -0
- data/spec/fake_app/user.rb +7 -12
- data/spec/shoulda/matchers/active_model/require_a_valid_cpf_matcher_spec.rb +11 -0
- data/spec/spec_helper.rb +9 -2
- data/spec/validates_cpf/cpf_spec.rb +85 -64
- data/validates_cpf.gemspec +15 -10
- metadata +73 -57
- data/gemfiles/Gemfile.rails3 +0 -5
- data/lib/cpf_validator.rb +0 -6
- data/spec/fake_app/admin.rb +0 -3
- data/spec/require_a_valid_cpf_matcher_spec.rb +0 -20
- data/spec/validates_cpf_spec.rb +0 -42
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a51bcc75d139d82663a7f4de36b199df7d0544f7d38e3c94f950a71676dd8a2c
|
4
|
+
data.tar.gz: 73179bb8a799acddcc28b8f9d756052cfe40a05b659debe35b7811f37bbe6ac6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e88ccc8e127484fa95bcd7180d86e838d670c426e57ca77a07b6e461c935eeab6a152dadc0862cb3f7c149bc3eea4c52379c513c0393c01e7365b412c8a250b7
|
7
|
+
data.tar.gz: 053aff16e8a75661b4285d86206575157286da071065b698306fedc5a7f09360ffcbd4592328e998ce27f6321b89cda7ec26a0f27b4bb609268f81bb7108405d
|
data/.rubocop.yml
ADDED
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
2.7.3
|
data/.travis.yml
CHANGED
@@ -1,17 +1,37 @@
|
|
1
|
-
rvm:
|
2
|
-
- 1.8.7
|
3
|
-
- 1.9.2
|
4
|
-
- 1.9.3
|
5
|
-
- jruby
|
6
|
-
- ree
|
7
|
-
gemfile:
|
8
|
-
- gemfiles/Gemfile.rails3
|
9
|
-
- gemfiles/Gemfile.rails4
|
10
1
|
matrix:
|
11
|
-
|
12
|
-
- rvm:
|
2
|
+
include:
|
3
|
+
- rvm: 2.2
|
13
4
|
gemfile: gemfiles/Gemfile.rails4
|
14
|
-
- rvm:
|
5
|
+
- rvm: 2.2
|
6
|
+
gemfile: gemfiles/Gemfile.rails5
|
7
|
+
- rvm: 2.3
|
15
8
|
gemfile: gemfiles/Gemfile.rails4
|
16
|
-
- rvm:
|
17
|
-
gemfile: gemfiles/Gemfile.
|
9
|
+
- rvm: 2.3
|
10
|
+
gemfile: gemfiles/Gemfile.rails5
|
11
|
+
- rvm: 2.4
|
12
|
+
gemfile: gemfiles/Gemfile.rails4
|
13
|
+
- rvm: 2.4
|
14
|
+
gemfile: gemfiles/Gemfile.rails5
|
15
|
+
- rvm: 2.5
|
16
|
+
gemfile: gemfiles/Gemfile.rails4
|
17
|
+
- rvm: 2.5
|
18
|
+
gemfile: gemfiles/Gemfile.rails5
|
19
|
+
- rvm: 2.5
|
20
|
+
gemfile: gemfiles/Gemfile.rails6
|
21
|
+
- rvm: 2.6
|
22
|
+
gemfile: gemfiles/Gemfile.rails4
|
23
|
+
- rvm: 2.6
|
24
|
+
gemfile: gemfiles/Gemfile.rails5
|
25
|
+
- rvm: 2.6
|
26
|
+
gemfile: gemfiles/Gemfile.rails6
|
27
|
+
- rvm: 2.7
|
28
|
+
gemfile: gemfiles/Gemfile.rails4
|
29
|
+
- rvm: 2.7
|
30
|
+
gemfile: gemfiles/Gemfile.rails5
|
31
|
+
- rvm: 2.7
|
32
|
+
gemfile: gemfiles/Gemfile.rails6
|
33
|
+
- rvm: 3.0
|
34
|
+
gemfile: gemfiles/Gemfile.rails4
|
35
|
+
- rvm: 3.0
|
36
|
+
gemfile: gemfiles/Gemfile.rails6
|
37
|
+
script: rake complete
|
data/Gemfile
CHANGED
data/{LICENSE → LICENSE.md}
RENAMED
File without changes
|
data/README.md
CHANGED
@@ -1,8 +1,9 @@
|
|
1
|
-
# ValidatesCPF
|
1
|
+
# ValidatesCPF
|
2
2
|
|
3
3
|
[](http://badge.fury.io/rb/validates_cpf) [](http://travis-ci.org/plribeiro3000/validates_cpf) [](https://gemnasium.com/plribeiro3000/validates_cpf) [](https://coveralls.io/r/plribeiro3000/validates_cpf) [](https://codeclimate.com/github/plribeiro3000/validates_cpf)
|
4
4
|
|
5
|
-
Validates cpf and test it in a simple way
|
5
|
+
Validates cpf and test it in a simple way.
|
6
|
+
Supports ruby 2.2+ and rails 3+. For older ruby versions use the version `2`.
|
6
7
|
|
7
8
|
## Installation
|
8
9
|
|
@@ -24,16 +25,32 @@ Just use as any other validator:
|
|
24
25
|
|
25
26
|
```ruby
|
26
27
|
class User < ActiveRecord::Base
|
27
|
-
|
28
|
+
validates :cpf, cpf: true
|
28
29
|
end
|
29
30
|
```
|
30
31
|
|
31
|
-
|
32
|
+
To force the attribute to be masked pass option `mask`:
|
32
33
|
|
33
|
-
|
34
|
+
```ruby
|
35
|
+
class User < ActiveRecord::Base
|
36
|
+
validates :cpf, cpf: { mask: true }
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
## Testing
|
41
|
+
|
42
|
+
Require the matcher:
|
34
43
|
|
35
|
-
|
36
|
-
|
44
|
+
```ruby
|
45
|
+
require 'validates_cpf/require_a_valid_cpf_matcher'
|
46
|
+
```
|
47
|
+
|
48
|
+
Use in your tests:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
it { is_expected.to require_a_valid_cpf } # It will test the attribute :cpf by default
|
52
|
+
it { is_expected.to require_a_valid_cpf(:id) }
|
53
|
+
```
|
37
54
|
|
38
55
|
## Contributing
|
39
56
|
|
@@ -41,4 +58,4 @@ It will load a macher to test automatically if the gem is below shoulda-matchers
|
|
41
58
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
42
59
|
3. Commit your changes (`git commit -am 'Added some feature'`)
|
43
60
|
4. Push to the branch (`git push origin my-new-feature`)
|
44
|
-
5. Create new Pull Request
|
61
|
+
5. Create new Pull Request
|
data/Rakefile
CHANGED
@@ -1,7 +1,12 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
require 'rubocop/rake_task'
|
3
6
|
|
4
7
|
RSpec::Core::RakeTask.new
|
8
|
+
RuboCop::RakeTask.new
|
5
9
|
|
6
|
-
desc
|
7
|
-
task :
|
10
|
+
desc 'Default Task'
|
11
|
+
task default: [:spec]
|
12
|
+
task complete: %i[spec rubocop]
|
data/gemfiles/Gemfile.rails4
CHANGED
data/lib/validates_cpf.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'validates_cpf/cpf_validator'
|
4
|
+
require 'validates_cpf/require_a_valid_cpf_matcher' if defined?(::Shoulda)
|
3
5
|
|
4
6
|
module ValidatesCpf
|
5
7
|
autoload :Cpf, 'validates_cpf/cpf'
|
6
|
-
end
|
8
|
+
end
|
data/lib/validates_cpf/cpf.rb
CHANGED
@@ -1,45 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ValidatesCpf
|
2
4
|
class Cpf
|
3
5
|
def initialize(number)
|
4
6
|
number =~ /^(\d{3}\.?\d{3}\.?\d{3})-?(\d{2})$/
|
5
7
|
@number = number
|
6
|
-
@pure_number =
|
7
|
-
@result =
|
8
|
-
@cleaned_number = @pure_number.nil? ? nil : @number.gsub(/[\.-]/,
|
8
|
+
@pure_number = Regexp.last_match(1)
|
9
|
+
@result = Regexp.last_match(2)
|
10
|
+
@cleaned_number = @pure_number.nil? ? nil : @number.gsub(/[\.-]/, '')
|
9
11
|
format_number! if @pure_number
|
10
12
|
end
|
11
13
|
|
12
14
|
def valid?
|
13
|
-
return true if @number.
|
15
|
+
return true if @number.blank?
|
14
16
|
return false unless @pure_number
|
17
|
+
|
15
18
|
check_cpf
|
16
19
|
end
|
17
20
|
|
18
|
-
|
19
|
-
@number
|
20
|
-
end
|
21
|
+
attr_reader :number
|
21
22
|
|
22
23
|
private
|
23
24
|
|
24
25
|
def check_cpf
|
25
|
-
return false if @cleaned_number.length != 11
|
26
|
+
return false if @cleaned_number.length != 11 || @cleaned_number.scan(/\d/).uniq.length == 1
|
27
|
+
|
26
28
|
@result == first_digit_verifier + second_digit_verifier
|
27
29
|
end
|
28
30
|
|
29
31
|
def first_digit_verifier
|
30
32
|
sum = multiply_and_sum([10, 9, 8, 7, 6, 5, 4, 3, 2], @pure_number)
|
31
|
-
digit_verifier(sum%11).to_s
|
33
|
+
digit_verifier(sum % 11).to_s
|
32
34
|
end
|
33
35
|
|
34
36
|
def second_digit_verifier
|
35
37
|
sum = multiply_and_sum([11, 10, 9, 8, 7, 6, 5, 4, 3, 2], @pure_number + first_digit_verifier)
|
36
|
-
digit_verifier(sum%11).to_s
|
38
|
+
digit_verifier(sum % 11).to_s
|
37
39
|
end
|
38
40
|
|
39
41
|
def multiply_and_sum(array, number)
|
40
42
|
multiplied = []
|
41
43
|
number.scan(/\d{1}/).each_with_index { |e, i| multiplied[i] = e.to_i * array[i] }
|
42
|
-
multiplied.inject { |s,e| s + e }
|
44
|
+
multiplied.inject { |s, e| s + e }
|
43
45
|
end
|
44
46
|
|
45
47
|
def digit_verifier(rest)
|
@@ -48,7 +50,7 @@ module ValidatesCpf
|
|
48
50
|
|
49
51
|
def format_number!
|
50
52
|
@cleaned_number =~ /(\d{3})(\d{3})(\d{3})(\d{2})/
|
51
|
-
@number = "#{
|
53
|
+
@number = "#{Regexp.last_match(1)}.#{Regexp.last_match(2)}.#{Regexp.last_match(3)}-#{Regexp.last_match(4)}"
|
52
54
|
end
|
53
55
|
end
|
54
|
-
end
|
56
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class CpfValidator < ActiveModel::EachValidator
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
cpf = ValidatesCpf::Cpf.new(value)
|
6
|
+
|
7
|
+
if cpf.valid?
|
8
|
+
record.send("#{attribute}=", cpf.number) if options[:mask]
|
9
|
+
else
|
10
|
+
ruby_prior_version_three =
|
11
|
+
Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.0.0')
|
12
|
+
|
13
|
+
if ruby_prior_version_three
|
14
|
+
record.errors.add(attribute, :invalid, options)
|
15
|
+
else
|
16
|
+
record.errors.add(attribute, :invalid, **options)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -1,5 +1,6 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'shoulda-matchers'
|
2
|
-
require 'active_support/core_ext/array/wrap'
|
3
4
|
|
4
5
|
module Shoulda
|
5
6
|
module Matchers
|
@@ -10,14 +11,18 @@ module Shoulda
|
|
10
11
|
|
11
12
|
class RequireAValidCpfMatcher < ValidationMatcher
|
12
13
|
def description
|
13
|
-
'
|
14
|
+
'requires a valid CPF'
|
15
|
+
end
|
16
|
+
|
17
|
+
def failure_message
|
18
|
+
'does not require a valid CPF'
|
14
19
|
end
|
15
20
|
|
16
21
|
def matches?(subject)
|
17
|
-
|
22
|
+
super(subject)
|
18
23
|
disallows_value_of('123456') && allows_value_of('897.546.112-20')
|
19
24
|
end
|
20
25
|
end
|
21
26
|
end
|
22
27
|
end
|
23
|
-
end
|
28
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe CpfValidator do
|
6
|
+
let(:user) { User.new }
|
7
|
+
|
8
|
+
context 'with invalid cpf' do
|
9
|
+
before do
|
10
|
+
user.cpf = '12345'
|
11
|
+
I18n.stub(:t).with(:'activerecord.errors.models.user.attributes.cpf.invalid',
|
12
|
+
default: :'activerecord.errors.messages.invalid').and_return('is invalid')
|
13
|
+
user.valid?
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'invalidates the object' do
|
17
|
+
expect(user).not_to be_valid
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'sets an error message' do
|
21
|
+
expect(user.errors[:cpf]).to match(['is invalid'])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'with valid cpf' do
|
26
|
+
before do
|
27
|
+
user.cpf = '11144477735'
|
28
|
+
user.valid?
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'validates the object' do
|
32
|
+
expect(user).to be_valid
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'does not set an error message' do
|
36
|
+
expect(user.errors[:cpf]).to be_blank
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'with nil value' do
|
41
|
+
before { user.cpf = nil }
|
42
|
+
|
43
|
+
it 'validates the object' do
|
44
|
+
expect(user).to be_valid
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'does not set an error message' do
|
48
|
+
expect(user.errors[:cpf]).to be_blank
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/spec/fake_app/user.rb
CHANGED
@@ -1,15 +1,10 @@
|
|
1
|
-
|
2
|
-
include ActiveModel::Validations
|
3
|
-
include ActiveModel::Conversion
|
4
|
-
extend ActiveModel::Naming
|
1
|
+
# frozen_string_literal: true
|
5
2
|
|
6
|
-
|
3
|
+
class User
|
4
|
+
include ActiveModel::Model
|
7
5
|
|
8
|
-
|
6
|
+
attr_accessor :cpf, :masked_cpf, :name
|
9
7
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
8
|
+
validates :cpf, cpf: true
|
9
|
+
validates :masked_cpf, cpf: { mask: true }
|
10
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe Shoulda::Matchers::ActiveModel::RequireAValidCpfMatcher do
|
6
|
+
subject { User.new }
|
7
|
+
|
8
|
+
it { is_expected.to require_a_valid_cpf(:cpf) }
|
9
|
+
it { is_expected.to require_a_valid_cpf }
|
10
|
+
it { is_expected.not_to require_a_valid_cpf(:name) }
|
11
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,8 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'rspec'
|
2
4
|
require 'active_model'
|
3
5
|
require 'coveralls'
|
6
|
+
require 'shoulda-matchers'
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
config.include Shoulda::Matchers::ActiveModel
|
10
|
+
end
|
4
11
|
|
5
12
|
Coveralls.wear!
|
6
13
|
|
7
|
-
|
8
|
-
|
14
|
+
require File.expand_path('lib/validates_cpf')
|
15
|
+
require File.expand_path('spec/fake_app/user')
|
@@ -1,95 +1,116 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
|
-
describe ValidatesCpf::Cpf do
|
4
|
-
|
5
|
-
it "blank number" do
|
6
|
-
ValidatesCpf::Cpf.new('').should_not be_valid
|
7
|
-
end
|
5
|
+
RSpec.describe ValidatesCpf::Cpf do
|
6
|
+
let(:user) { User.new }
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
it 'rejects 345.65.67.3' do
|
9
|
+
expect(ValidatesCpf::Cpf.new('345.65.67.3')).not_to be_valid
|
10
|
+
end
|
12
11
|
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
it 'rejects 567.765-87698' do
|
13
|
+
expect(ValidatesCpf::Cpf.new('567.765-87698')).not_to be_valid
|
14
|
+
end
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
it 'rejects 345456-654-01' do
|
17
|
+
expect(ValidatesCpf::Cpf.new('345456-654-01')).not_to be_valid
|
18
|
+
end
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
it 'rejects 123456' do
|
21
|
+
expect(ValidatesCpf::Cpf.new('123456')).not_to be_valid
|
22
|
+
end
|
24
23
|
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
it 'rejects 23342345699' do
|
25
|
+
expect(ValidatesCpf::Cpf.new('23342345699')).not_to be_valid
|
26
|
+
end
|
28
27
|
|
29
|
-
|
30
|
-
|
31
|
-
|
28
|
+
it 'rejects 34.543.567-98' do
|
29
|
+
expect(ValidatesCpf::Cpf.new('34.543.567-98')).not_to be_valid
|
30
|
+
end
|
32
31
|
|
33
|
-
|
34
|
-
|
35
|
-
|
32
|
+
it 'rejects 456.676456-87' do
|
33
|
+
expect(ValidatesCpf::Cpf.new('456.676456-87')).not_to be_valid
|
34
|
+
end
|
36
35
|
|
37
|
-
|
38
|
-
|
39
|
-
|
36
|
+
it 'rejects 333333333-33' do
|
37
|
+
expect(ValidatesCpf::Cpf.new('333333333-33')).not_to be_valid
|
38
|
+
end
|
40
39
|
|
41
|
-
|
42
|
-
|
43
|
-
|
40
|
+
it 'rejects 00000000000' do
|
41
|
+
expect(ValidatesCpf::Cpf.new('00000000000')).not_to be_valid
|
42
|
+
end
|
44
43
|
|
45
|
-
|
46
|
-
|
47
|
-
|
44
|
+
it 'rejects 000.000.000-00' do
|
45
|
+
expect(ValidatesCpf::Cpf.new('000.000.000-00')).not_to be_valid
|
46
|
+
end
|
48
47
|
|
49
|
-
|
50
|
-
|
51
|
-
|
48
|
+
it 'rejects 111.444.777-3500' do
|
49
|
+
expect(ValidatesCpf::Cpf.new('111.444.777-3500')).not_to be_valid
|
50
|
+
end
|
52
51
|
|
53
|
-
|
54
|
-
|
55
|
-
end
|
52
|
+
it 'rejects 11144477735AB' do
|
53
|
+
expect(ValidatesCpf::Cpf.new('11144477735AB')).not_to be_valid
|
56
54
|
end
|
57
55
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
end
|
56
|
+
it 'accepts blank string' do
|
57
|
+
expect(ValidatesCpf::Cpf.new('')).to be_valid
|
58
|
+
end
|
62
59
|
|
63
|
-
|
64
|
-
|
65
|
-
|
60
|
+
it 'accepts nil' do
|
61
|
+
expect(ValidatesCpf::Cpf.new(nil)).to be_valid
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'accepts 111.444.777-35' do
|
65
|
+
expect(ValidatesCpf::Cpf.new('111.444.777-35')).to be_valid
|
66
|
+
end
|
66
67
|
|
67
|
-
|
68
|
-
|
68
|
+
it 'accepts 11144477735' do
|
69
|
+
expect(ValidatesCpf::Cpf.new('11144477735')).to be_valid
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'accepts 111.444777-35' do
|
73
|
+
expect(ValidatesCpf::Cpf.new('111.444777-35')).to be_valid
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'accepts 111444.777-35' do
|
77
|
+
expect(ValidatesCpf::Cpf.new('111444.777-35')).to be_valid
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'accepts 111.444.77735' do
|
81
|
+
expect(ValidatesCpf::Cpf.new('111.444.77735')).to be_valid
|
82
|
+
end
|
83
|
+
|
84
|
+
context 'with a valid value on not masked attribute' do
|
85
|
+
before do
|
86
|
+
user.cpf = '11144477735'
|
87
|
+
user.valid?
|
69
88
|
end
|
70
89
|
|
71
|
-
it
|
72
|
-
|
90
|
+
it 'leaves the original value' do
|
91
|
+
expect(user.cpf).to eq('11144477735')
|
73
92
|
end
|
93
|
+
end
|
74
94
|
|
75
|
-
|
76
|
-
|
95
|
+
context 'with a valid value on masked attribute' do
|
96
|
+
before do
|
97
|
+
user.masked_cpf = '11144477735'
|
98
|
+
user.valid?
|
77
99
|
end
|
78
100
|
|
79
|
-
it
|
80
|
-
|
101
|
+
it 'masks the original value' do
|
102
|
+
expect(user.masked_cpf).to eq('111.444.777-35')
|
81
103
|
end
|
82
104
|
end
|
83
105
|
|
84
|
-
context
|
85
|
-
|
86
|
-
|
106
|
+
context 'with an invalid value' do
|
107
|
+
before do
|
108
|
+
user.cpf = '123456'
|
109
|
+
user.valid?
|
87
110
|
end
|
88
|
-
end
|
89
111
|
|
90
|
-
|
91
|
-
|
92
|
-
ValidatesCpf::Cpf.new('123456').number.should == '123456'
|
112
|
+
it 'leaves the original value' do
|
113
|
+
expect(user.cpf).to eq('123456')
|
93
114
|
end
|
94
115
|
end
|
95
|
-
end
|
116
|
+
end
|
data/validates_cpf.gemspec
CHANGED
@@ -1,24 +1,29 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require File.expand_path('lib/validates_cpf/version', __dir__)
|
3
4
|
|
4
5
|
Gem::Specification.new do |gem|
|
5
6
|
gem.name = 'validates_cpf'
|
6
7
|
gem.version = ValidatesCpf::VERSION
|
7
|
-
gem.authors =
|
8
|
-
gem.email =
|
9
|
-
gem.summary =
|
8
|
+
gem.authors = 'Paulo Henrique Lopes Ribeiro'
|
9
|
+
gem.email = 'plribeiro3000@gmail.com'
|
10
|
+
gem.summary = 'Validates CPF and test it with matchers in a simple way.'
|
10
11
|
|
11
12
|
gem.files = `git ls-files`.split("\n")
|
12
13
|
gem.test_files = `git ls-files -- {test,spec,features,examples,gemfiles}/*`.split("\n")
|
13
|
-
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
14
|
-
gem.require_paths = %w
|
14
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
15
|
+
gem.require_paths = %w[lib]
|
16
|
+
|
17
|
+
gem.required_ruby_version = '>= 2.2'
|
15
18
|
|
16
19
|
gem.license = 'MIT'
|
17
20
|
|
21
|
+
gem.add_development_dependency 'coveralls'
|
18
22
|
gem.add_development_dependency 'rake'
|
19
23
|
gem.add_development_dependency 'rspec'
|
20
|
-
gem.add_development_dependency '
|
21
|
-
gem.add_development_dependency '
|
24
|
+
gem.add_development_dependency 'rubocop', '< 0.68'
|
25
|
+
gem.add_development_dependency 'rubocop-rspec'
|
26
|
+
gem.add_development_dependency 'shoulda-matchers'
|
22
27
|
|
23
28
|
gem.add_runtime_dependency 'activemodel'
|
24
|
-
end
|
29
|
+
end
|
metadata
CHANGED
@@ -1,156 +1,172 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_cpf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
5
|
-
prerelease: 6
|
4
|
+
version: 3.1.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Paulo Henrique Lopes Ribeiro
|
9
|
-
autorequire:
|
8
|
+
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2021-04-26 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: coveralls
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
14
27
|
- !ruby/object:Gem::Dependency
|
15
28
|
name: rake
|
16
29
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
30
|
requirements:
|
19
|
-
- -
|
31
|
+
- - ">="
|
20
32
|
- !ruby/object:Gem::Version
|
21
33
|
version: '0'
|
22
34
|
type: :development
|
23
35
|
prerelease: false
|
24
36
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
37
|
requirements:
|
27
|
-
- -
|
38
|
+
- - ">="
|
28
39
|
- !ruby/object:Gem::Version
|
29
40
|
version: '0'
|
30
41
|
- !ruby/object:Gem::Dependency
|
31
42
|
name: rspec
|
32
43
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
44
|
requirements:
|
35
|
-
- -
|
45
|
+
- - ">="
|
36
46
|
- !ruby/object:Gem::Version
|
37
47
|
version: '0'
|
38
48
|
type: :development
|
39
49
|
prerelease: false
|
40
50
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
51
|
requirements:
|
43
|
-
- -
|
52
|
+
- - ">="
|
44
53
|
- !ruby/object:Gem::Version
|
45
54
|
version: '0'
|
46
55
|
- !ruby/object:Gem::Dependency
|
47
|
-
name:
|
56
|
+
name: rubocop
|
48
57
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
58
|
requirements:
|
51
|
-
- -
|
59
|
+
- - "<"
|
52
60
|
- !ruby/object:Gem::Version
|
53
|
-
version:
|
61
|
+
version: '0.68'
|
54
62
|
type: :development
|
55
63
|
prerelease: false
|
56
64
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
65
|
requirements:
|
59
|
-
- -
|
66
|
+
- - "<"
|
60
67
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
68
|
+
version: '0.68'
|
62
69
|
- !ruby/object:Gem::Dependency
|
63
|
-
name:
|
70
|
+
name: rubocop-rspec
|
64
71
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
72
|
requirements:
|
67
|
-
- -
|
73
|
+
- - ">="
|
68
74
|
- !ruby/object:Gem::Version
|
69
75
|
version: '0'
|
70
76
|
type: :development
|
71
77
|
prerelease: false
|
72
78
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
79
|
requirements:
|
75
|
-
- -
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: shoulda-matchers
|
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
|
+
- - ">="
|
76
95
|
- !ruby/object:Gem::Version
|
77
96
|
version: '0'
|
78
97
|
- !ruby/object:Gem::Dependency
|
79
98
|
name: activemodel
|
80
99
|
requirement: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
100
|
requirements:
|
83
|
-
- -
|
101
|
+
- - ">="
|
84
102
|
- !ruby/object:Gem::Version
|
85
103
|
version: '0'
|
86
104
|
type: :runtime
|
87
105
|
prerelease: false
|
88
106
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
107
|
requirements:
|
91
|
-
- -
|
108
|
+
- - ">="
|
92
109
|
- !ruby/object:Gem::Version
|
93
110
|
version: '0'
|
94
|
-
description:
|
111
|
+
description:
|
95
112
|
email: plribeiro3000@gmail.com
|
96
113
|
executables: []
|
97
114
|
extensions: []
|
98
115
|
extra_rdoc_files: []
|
99
116
|
files:
|
100
|
-
- .gitignore
|
101
|
-
- .rspec
|
102
|
-
- .
|
103
|
-
- .ruby-
|
104
|
-
- .
|
117
|
+
- ".gitignore"
|
118
|
+
- ".rspec"
|
119
|
+
- ".rubocop.yml"
|
120
|
+
- ".ruby-gemset"
|
121
|
+
- ".ruby-version"
|
122
|
+
- ".travis.yml"
|
105
123
|
- Gemfile
|
106
|
-
- LICENSE
|
124
|
+
- LICENSE.md
|
107
125
|
- README.md
|
108
126
|
- Rakefile
|
109
|
-
- gemfiles/Gemfile.rails3
|
110
127
|
- gemfiles/Gemfile.rails4
|
111
|
-
-
|
112
|
-
-
|
128
|
+
- gemfiles/Gemfile.rails5
|
129
|
+
- gemfiles/Gemfile.rails6
|
113
130
|
- lib/validates_cpf.rb
|
114
131
|
- lib/validates_cpf/cpf.rb
|
132
|
+
- lib/validates_cpf/cpf_validator.rb
|
133
|
+
- lib/validates_cpf/require_a_valid_cpf_matcher.rb
|
115
134
|
- lib/validates_cpf/version.rb
|
116
|
-
- spec/
|
135
|
+
- spec/cpf_validator_spec.rb
|
117
136
|
- spec/fake_app/user.rb
|
118
|
-
- spec/require_a_valid_cpf_matcher_spec.rb
|
137
|
+
- spec/shoulda/matchers/active_model/require_a_valid_cpf_matcher_spec.rb
|
119
138
|
- spec/spec_helper.rb
|
120
139
|
- spec/validates_cpf/cpf_spec.rb
|
121
|
-
- spec/validates_cpf_spec.rb
|
122
140
|
- validates_cpf.gemspec
|
123
|
-
homepage:
|
141
|
+
homepage:
|
124
142
|
licenses:
|
125
143
|
- MIT
|
126
|
-
|
144
|
+
metadata: {}
|
145
|
+
post_install_message:
|
127
146
|
rdoc_options: []
|
128
147
|
require_paths:
|
129
148
|
- lib
|
130
149
|
required_ruby_version: !ruby/object:Gem::Requirement
|
131
|
-
none: false
|
132
150
|
requirements:
|
133
|
-
- -
|
151
|
+
- - ">="
|
134
152
|
- !ruby/object:Gem::Version
|
135
|
-
version: '
|
153
|
+
version: '2.2'
|
136
154
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
|
-
none: false
|
138
155
|
requirements:
|
139
|
-
- -
|
156
|
+
- - ">="
|
140
157
|
- !ruby/object:Gem::Version
|
141
|
-
version:
|
158
|
+
version: '0'
|
142
159
|
requirements: []
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
specification_version: 3
|
160
|
+
rubygems_version: 3.1.6
|
161
|
+
signing_key:
|
162
|
+
specification_version: 4
|
147
163
|
summary: Validates CPF and test it with matchers in a simple way.
|
148
164
|
test_files:
|
149
|
-
- gemfiles/Gemfile.rails3
|
150
165
|
- gemfiles/Gemfile.rails4
|
151
|
-
-
|
166
|
+
- gemfiles/Gemfile.rails5
|
167
|
+
- gemfiles/Gemfile.rails6
|
168
|
+
- spec/cpf_validator_spec.rb
|
152
169
|
- spec/fake_app/user.rb
|
153
|
-
- spec/require_a_valid_cpf_matcher_spec.rb
|
170
|
+
- spec/shoulda/matchers/active_model/require_a_valid_cpf_matcher_spec.rb
|
154
171
|
- spec/spec_helper.rb
|
155
172
|
- spec/validates_cpf/cpf_spec.rb
|
156
|
-
- spec/validates_cpf_spec.rb
|
data/gemfiles/Gemfile.rails3
DELETED
data/lib/cpf_validator.rb
DELETED
@@ -1,6 +0,0 @@
|
|
1
|
-
class CpfValidator < ActiveModel::EachValidator
|
2
|
-
def validate_each(record, attribute, value)
|
3
|
-
key = :"activerecord.errors.models.#{record.class.name.underscore}.attributes.#{attribute}.invalid"
|
4
|
-
record.errors.add(attribute, I18n.t(key, :default => :'activerecord.errors.messages.invalid')) unless ValidatesCpf::Cpf.new(value).valid?
|
5
|
-
end
|
6
|
-
end
|
data/spec/fake_app/admin.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'shoulda-matchers'
|
3
|
-
|
4
|
-
describe Shoulda::Matchers::ActiveModel::RequireAValidCpfMatcher do
|
5
|
-
before :each do
|
6
|
-
@user = User.new
|
7
|
-
end
|
8
|
-
|
9
|
-
it 'should accept on cpf' do
|
10
|
-
@user.should require_a_valid_cpf(:cpf)
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'should accept without a specified attribute' do
|
14
|
-
@user.should require_a_valid_cpf
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'should reject on name' do
|
18
|
-
@user.should_not require_a_valid_cpf(:name)
|
19
|
-
end
|
20
|
-
end
|
data/spec/validates_cpf_spec.rb
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe CpfValidator do
|
4
|
-
context 'when cpf is invalid' do
|
5
|
-
context 'on a non namespaced model' do
|
6
|
-
before :each do
|
7
|
-
@user = User.new(:cpf => '12345')
|
8
|
-
I18n.stub(:t).with(:'activerecord.errors.models.user.attributes.cpf.invalid',
|
9
|
-
:default => :'activerecord.errors.messages.invalid').and_return('is invalid')
|
10
|
-
end
|
11
|
-
|
12
|
-
it 'should set object as invalid' do
|
13
|
-
@user.valid?.should be_false
|
14
|
-
end
|
15
|
-
|
16
|
-
it 'should set an error message on attribute' do
|
17
|
-
@user.valid?
|
18
|
-
@user.errors[:cpf].should == ['is invalid']
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
context 'when cpf is valid' do
|
24
|
-
before :each do
|
25
|
-
@user = User.new(:cpf => '11144477735')
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'should set object as valid' do
|
29
|
-
@user.valid?.should be_true
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'should not set an error message on attribute' do
|
33
|
-
@user.valid?
|
34
|
-
@user.errors[:cpf].should be_blank
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'should accept a nil value' do
|
39
|
-
@user = User.new(:cpf => nil)
|
40
|
-
@user.valid?.should be_true
|
41
|
-
end
|
42
|
-
end
|