bra_document_validation 1.0.0 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +8 -0
- data/.travis.yml +4 -1
- data/CHANGELOG.md +24 -1
- data/Gemfile.lock +34 -37
- data/README.md +21 -2
- data/bin/console +2 -2
- data/bra_document_validation.gemspec +1 -1
- data/lib/bra_document_validation/cnpj_validator.rb +8 -27
- data/lib/bra_document_validation/cpf_or_cnpj_validator.rb +31 -0
- data/lib/bra_document_validation/cpf_validator.rb +8 -22
- data/lib/bra_document_validation/version.rb +1 -1
- data/lib/bra_document_validation.rb +1 -0
- metadata +10 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: be7fed5ec9bede122da6a1c7199604ac279bb2ad6fa306a1f7bae4b82b23f348
|
4
|
+
data.tar.gz: 2a15db815a15aceb31e7328da962362f6d99b5b5f285782c9a79ecd195d0a5ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 810e1af48bfc0ea25c639e119a591f6407935ecc2ee49f1db193e1d098ef1619daca3cfd34cc1ae1555828136a85a8c2f70e96ecd759af4e2e56d207b7ea89ec
|
7
|
+
data.tar.gz: 848176ec17b7f940ea092f9a28245360b24045851e97212cff3b174784a2b17d8c184466bdd0fa4aed535b22ffce52256b5052671557ed9e430cff1a4e3857cb
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
|
2
2
|
# Changelog
|
3
|
+
|
3
4
|
All notable changes to this project will be documented in this file.
|
4
5
|
|
5
6
|
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
|
@@ -7,6 +8,28 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|
7
8
|
|
8
9
|
## [Unreleased]
|
9
10
|
|
11
|
+
## [1.0.2] - 2022-01-04
|
12
|
+
|
13
|
+
### Removed
|
14
|
+
|
15
|
+
- Drop Support for Ruby 2.6 or less
|
16
|
+
|
17
|
+
### Refactory
|
18
|
+
|
19
|
+
- Refactory CPF and CNPJ Validators to use new bra_documents's methods
|
20
|
+
|
21
|
+
### Added
|
22
|
+
|
23
|
+
- Add new validator to check if an attribute is a CPF or a CNPJ
|
24
|
+
|
25
|
+
## [1.0.1] - 2020-11-08
|
26
|
+
|
27
|
+
### Fixed
|
28
|
+
|
29
|
+
- Fix Gemfile lock
|
30
|
+
|
10
31
|
## [1.0.0] - 2020-11-08
|
32
|
+
|
11
33
|
### Added
|
12
|
-
|
34
|
+
|
35
|
+
- CPF and CNPJ Validators
|
data/Gemfile.lock
CHANGED
@@ -1,52 +1,49 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
bra_document_validation (
|
4
|
+
bra_document_validation (1.0.2)
|
5
5
|
activemodel (>= 4.0)
|
6
|
-
bra_documents
|
6
|
+
bra_documents (>= 1.0.2)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
activemodel (
|
12
|
-
activesupport (=
|
13
|
-
activesupport (
|
11
|
+
activemodel (7.0.2.3)
|
12
|
+
activesupport (= 7.0.2.3)
|
13
|
+
activesupport (7.0.2.3)
|
14
14
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
15
|
-
i18n (>=
|
16
|
-
minitest (
|
17
|
-
tzinfo (~>
|
18
|
-
|
19
|
-
bra_documents (1.0.0)
|
15
|
+
i18n (>= 1.6, < 2)
|
16
|
+
minitest (>= 5.1)
|
17
|
+
tzinfo (~> 2.0)
|
18
|
+
bra_documents (1.0.2)
|
20
19
|
coderay (1.1.3)
|
21
|
-
concurrent-ruby (1.1.
|
22
|
-
diff-lcs (1.
|
23
|
-
i18n (1.
|
20
|
+
concurrent-ruby (1.1.10)
|
21
|
+
diff-lcs (1.5.0)
|
22
|
+
i18n (1.10.0)
|
24
23
|
concurrent-ruby (~> 1.0)
|
25
|
-
method_source (0.
|
26
|
-
minitest (5.
|
27
|
-
pry (0.
|
28
|
-
coderay (~> 1.1
|
29
|
-
method_source (~>
|
30
|
-
pry-nav (0.
|
31
|
-
pry (>= 0.9.10, < 0.
|
32
|
-
rake (13.0.
|
33
|
-
rspec (3.
|
34
|
-
rspec-core (~> 3.
|
35
|
-
rspec-expectations (~> 3.
|
36
|
-
rspec-mocks (~> 3.
|
37
|
-
rspec-core (3.
|
38
|
-
rspec-support (~> 3.
|
39
|
-
rspec-expectations (3.
|
24
|
+
method_source (1.0.0)
|
25
|
+
minitest (5.15.0)
|
26
|
+
pry (0.14.1)
|
27
|
+
coderay (~> 1.1)
|
28
|
+
method_source (~> 1.0)
|
29
|
+
pry-nav (1.0.0)
|
30
|
+
pry (>= 0.9.10, < 0.15)
|
31
|
+
rake (13.0.6)
|
32
|
+
rspec (3.11.0)
|
33
|
+
rspec-core (~> 3.11.0)
|
34
|
+
rspec-expectations (~> 3.11.0)
|
35
|
+
rspec-mocks (~> 3.11.0)
|
36
|
+
rspec-core (3.11.0)
|
37
|
+
rspec-support (~> 3.11.0)
|
38
|
+
rspec-expectations (3.11.0)
|
40
39
|
diff-lcs (>= 1.2.0, < 2.0)
|
41
|
-
rspec-support (~> 3.
|
42
|
-
rspec-mocks (3.
|
40
|
+
rspec-support (~> 3.11.0)
|
41
|
+
rspec-mocks (3.11.0)
|
43
42
|
diff-lcs (>= 1.2.0, < 2.0)
|
44
|
-
rspec-support (~> 3.
|
45
|
-
rspec-support (3.
|
46
|
-
|
47
|
-
|
48
|
-
thread_safe (~> 0.1)
|
49
|
-
zeitwerk (2.4.0)
|
43
|
+
rspec-support (~> 3.11.0)
|
44
|
+
rspec-support (3.11.0)
|
45
|
+
tzinfo (2.0.4)
|
46
|
+
concurrent-ruby (~> 1.0)
|
50
47
|
|
51
48
|
PLATFORMS
|
52
49
|
ruby
|
@@ -59,4 +56,4 @@ DEPENDENCIES
|
|
59
56
|
rspec
|
60
57
|
|
61
58
|
BUNDLED WITH
|
62
|
-
2.
|
59
|
+
2.2.31
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# BraDocumentValidation
|
2
2
|
|
3
|
+
[![Build Status](https://travis-ci.com/bvicenzo/bra_document_validation.svg?branch=master)](https://travis-ci.com/bvicenzo/bra_document_validation)
|
4
|
+
|
3
5
|
This gem allows to validate Brazilian documents, CPF and CNPJ, with format and digit verification.
|
4
6
|
|
5
7
|
## Installation
|
@@ -12,17 +14,32 @@ gem 'bra_document_validation'
|
|
12
14
|
|
13
15
|
And then execute:
|
14
16
|
|
15
|
-
|
17
|
+
```sh
|
18
|
+
bundle install
|
19
|
+
```
|
16
20
|
|
17
21
|
Or install it yourself as:
|
18
22
|
|
19
|
-
|
23
|
+
```sh
|
24
|
+
gem install bra_document_validation
|
25
|
+
```
|
20
26
|
|
21
27
|
## Usage
|
22
28
|
|
23
29
|
```rb
|
24
30
|
validates :document_number, 'bra_document_validation/cpf': true
|
25
31
|
validates :document_number, 'bra_document_validation/cnpj': true
|
32
|
+
validates :document_number, 'bra_document_validation/cpf_or_cnpj': true
|
33
|
+
```
|
34
|
+
|
35
|
+
You might need add CPF and CNPJ acronyms to inflactions.
|
36
|
+
|
37
|
+
```rb
|
38
|
+
# config/initializers/inflections.rb
|
39
|
+
ActiveSupport::Inflector.inflections do |inflect|
|
40
|
+
inflect.acronym 'CPF'
|
41
|
+
inflect.acronym 'CNPJ'
|
42
|
+
end
|
26
43
|
```
|
27
44
|
|
28
45
|
# Format
|
@@ -33,6 +50,7 @@ However you can pass options if your number is formatted.
|
|
33
50
|
```rb
|
34
51
|
validates :document_number, 'bra_document_validation/cpf': { formatted: true }
|
35
52
|
validates :document_number, 'bra_document_validation/cnpj': { formatted: false }
|
53
|
+
validates :document_number, 'bra_document_validation/cpf_or_cnpj': { formatted: false }
|
36
54
|
```
|
37
55
|
|
38
56
|
# Messaging
|
@@ -44,6 +62,7 @@ However you can add a custom message that substitute the above messages.
|
|
44
62
|
```rb
|
45
63
|
validates :document_number, 'bra_document_validation/cpf': { message: :invalid }
|
46
64
|
validates :document_number, 'bra_document_validation/cnpj': { message: 'A custom message' }
|
65
|
+
validates :document_number, 'bra_document_validation/cpf_or_cnpj': { message: :invalid }
|
47
66
|
```
|
48
67
|
|
49
68
|
## Development
|
data/bin/console
CHANGED
@@ -7,8 +7,8 @@ require 'bra_document_validation'
|
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
8
8
|
|
9
9
|
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
|
11
|
-
|
10
|
+
require 'pry'
|
11
|
+
require 'pry-nav'
|
12
12
|
|
13
13
|
require 'irb'
|
14
14
|
IRB.start(__FILE__)
|
@@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
|
|
28
28
|
spec.require_paths = ['lib']
|
29
29
|
|
30
30
|
spec.add_runtime_dependency 'activemodel', '>= 4.0'
|
31
|
-
spec.add_runtime_dependency 'bra_documents'
|
31
|
+
spec.add_runtime_dependency 'bra_documents', '>= 1.0.2'
|
32
32
|
|
33
33
|
spec.add_development_dependency 'pry'
|
34
34
|
spec.add_development_dependency 'pry-nav'
|
@@ -2,43 +2,24 @@
|
|
2
2
|
|
3
3
|
module BraDocumentValidation
|
4
4
|
class CNPJValidator < ActiveModel::EachValidator
|
5
|
-
FORMATTED_CNPJ_PATTERN = /\A(\d{2}\.\d{3}\.\d{3}\/\d{4})-(\d{2})\Z/.freeze
|
6
|
-
RAW_CNPJ_PATTERN = /\A\d{14}\Z/.freeze
|
7
|
-
NOT_NUMBER_PATTERN = /\D/.freeze
|
8
|
-
|
9
5
|
def validate_each(record, attribute, value)
|
10
|
-
return record.errors.add(attribute, error_message(:invalid_format)) unless
|
11
|
-
full_number = only_numbers_for(value.to_s)
|
12
|
-
record.errors.add(attribute, error_message(:invalid_verification_digit)) if black_listed?(full_number) || !digit_verified?(full_number)
|
13
|
-
end
|
14
|
-
|
15
|
-
private
|
6
|
+
return record.errors.add(attribute, error_message(:invalid_format)) unless valid_format?(value)
|
16
7
|
|
17
|
-
|
18
|
-
company_number, matrix_subsidiary_number = numbers_for(full_number)
|
19
|
-
|
20
|
-
full_number == BraDocuments::CNPJGenerator.generate(company_number: company_number, matrix_subsidiary_number: matrix_subsidiary_number)
|
8
|
+
record.errors.add(attribute, error_message(:invalid_verification_digit)) unless valid_verification_digit?(value)
|
21
9
|
end
|
22
10
|
|
23
|
-
|
24
|
-
number = value.gsub(NOT_NUMBER_PATTERN, '').chars
|
25
|
-
|
26
|
-
[
|
27
|
-
number.shift(BraDocuments::CNPJGenerator::COMPANY_NUMBER_SIZE).join,
|
28
|
-
number.shift(BraDocuments::CNPJGenerator::MATRIX_SUBSIDIARY_SIZE).join
|
29
|
-
]
|
30
|
-
end
|
11
|
+
private
|
31
12
|
|
32
|
-
def
|
33
|
-
|
13
|
+
def valid_format?(document)
|
14
|
+
BraDocuments::Matcher.match?(document.to_s, kind: :cnpj, mode: document_format)
|
34
15
|
end
|
35
16
|
|
36
|
-
def
|
37
|
-
|
17
|
+
def valid_verification_digit?(document)
|
18
|
+
BraDocuments::CNPJGenerator.valid_verification_digit?(document: document.to_s)
|
38
19
|
end
|
39
20
|
|
40
21
|
def document_format
|
41
|
-
options[:formatted] ?
|
22
|
+
options[:formatted] ? :formatted : :raw
|
42
23
|
end
|
43
24
|
|
44
25
|
def error_message(default_message)
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BraDocumentValidation
|
4
|
+
class CPFOrCNPJValidator < ActiveModel::EachValidator
|
5
|
+
def validate_each(record, attribute, value)
|
6
|
+
return record.errors.add(attribute, error_message(:invalid_format)) unless valid_format?(value)
|
7
|
+
|
8
|
+
record.errors.add(attribute, error_message(:invalid_verification_digit)) unless valid_verification_digit?(value)
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def valid_format?(document)
|
14
|
+
BraDocuments::Matcher.match?(document.to_s, kind: :cpf, mode: document_format) ||
|
15
|
+
BraDocuments::Matcher.match?(document.to_s, kind: :cnpj, mode: document_format)
|
16
|
+
end
|
17
|
+
|
18
|
+
def valid_verification_digit?(document)
|
19
|
+
BraDocuments::CPFGenerator.valid_verification_digit?(document: document.to_s) ||
|
20
|
+
BraDocuments::CNPJGenerator.valid_verification_digit?(document: document.to_s)
|
21
|
+
end
|
22
|
+
|
23
|
+
def document_format
|
24
|
+
options[:formatted] ? :formatted : :raw
|
25
|
+
end
|
26
|
+
|
27
|
+
def error_message(default_message)
|
28
|
+
options.fetch(:message, default_message)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -2,38 +2,24 @@
|
|
2
2
|
|
3
3
|
module BraDocumentValidation
|
4
4
|
class CPFValidator < ActiveModel::EachValidator
|
5
|
-
FORMATTED_CPF_PATTERN = /^(\d{3}\.\d{3}\.\d{3})-(\d{2})$/.freeze
|
6
|
-
RAW_CPF_PATTERN = /\A\d{11}\z/
|
7
|
-
NOT_NUMBER_PATTERN = /\D/.freeze
|
8
|
-
|
9
5
|
def validate_each(record, attribute, value)
|
10
|
-
return record.errors.add(attribute, error_message(:invalid_format)) unless
|
11
|
-
full_number = only_numbers_for(value.to_s)
|
12
|
-
record.errors.add(attribute, error_message(:invalid_verification_digit)) if black_listed?(full_number) || !digit_verified?(full_number)
|
13
|
-
end
|
14
|
-
|
15
|
-
private
|
6
|
+
return record.errors.add(attribute, error_message(:invalid_format)) unless valid_format?(value)
|
16
7
|
|
17
|
-
|
18
|
-
person_number = number_without_verifying_digits_for(number)
|
19
|
-
|
20
|
-
number == BraDocuments::CPFGenerator.generate(person_number: person_number)
|
8
|
+
record.errors.add(attribute, error_message(:invalid_verification_digit)) unless valid_verification_digit?(value)
|
21
9
|
end
|
22
10
|
|
23
|
-
|
24
|
-
number.chars.shift(BraDocuments::CPFGenerator::PERSON_NUMBER_SIZE).join
|
25
|
-
end
|
11
|
+
private
|
26
12
|
|
27
|
-
def
|
28
|
-
|
13
|
+
def valid_format?(document)
|
14
|
+
BraDocuments::Matcher.match?(document.to_s, kind: :cpf, mode: document_format)
|
29
15
|
end
|
30
16
|
|
31
|
-
def
|
32
|
-
|
17
|
+
def valid_verification_digit?(document)
|
18
|
+
BraDocuments::CPFGenerator.valid_verification_digit?(document: document.to_s)
|
33
19
|
end
|
34
20
|
|
35
21
|
def document_format
|
36
|
-
options[:formatted] ?
|
22
|
+
options[:formatted] ? :formatted : :raw
|
37
23
|
end
|
38
24
|
|
39
25
|
def error_message(default_message)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bra_document_validation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bruno Vicenzo
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-04-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 1.0.2
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 1.0.2
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: pry
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -101,6 +101,7 @@ executables: []
|
|
101
101
|
extensions: []
|
102
102
|
extra_rdoc_files: []
|
103
103
|
files:
|
104
|
+
- ".github/dependabot.yml"
|
104
105
|
- ".gitignore"
|
105
106
|
- ".rspec"
|
106
107
|
- ".travis.yml"
|
@@ -115,6 +116,7 @@ files:
|
|
115
116
|
- bra_document_validation.gemspec
|
116
117
|
- lib/bra_document_validation.rb
|
117
118
|
- lib/bra_document_validation/cnpj_validator.rb
|
119
|
+
- lib/bra_document_validation/cpf_or_cnpj_validator.rb
|
118
120
|
- lib/bra_document_validation/cpf_validator.rb
|
119
121
|
- lib/bra_document_validation/version.rb
|
120
122
|
homepage: https://github.com/bvicenzo/bra_document_validation
|
@@ -125,7 +127,7 @@ metadata:
|
|
125
127
|
homepage_uri: https://github.com/bvicenzo/bra_document_validation
|
126
128
|
source_code_uri: https://github.com/bvicenzo/bra_document_validation
|
127
129
|
changelog_uri: https://github.com/bvicenzo/bra_document_validation/blob/master/CHANGELOG.md
|
128
|
-
post_install_message:
|
130
|
+
post_install_message:
|
129
131
|
rdoc_options: []
|
130
132
|
require_paths:
|
131
133
|
- lib
|
@@ -140,8 +142,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
142
|
- !ruby/object:Gem::Version
|
141
143
|
version: '0'
|
142
144
|
requirements: []
|
143
|
-
rubygems_version: 3.
|
144
|
-
signing_key:
|
145
|
+
rubygems_version: 3.2.31
|
146
|
+
signing_key:
|
145
147
|
specification_version: 4
|
146
148
|
summary: This gem add ActiveModel validations to Brazilian documents.
|
147
149
|
test_files: []
|