Refactor SiretFormatValidator#validate_each

This commit is contained in:
gregoirenovel 2018-09-18 22:19:07 +02:00
parent 49d872452a
commit 36950b985b

View file

@ -1,10 +1,10 @@
class SiretFormatValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if !(value =~ /^\d{14}$/)
if !value&.match?(/^\d{14}$/)
record.errors.add(attribute, :format)
end
if value.nil? || (luhn_checksum(value) % 10 != 0)
if value.present? && (luhn_checksum(value) % 10 != 0)
record.errors.add(attribute, :checksum)
end
end