From 36950b985bcff485211865c70dcf3c9950bc9e55 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Tue, 18 Sep 2018 22:19:07 +0200 Subject: [PATCH] Refactor SiretFormatValidator#validate_each --- app/validators/siret_format_validator.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/validators/siret_format_validator.rb b/app/validators/siret_format_validator.rb index 89d41f2d2..268b3a1d4 100644 --- a/app/validators/siret_format_validator.rb +++ b/app/validators/siret_format_validator.rb @@ -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