portuguese_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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +55 -0
- data/Rakefile +1 -0
- data/lib/portuguese_validators/bi_validator.rb +39 -0
- data/lib/portuguese_validators/nib_validator.rb +37 -0
- data/lib/portuguese_validators/nif_validator.rb +41 -0
- data/lib/portuguese_validators/version.rb +3 -0
- data/lib/portuguese_validators.rb +9 -0
- data/portuguese_validators.gemspec +25 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cb5e228a583342dcd6b0afdfe85cadc5508054f3
|
4
|
+
data.tar.gz: 398b47b5b3f861d5a94bb5d537c5fa7647247ba8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dcb090dc850e45d3259dede6dd878975526712d55773ac07c92062e8fa94500fcf213a6c291b634a6b40ad42d7717d982f20540d495a18ea730e942ce8179c95
|
7
|
+
data.tar.gz: d74ed5af267eea869f35efd8c3482f99a86453a00c3e13aab6d7b7f1f6bc823a220fc1d9f6eab9e627c98e2a1ca3b5f255e662eae79d4a65802912075f76304b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Ricardo Otero
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# PortugueseValidators
|
2
|
+
|
3
|
+
This is a simple gem to validate NIB, NIF and BI numbers.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```console
|
10
|
+
$ gem 'portuguese_validators'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```console
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
```console
|
22
|
+
$ gem install portuguese_validators
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
You will have to include `PortugueseValidators` and then just add the validators to your model:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class MyModel < ActiveRecord::Base
|
31
|
+
include PortugueseValidators
|
32
|
+
|
33
|
+
validates :nif_field, nif: true
|
34
|
+
validates :nib_field, nib: true
|
35
|
+
validates :bi_field, bi: true
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
Note that these validators are just like the ones that Rails provide you so you can customize the error message or add an `:if` condition and all that. For example:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
class MyModel < ActiveRecord::Base
|
43
|
+
include PortugueseValidators
|
44
|
+
|
45
|
+
validates :bi_field, bi: { message: 'é um número errado' }, if: :user_pt?
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
1. Fork it ( http://github.com/rikas/portuguese_validators/fork )
|
52
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
53
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
54
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
55
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module PortugueseValidators
|
2
|
+
# Validates Portuguese BI's.
|
3
|
+
#
|
4
|
+
# A Portuguese BI number is comprised by eight digits. Between that number and the "issue" box,
|
5
|
+
# the card also has a small box with a single digit. That's the control digit. In order to
|
6
|
+
# validate a Portuguese BI you have to give the full number (including the control digit).
|
7
|
+
class BiValidator
|
8
|
+
def validate_each(record, attribute, value)
|
9
|
+
unless is_valid?(value)
|
10
|
+
record.errors[attribute] << (options[:message] || 'is not a valid BI')
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def is_valid?(number)
|
15
|
+
looks_like_bi?(number.to_s) && is_valid_bi?(number.to_s)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def is_valid_bi?(number)
|
21
|
+
control = number.split('').map { |digit| digit.to_i }
|
22
|
+
|
23
|
+
sum = 0
|
24
|
+
9.downto(2) do |num|
|
25
|
+
sum += num * control.shift
|
26
|
+
end
|
27
|
+
|
28
|
+
expected = 11 - sum % 11;
|
29
|
+
expected = 0 if expected > 9 # when the value is greater than 9 then we assume 0
|
30
|
+
|
31
|
+
expected == control.last
|
32
|
+
end
|
33
|
+
|
34
|
+
def looks_like_bi?(number)
|
35
|
+
return false unless number
|
36
|
+
number.match(/^\d{9}$/) ? true : false
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module PortugueseValidators
|
2
|
+
# Validates Portuguese bank numbers (NIB).
|
3
|
+
#
|
4
|
+
# The number is always composed by 21 where the last two form the control number.
|
5
|
+
class NibValidator < ActiveModel::EachValidator
|
6
|
+
def validate_each(record, attribute, value)
|
7
|
+
unless is_valid?(value)
|
8
|
+
record.errors[attribute] << (options[:message] || 'is not a valid NIB')
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def is_valid?(number)
|
13
|
+
number = sprintf("%021o", number) if number.kind_of?(Integer)
|
14
|
+
looks_like_nib?(number) && is_valid_nib?(number)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def is_valid_nib?(number)
|
20
|
+
nib = number.slice(0..18).split('').map { |digit| digit.to_i }
|
21
|
+
control = number.slice(19..20).to_i
|
22
|
+
|
23
|
+
sum = 0
|
24
|
+
nib.each do |num|
|
25
|
+
sum = ((sum + num) * 10) % 97
|
26
|
+
end
|
27
|
+
|
28
|
+
expected = 98 - ((sum * 10) % 97)
|
29
|
+
expected == control
|
30
|
+
end
|
31
|
+
|
32
|
+
def looks_like_nib?(number)
|
33
|
+
return false unless number
|
34
|
+
number.match(/^\d{21}$/) ? true : false
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module PortugueseValidators
|
2
|
+
# Validates Portuguese NIF's.
|
3
|
+
#
|
4
|
+
# The portuguese NIF is composed by 9 digits and it must start with 1, 2, 5, 6, 7, 8 or 9. The
|
5
|
+
# last digit is the control digit.
|
6
|
+
class NifValidator < ActiveModel::EachValidator
|
7
|
+
def validate_each(record, attribute, value)
|
8
|
+
unless is_valid?(value)
|
9
|
+
record.errors[attribute] << (options[:message] || 'is not a valid NIF')
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def is_valid?(number)
|
14
|
+
nif = number.to_s
|
15
|
+
looks_like_nif?(nif) && is_valid_nif?(nif)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def is_valid_nif?(number)
|
21
|
+
control = number.split('').map { |digit| digit.to_i }
|
22
|
+
|
23
|
+
sum = 0
|
24
|
+
9.downto(2) do |num|
|
25
|
+
sum += num * control.shift
|
26
|
+
end
|
27
|
+
|
28
|
+
expected = 11 - sum % 11
|
29
|
+
expected = 0 if expected > 9 # when the value is greater than 9 then we assume 0
|
30
|
+
|
31
|
+
expected == control.last
|
32
|
+
end
|
33
|
+
|
34
|
+
# Checks if at least the given number has the correct number of digits and starts with a valid
|
35
|
+
# digit.
|
36
|
+
def looks_like_nif?(number)
|
37
|
+
return false unless number
|
38
|
+
number.match(/^\d{9}$/) && number.match(/^[1256789]/) ? true : false
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'portuguese_validators/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'portuguese_validators'
|
8
|
+
spec.version = PortugueseValidators::VERSION
|
9
|
+
spec.authors = ['Ricardo Otero']
|
10
|
+
spec.email = ['oterosantos@gmail.com']
|
11
|
+
spec.summary = 'Validators for portuguese related numbers like NIB, NIF and BI.'
|
12
|
+
spec.description = spec.summary
|
13
|
+
spec.homepage = 'http://github.com/rikas/portuguese_validators/fork'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.5'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
|
24
|
+
spec.add_dependency 'activemodel'
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: portuguese_validators
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ricardo Otero
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activemodel
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Validators for portuguese related numbers like NIB, NIF and BI.
|
56
|
+
email:
|
57
|
+
- oterosantos@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/portuguese_validators.rb
|
68
|
+
- lib/portuguese_validators/bi_validator.rb
|
69
|
+
- lib/portuguese_validators/nib_validator.rb
|
70
|
+
- lib/portuguese_validators/nif_validator.rb
|
71
|
+
- lib/portuguese_validators/version.rb
|
72
|
+
- portuguese_validators.gemspec
|
73
|
+
homepage: http://github.com/rikas/portuguese_validators/fork
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.2.1
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Validators for portuguese related numbers like NIB, NIF and BI.
|
97
|
+
test_files: []
|