validates_spanish_documents 1.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 +18 -0
- data/.gitlab-ci.yml +31 -0
- data/.rubocop.yml +4 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +62 -0
- data/Rakefile +1 -0
- data/lib/validates_spanish_documents.rb +250 -0
- data/lib/validates_spanish_documents/version.rb +3 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/validations_spec.rb +176 -0
- data/validates_spanish_documents.gemspec +22 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a7ad05e14e4f298b3e564a3434ede9bf53257269f43c8a099c5fcbd971bfb437
|
4
|
+
data.tar.gz: 5084889c94aeafe35b3a69014076337c24f02ab3cf7088abf42ccc133e8a92ad
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 33c2b4d28b169f1ef275230ec3c6f19a91a65611fa1aa0ddfce2a4c679d71d5bd0be841b918334c47596ab02f4bcaef0e44339002cd12487f8841592e111c6bf
|
7
|
+
data.tar.gz: f3d5a77a08dd01f593799d3cad529bd14d4241e6d01f5b932c05acc00026116847eac107eba775258543a2d399f311078128b646ac2bbe98547e7f95ec24792e
|
data/.gitignore
ADDED
data/.gitlab-ci.yml
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
image: nosolosoftware/testing:16.04
|
2
|
+
|
3
|
+
cache:
|
4
|
+
paths:
|
5
|
+
- vendor/bundle
|
6
|
+
|
7
|
+
stages:
|
8
|
+
- testing
|
9
|
+
- audit
|
10
|
+
|
11
|
+
cache:
|
12
|
+
paths:
|
13
|
+
- vendor/bundle
|
14
|
+
|
15
|
+
rspec:
|
16
|
+
stage: testing
|
17
|
+
before_script:
|
18
|
+
- mongod --smallfiles --fork --syslog --quiet
|
19
|
+
- bundle install --without production tools --jobs $(nproc)
|
20
|
+
script:
|
21
|
+
- bundle exec rspec
|
22
|
+
|
23
|
+
rubocop:
|
24
|
+
stage: audit
|
25
|
+
allow_failure: true
|
26
|
+
before_script:
|
27
|
+
- gem install rubocop --no-ri --no-rdoc
|
28
|
+
script:
|
29
|
+
- rubocop -D lib/ spec/
|
30
|
+
|
31
|
+
|
data/.rubocop.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Rafael Jurado
|
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,62 @@
|
|
1
|
+
# ValidatesSpanishDocuments
|
2
|
+
|
3
|
+
Common validations of spanish identification documents.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'validates_spanish_documents'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install validates_spanish_documents
|
18
|
+
|
19
|
+
## Methods
|
20
|
+
|
21
|
+
* **validate_dni(field_name, options)**: check if it is valid dni
|
22
|
+
* **validate_nie(field_name, options)**: check if it is valid nie
|
23
|
+
* **validate_cif(field_name, options)**: check if it is valid cif
|
24
|
+
* **validate_nif(field_name, options)**: check if it is valid dni, nie or cif
|
25
|
+
* **validate_person_nif(field_name, options)**: check if it is valid dni or nie
|
26
|
+
|
27
|
+
## Options
|
28
|
+
|
29
|
+
* **if**: name of boolean field or method to conditional validation
|
30
|
+
|
31
|
+
## Example
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class Entity
|
35
|
+
include Mongoid::Document
|
36
|
+
include ValidatesSpanishDocuments
|
37
|
+
|
38
|
+
field :dni
|
39
|
+
field :nie
|
40
|
+
field :cif
|
41
|
+
field :nif
|
42
|
+
field :person_nif
|
43
|
+
|
44
|
+
validate_dni :dni
|
45
|
+
validate_nie :nie
|
46
|
+
validate_cif :cif
|
47
|
+
validate_nif :nif, if: :condition
|
48
|
+
validate_person_nif :person_nif
|
49
|
+
|
50
|
+
def condition
|
51
|
+
return true
|
52
|
+
end
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
1. Create a personal fork of the project
|
59
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
60
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
61
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
62
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,250 @@
|
|
1
|
+
require 'validates_spanish_documents/version'
|
2
|
+
|
3
|
+
##
|
4
|
+
# Add common valiations methods
|
5
|
+
#
|
6
|
+
module ValidatesSpanishDocuments
|
7
|
+
LETTERS_DNI = %w[T R W A G M Y F P D X B N J Z S Q V H L C K E].freeze
|
8
|
+
REGEX_DNI = /^(\d{8})\-?(#{LETTERS_DNI.join('|')})$/
|
9
|
+
|
10
|
+
LETTERS_NIE = %w[X Y Z].freeze
|
11
|
+
REGEX_NIE = /^(#{LETTERS_NIE.join('|')})\-?(\d{7})(#{LETTERS_DNI.join('|')})$/
|
12
|
+
|
13
|
+
LETTERS_CIF = %w[A B C D E F G H J N P Q R S U V W].freeze
|
14
|
+
LETTERS_CIF_NUMBER = %w[P Q S W].freeze
|
15
|
+
LETTERS_CIF_CONTROL = %w[J A B C D E F G H I].freeze
|
16
|
+
REGEX_CIF = /^(#{LETTERS_CIF.join('|')})\-?(\d{7})\-?(\d|#{LETTERS_CIF_CONTROL.join('|')})$/
|
17
|
+
|
18
|
+
# rubocop:disable Metrics/ModuleLength
|
19
|
+
module InstanceMethods
|
20
|
+
private
|
21
|
+
|
22
|
+
# Check validate nif
|
23
|
+
def nif_valid?(field_name)
|
24
|
+
if send(field_name).match?(REGEX_DNI)
|
25
|
+
dni_valid?(field_name)
|
26
|
+
elsif send(field_name).match?(REGEX_NIE)
|
27
|
+
nie_valid?(field_name)
|
28
|
+
else
|
29
|
+
cif_valid?(field_name)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Check validate PERSON NIF
|
34
|
+
def person_nif_valid?(field_name)
|
35
|
+
if send(field_name).match?(REGEX_DNI)
|
36
|
+
dni_valid?(field_name)
|
37
|
+
else
|
38
|
+
nie_valid?(field_name)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Check validate DNI
|
43
|
+
def dni_valid?(field_name)
|
44
|
+
if send(field_name) =~ REGEX_DNI
|
45
|
+
number = Regexp.last_match(1).to_i
|
46
|
+
position = number % 23
|
47
|
+
control_code = Regexp.last_match(2)
|
48
|
+
|
49
|
+
return true if control_code == LETTERS_DNI[position]
|
50
|
+
end
|
51
|
+
|
52
|
+
false
|
53
|
+
end
|
54
|
+
|
55
|
+
# Check validate NIE
|
56
|
+
def nie_valid?(field_name)
|
57
|
+
if send(field_name) =~ REGEX_NIE
|
58
|
+
number_first = LETTERS_NIE.index(Regexp.last_match(1))
|
59
|
+
number = (number_first.to_s + Regexp.last_match(2)).to_i
|
60
|
+
position = number % 23
|
61
|
+
control_code = Regexp.last_match(3)
|
62
|
+
|
63
|
+
return true if control_code == LETTERS_DNI[position]
|
64
|
+
end
|
65
|
+
|
66
|
+
false
|
67
|
+
end
|
68
|
+
|
69
|
+
# Check validate CIF
|
70
|
+
def cif_valid?(field_name)
|
71
|
+
if send(field_name) =~ REGEX_CIF
|
72
|
+
number = Regexp.last_match(2)
|
73
|
+
first_letter = Regexp.last_match(1)
|
74
|
+
province_code = number[0..1]
|
75
|
+
actual_control = Regexp.last_match(3)
|
76
|
+
|
77
|
+
total = number
|
78
|
+
.split('')
|
79
|
+
.each_with_index
|
80
|
+
.inject(0) do |acc, (element, index)|
|
81
|
+
acc + if index.even?
|
82
|
+
(element.to_i * 2).digits.inject(:+)
|
83
|
+
else
|
84
|
+
element.to_i
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
decimal = total.digits.first
|
89
|
+
expected_control = decimal != 0 ? 10 - decimal : decimal
|
90
|
+
|
91
|
+
# Control code must be a letter
|
92
|
+
return LETTERS_CIF_CONTROL[expected_control] if LETTERS_CIF_NUMBER.include?(first_letter) ||
|
93
|
+
province_code == '00'
|
94
|
+
|
95
|
+
# Control code will be a number or a letter
|
96
|
+
return [expected_control.to_s,
|
97
|
+
LETTERS_CIF_CONTROL[expected_control]].include?(actual_control)
|
98
|
+
end
|
99
|
+
|
100
|
+
false
|
101
|
+
end
|
102
|
+
|
103
|
+
# Validate NIF method
|
104
|
+
def validate_nss_nif(field_name)
|
105
|
+
unless nif_valid?(field_name)
|
106
|
+
errors.add field_name, :invalid
|
107
|
+
false
|
108
|
+
end
|
109
|
+
|
110
|
+
true
|
111
|
+
end
|
112
|
+
|
113
|
+
# Validate NIF method
|
114
|
+
def validate_nss_person_nif(field_name)
|
115
|
+
unless person_nif_valid?(field_name)
|
116
|
+
errors.add field_name, :invalid
|
117
|
+
false
|
118
|
+
end
|
119
|
+
|
120
|
+
true
|
121
|
+
end
|
122
|
+
|
123
|
+
# Validate DNI method
|
124
|
+
def validate_nss_dni(field_name)
|
125
|
+
unless dni_valid?(field_name)
|
126
|
+
errors.add field_name, :invalid
|
127
|
+
false
|
128
|
+
end
|
129
|
+
|
130
|
+
true
|
131
|
+
end
|
132
|
+
|
133
|
+
# Validate NIE method
|
134
|
+
def validate_nss_nie(field_name)
|
135
|
+
unless nie_valid?(field_name)
|
136
|
+
errors.add field_name, :invalid
|
137
|
+
false
|
138
|
+
end
|
139
|
+
|
140
|
+
true
|
141
|
+
end
|
142
|
+
|
143
|
+
# Validate CIF method
|
144
|
+
# https://es.wikipedia.org/wiki/C%C3%B3digo_de_identificaci%C3%B3n_fiscal
|
145
|
+
def validate_nss_cif(field_name)
|
146
|
+
unless cif_valid?(field_name)
|
147
|
+
errors.add field_name, :invalid
|
148
|
+
false
|
149
|
+
end
|
150
|
+
|
151
|
+
true
|
152
|
+
end
|
153
|
+
|
154
|
+
# Call all validations
|
155
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
156
|
+
def nss_validators
|
157
|
+
self.class.nss_validations.each do |item|
|
158
|
+
next if send(item[:field_name]).blank? || (item[:if] && !check_if(item[:if]))
|
159
|
+
|
160
|
+
case item[:validation]
|
161
|
+
when :nif then send(:validate_nss_nif, item[:field_name])
|
162
|
+
when :dni then send(:validate_nss_dni, item[:field_name])
|
163
|
+
when :cif then send(:validate_nss_cif, item[:field_name])
|
164
|
+
when :nie then send(:validate_nss_nie, item[:field_name])
|
165
|
+
when :person_nif then send(:validate_nss_person_nif, item[:field_name])
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
170
|
+
|
171
|
+
def check_if(condition)
|
172
|
+
return send(condition) if condition.class == String || condition.class == Symbol
|
173
|
+
return condition.call if condition.class == Proc
|
174
|
+
|
175
|
+
true
|
176
|
+
end
|
177
|
+
|
178
|
+
class << self
|
179
|
+
# Call nss validators
|
180
|
+
def included(base)
|
181
|
+
base.send(:validate, :nss_validators)
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
module ClassMethods
|
187
|
+
##
|
188
|
+
# Add NIF validation to field
|
189
|
+
#
|
190
|
+
def validate_nif(field_name, options={})
|
191
|
+
add_nss_validation({field_name: field_name, validation: :nif}.merge(options))
|
192
|
+
end
|
193
|
+
|
194
|
+
##
|
195
|
+
# Add DNI validation to field
|
196
|
+
#
|
197
|
+
def validate_dni(field_name, options={})
|
198
|
+
add_nss_validation({field_name: field_name, validation: :dni}.merge(options))
|
199
|
+
end
|
200
|
+
|
201
|
+
##
|
202
|
+
# Add NIE validation to field
|
203
|
+
#
|
204
|
+
def validate_nie(field_name, options={})
|
205
|
+
add_nss_validation({field_name: field_name, validation: :nie}.merge(options))
|
206
|
+
end
|
207
|
+
|
208
|
+
##
|
209
|
+
# Add CIF validation to field
|
210
|
+
#
|
211
|
+
def validate_cif(field_name, options={})
|
212
|
+
add_nss_validation({field_name: field_name, validation: :cif}.merge(options))
|
213
|
+
end
|
214
|
+
|
215
|
+
##
|
216
|
+
# Add DNI/NIE validation to field
|
217
|
+
#
|
218
|
+
def validate_person_nif(field_name, options={})
|
219
|
+
add_nss_validation({field_name: field_name, validation: :person_nif}.merge(options))
|
220
|
+
end
|
221
|
+
|
222
|
+
##
|
223
|
+
# Add validation
|
224
|
+
#
|
225
|
+
def add_nss_validation(options)
|
226
|
+
@nss_validations ||= []
|
227
|
+
@nss_validations.push(options)
|
228
|
+
end
|
229
|
+
|
230
|
+
##
|
231
|
+
# Return nss validations
|
232
|
+
#
|
233
|
+
def nss_validations
|
234
|
+
if superclass.methods.include? :nss_validations
|
235
|
+
superclass.nss_validations + (@nss_validations || [])
|
236
|
+
else
|
237
|
+
@nss_validations || []
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
##
|
243
|
+
# Adds class methods
|
244
|
+
#
|
245
|
+
def self.included(base)
|
246
|
+
base.extend(ClassMethods)
|
247
|
+
base.include(InstanceMethods)
|
248
|
+
end
|
249
|
+
end
|
250
|
+
# rubocop:enable Metrics/ModuleLength
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.shared_context "validations" do
|
4
|
+
let(:valid_dni) { '29032146M' }
|
5
|
+
let(:valid_nie) { 'Y5284410J' }
|
6
|
+
let(:valid_cif) { 'R8693558B' }
|
7
|
+
let(:valid_cif_number) { 'A89464903' }
|
8
|
+
let(:invalid_cif_valid_control_invalid_letter) { 'X52168135' }
|
9
|
+
let(:invalid_dni) { valid_dni + 'X' }
|
10
|
+
let(:invalid_nie) { valid_dni + 'X' }
|
11
|
+
let(:invalid_cif) { valid_dni + 'X' }
|
12
|
+
|
13
|
+
describe 'validation' do
|
14
|
+
describe 'dni' do
|
15
|
+
it 'with valid' do
|
16
|
+
expect(Entity.new(dni: valid_dni).valid?).to be_truthy
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'with multiple valid' do
|
20
|
+
entity = Entity.new(dni: "#{valid_dni} #{valid_dni}")
|
21
|
+
expect(entity.valid?).to be_falsey
|
22
|
+
expect(entity.errors[:dni]).not_to be_empty
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'with invalid letter' do
|
26
|
+
entity = Entity.new(dni: '29032146X')
|
27
|
+
expect(entity.valid?).to be_falsey
|
28
|
+
expect(entity.errors[:dni]).not_to be_empty
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'with invalid' do
|
32
|
+
expect(Entity.new(dni: invalid_dni).valid?).to be_falsey
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe 'nie' do
|
37
|
+
it 'with valid' do
|
38
|
+
expect(Entity.new(nie: valid_nie).valid?).to be_truthy
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'with multiple valid' do
|
42
|
+
entity = Entity.new(nie: "#{valid_nie} #{valid_nie}")
|
43
|
+
expect(entity.valid?).to be_falsey
|
44
|
+
expect(entity.errors[:nie]).not_to be_empty
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'with invalid letter' do
|
48
|
+
entity = Entity.new(nie: 'X0709831Q')
|
49
|
+
expect(entity.valid?).to be_falsey
|
50
|
+
expect(entity.errors[:nie]).not_to be_empty
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'with invalid' do
|
54
|
+
expect(Entity.new(nie: invalid_nie).valid?).to be_falsey
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe 'cif' do
|
59
|
+
context 'when is valid' do
|
60
|
+
it 'has control code a letter' do
|
61
|
+
expect(Entity.new(cif: valid_cif).valid?).to be_truthy
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'has control code a number' do
|
65
|
+
expect(Entity.new(cif: valid_cif_number).valid?).to be_truthy
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context 'when is invalid' do
|
70
|
+
it 'with valid control_code but invalid first_letter' do
|
71
|
+
entity = Entity.new(cif: invalid_cif_valid_control_invalid_letter)
|
72
|
+
expect(entity.valid?).to be_falsey
|
73
|
+
expect(entity.errors[:cif]).not_to be_empty
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'with multiple valid' do
|
77
|
+
entity = Entity.new(cif: "#{valid_cif} #{valid_cif}")
|
78
|
+
expect(entity.valid?).to be_falsey
|
79
|
+
expect(entity.errors[:cif]).not_to be_empty
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'with invalid letter' do
|
83
|
+
# invalid last number
|
84
|
+
entity = Entity.new(cif: 'E93339490')
|
85
|
+
expect(entity.valid?).to be_falsey
|
86
|
+
expect(entity.errors[:cif]).not_to be_empty
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'with invalid' do
|
90
|
+
expect(Entity.new(cif: invalid_cif).valid?).to be_falsey
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'validate person_nif' do
|
96
|
+
expect(Entity.new(person_nif: valid_dni).valid?).to be_truthy
|
97
|
+
expect(Entity.new(person_nif: valid_nie).valid?).to be_truthy
|
98
|
+
expect(Entity.new(person_nif: valid_cif).valid?).to be_falsey
|
99
|
+
expect(Entity.new(person_nif: invalid_dni).valid?).to be_falsey
|
100
|
+
expect(Entity.new(person_nif: invalid_nie).valid?).to be_falsey
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'validate nif' do
|
104
|
+
expect(Entity.new(nif: valid_dni).valid?).to be_truthy
|
105
|
+
expect(Entity.new(nif: valid_nie).valid?).to be_truthy
|
106
|
+
expect(Entity.new(nif: valid_cif).valid?).to be_truthy
|
107
|
+
expect(Entity.new(nif: invalid_dni).valid?).to be_falsey
|
108
|
+
expect(Entity.new(nif: invalid_nie).valid?).to be_falsey
|
109
|
+
expect(Entity.new(nif: invalid_cif).valid?).to be_falsey
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe 'functinality' do
|
114
|
+
it 'allow heritage' do
|
115
|
+
class MyEntity < Entity
|
116
|
+
end
|
117
|
+
|
118
|
+
# invalid dni letter (last)
|
119
|
+
entity = MyEntity.new(dni: '29032146X')
|
120
|
+
expect(entity.valid?).to be_falsey
|
121
|
+
expect(entity.errors[:dni]).not_to be_empty
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
describe ValidatesSpanishDocuments do
|
127
|
+
context 'when use with Mongoid' do
|
128
|
+
class Entity
|
129
|
+
include Mongoid::Document
|
130
|
+
include ValidatesSpanishDocuments
|
131
|
+
|
132
|
+
field :dni
|
133
|
+
field :nie
|
134
|
+
field :cif
|
135
|
+
field :nif
|
136
|
+
field :person_nif
|
137
|
+
|
138
|
+
validate_dni :dni, if: -> { true }
|
139
|
+
validate_nie :nie
|
140
|
+
validate_cif :cif
|
141
|
+
validate_nif :nif
|
142
|
+
validate_person_nif :person_nif
|
143
|
+
end
|
144
|
+
|
145
|
+
include_context 'validations'
|
146
|
+
end
|
147
|
+
|
148
|
+
context 'when use with ActiveModel::Validations' do
|
149
|
+
class Entity
|
150
|
+
include ActiveModel::Validations
|
151
|
+
include ValidatesSpanishDocuments
|
152
|
+
|
153
|
+
validate_dni :dni, if: 'condition'
|
154
|
+
validate_nie :nie
|
155
|
+
validate_cif :cif
|
156
|
+
validate_nif :nif
|
157
|
+
validate_person_nif :person_nif
|
158
|
+
|
159
|
+
attr_reader :dni, :nie, :cif, :nif, :person_nif
|
160
|
+
|
161
|
+
def initialize(params)
|
162
|
+
@dni = params[:dni]
|
163
|
+
@nie = params[:nie]
|
164
|
+
@cif = params[:cif]
|
165
|
+
@nif = params[:nif]
|
166
|
+
@person_nif = params[:person_nif]
|
167
|
+
end
|
168
|
+
|
169
|
+
def condition
|
170
|
+
true
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
include_context 'validations'
|
175
|
+
end
|
176
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'validates_spanish_documents'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'validates_spanish_documents'
|
7
|
+
spec.version = ValidatesSpanishDocuments::VERSION
|
8
|
+
spec.authors = ['Rafael Jurado']
|
9
|
+
spec.email = ['rjurado@nosolosoftware.es']
|
10
|
+
spec.summary = 'Common validations.'
|
11
|
+
spec.description = 'Add common validations.'
|
12
|
+
spec.homepage = ''
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler'
|
21
|
+
spec.add_development_dependency 'rake'
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validates_spanish_documents
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rafael Jurado
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-12-20 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: '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'
|
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
|
+
description: Add common validations.
|
42
|
+
email:
|
43
|
+
- rjurado@nosolosoftware.es
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".gitlab-ci.yml"
|
50
|
+
- ".rubocop.yml"
|
51
|
+
- Gemfile
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- lib/validates_spanish_documents.rb
|
56
|
+
- lib/validates_spanish_documents/version.rb
|
57
|
+
- spec/spec_helper.rb
|
58
|
+
- spec/validations_spec.rb
|
59
|
+
- validates_spanish_documents.gemspec
|
60
|
+
homepage: ''
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.7.3
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Common validations.
|
84
|
+
test_files:
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
- spec/validations_spec.rb
|