diff --git a/lib/spanish_vat_validators.rb b/lib/spanish_vat_validators.rb index c5c9bcd..d311478 100644 --- a/lib/spanish_vat_validators.rb +++ b/lib/spanish_vat_validators.rb @@ -1,3 +1,4 @@ +#encoding: utf-8 require "spanish_vat_validators/version" module ActiveModel::Validations @@ -8,16 +9,20 @@ def message(kind='vat') end # Validates NIF - def validate_nif(value) + 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) letters = "TRWAGMYFPDXBNJZSQVHLCKE" - check = value.slice!(value.length - 1..value.length - 1).upcase + check = value.slice!(value.length - 1..value.length - 1) calculated_letter = letters[value.to_i % 23].chr return check === calculated_letter end # Validates CIF - def validate_cif(value) + 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) pares = 0 impares = 0 @@ -49,7 +54,9 @@ def validate_cif(value) # Validates NIE # a NIE is really a NIF with first number changed to capital X, Y or Z letter - def validate_nie(value) + 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) value[0] = {"X" => "0", "Y" => "1", "Z" => "2"}[value[0]] validate_nif(value) @@ -60,7 +67,7 @@ def validate_nie(value) class ValidSpanishVatValidator < ActiveModel::EachValidator include SpanishVatValidatorsHelpers def validate_each(record, attribute, value) - record.errors[attribute] = message unless validate_nif(value.clone) or validate_cif(value.clone) or validate_nie(value.clone) + record.errors[attribute] = message unless validate_nif(value) or validate_cif(value) or validate_nie(value) end end @@ -68,7 +75,7 @@ def validate_each(record, attribute, value) class ValidSpanishIdValidator < ActiveModel::EachValidator include SpanishVatValidatorsHelpers def validate_each(record, attribute, value) - record.errors[attribute] = message unless validate_nif(value.clone) or validate_nie(value.clone) + record.errors[attribute] = message unless validate_nif(value) or validate_nie(value) end end @@ -76,7 +83,7 @@ def validate_each(record, attribute, value) class ValidNifValidator < ActiveModel::EachValidator include SpanishVatValidatorsHelpers def validate_each(record, attribute,value) - record.errors[attribute] = message('nif') unless validate_nif(value.clone) + record.errors[attribute] = message('nif') unless validate_nif(value) end end @@ -84,7 +91,7 @@ def validate_each(record, attribute,value) class ValidCifValidator < ActiveModel::EachValidator include SpanishVatValidatorsHelpers def validate_each(record, attribute,value) - record.errors[attribute] = message('cif') unless validate_cif(value.clone) + record.errors[attribute] = message('cif') unless validate_cif(value) end end @@ -92,7 +99,7 @@ def validate_each(record, attribute,value) class ValidNieValidator < ActiveModel::EachValidator include SpanishVatValidatorsHelpers def validate_each(record, attribute,value) - record.errors[attribute] = message('nie') unless validate_nie(value.clone) + record.errors[attribute] = message('nie') unless validate_nie(value) end end