Skip to content

Commit

Permalink
Use correct REGEX start/end string delimitators
Browse files Browse the repository at this point in the history
  • Loading branch information
AbelToy committed Jun 17, 2019
1 parent 7e8b14d commit 8ed6ebb
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/spanish_vat_validators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def message(kind='spanish_vat')
def validate_nif(v)
return false if v.nil? || v.empty?
value = v.upcase
return false unless value.match(/^[0-9]{8}[a-z]$/i)
return false unless value.match(/\A[0-9]{8}[a-z]\z/i)
letters = "TRWAGMYFPDXBNJZSQVHLCKE"
check = value.slice!(value.length - 1)
calculated_letter = letters[value.to_i % 23].chr
Expand All @@ -24,12 +24,12 @@ def validate_nif(v)
def validate_cif(v)
return false if v.nil? || v.empty?
value = v.clone
return false unless value.match(/^[a-wyz][0-9]{7}[0-9a-z]$/i)
return false unless value.match(/\A[a-wyz][0-9]{7}[0-9a-z]\z/i)
even = 0
odd = 0
uletter = ["J", "A", "B", "C", "D", "E", "F", "G", "H", "I"]
text = value.upcase
regular = /^[ABCDEFGHJKLMNPQRSVW]\d{7}[0-9,A-J]$/#g);
regular = /\A[ABCDEFGHJKLMNPQRSVW]\d{7}[0-9,A-J]\z/i
if regular.match(value).blank?
false
else
Expand All @@ -54,7 +54,7 @@ def validate_cif(v)
def validate_nie(v)
return false if v.nil? || v.empty?
value = v.upcase
return false unless value.match(/^[xyz][0-9]{7}[a-z]$/i)
return false unless value.match(/\A[xyz][0-9]{7}[a-z]\z/i)
value[0] = {"X" => "0", "Y" => "1", "Z" => "2"}[value[0]]
validate_nif(value)
end
Expand Down
102 changes: 102 additions & 0 deletions spec/spanish_vat_validators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@
should_be_invalid(record, 'invalid')
end
end

it 'adds errors when there are surrounding characters' do
%w[22472947S S6185663I Y8527549Z].each do |identification_number|
surrounded = %W[junk#{identification_number} #{identification_number}junk junk#{identification_number}junk]
surrounded.each do |surrounded_identification_number|
record = build_record(surrounded_identification_number)
should_be_invalid(record)
end
end
end

it 'adds errors when there are surrounding lines' do
%w[22472947S S6185663I Y8527549Z].each do |identification_number|
surrounded = %W[\n#{identification_number} #{identification_number}\n \n#{identification_number}\n]
surrounded.each do |surrounded_identification_number|
record = build_record(surrounded_identification_number)
should_be_invalid(record)
end
end
end
end
end

Expand All @@ -48,6 +68,28 @@
should_be_invalid(record, 'invalid')
end
end

it 'adds errors when there are surrounding characters' do
%w[22472947S 96380632Y 28459349T 35696134L 86159868M 29052409M
Y8527549Z Y8305424T X9393496C X0012309G Y9370869Q Y2995306F].each do |identification_number|
surrounded = %W[junk#{identification_number} #{identification_number}junk junk#{identification_number}junk]
surrounded.each do |surrounded_identification_number|
record = build_record(surrounded_identification_number)
should_be_invalid(record)
end
end
end

it 'adds errors when there are surrounding lines' do
%w[22472947S 96380632Y 28459349T 35696134L 86159868M 29052409M
Y8527549Z Y8305424T X9393496C X0012309G Y9370869Q Y2995306F].each do |identification_number|
surrounded = %W[\n#{identification_number} #{identification_number}\n \n#{identification_number}\n]
surrounded.each do |surrounded_identification_number|
record = build_record(surrounded_identification_number)
should_be_invalid(record)
end
end
end
end
end

Expand All @@ -73,6 +115,26 @@
should_be_invalid(record, 'invalid')
end
end

it 'adds errors when there are surrounding characters' do
%w[22472947S 96380632Y 28459349T 35696134L 86159868M 29052409M].each do |identification_number|
surrounded = %W[junk#{identification_number} #{identification_number}junk junk#{identification_number}junk]
surrounded.each do |surrounded_identification_number|
record = build_record(surrounded_identification_number)
should_be_invalid(record)
end
end
end

it 'adds errors when there are surrounding lines' do
%w[22472947S 96380632Y 28459349T 35696134L 86159868M 29052409M].each do |identification_number|
surrounded = %W[\n#{identification_number} #{identification_number}\n \n#{identification_number}\n]
surrounded.each do |surrounded_identification_number|
record = build_record(surrounded_identification_number)
should_be_invalid(record)
end
end
end
end
end

Expand All @@ -98,6 +160,26 @@
should_be_invalid(record, 'invalid')
end
end

it 'adds errors when there are surrounding characters' do
%w[S6185663I C2871341J G37880135 F43766880 A58818501 J27950005 V63423321].each do |identification_number|
surrounded = %W[junk#{identification_number} #{identification_number}junk junk#{identification_number}junk]
surrounded.each do |surrounded_identification_number|
record = build_record(surrounded_identification_number)
should_be_invalid(record)
end
end
end

it 'adds errors when there are surrounding lines' do
%w[S6185663I C2871341J G37880135 F43766880 A58818501 J27950005 V63423321].each do |identification_number|
surrounded = %W[\n#{identification_number} #{identification_number}\n \n#{identification_number}\n]
surrounded.each do |surrounded_identification_number|
record = build_record(surrounded_identification_number)
should_be_invalid(record)
end
end
end
end
end

Expand All @@ -123,6 +205,26 @@
should_be_invalid(record, 'invalid')
end
end

it 'adds errors when there are surrounding characters' do
%w[Y8527549Z Y8305424T X9393496C X0012309G Y9370869Q Y2995306F].each do |identification_number|
surrounded = %W[junk#{identification_number} #{identification_number}junk junk#{identification_number}junk]
surrounded.each do |surrounded_identification_number|
record = build_record(surrounded_identification_number)
should_be_invalid(record)
end
end
end

it 'adds errors when there are surrounding lines' do
%w[Y8527549Z Y8305424T X9393496C X0012309G Y9370869Q Y2995306F].each do |identification_number|
surrounded = %W[\n#{identification_number} #{identification_number}\n \n#{identification_number}\n]
surrounded.each do |surrounded_identification_number|
record = build_record(surrounded_identification_number)
should_be_invalid(record)
end
end
end
end
end

Expand Down

0 comments on commit 8ed6ebb

Please sign in to comment.