[Fix #2618] Do not enforce Luhn for La Poste SIRET numbers

This commit is contained in:
gregoirenovel 2018-09-19 10:29:43 +02:00
parent 940dfe422b
commit 4e4b7c617f
2 changed files with 14 additions and 1 deletions

View file

@ -11,12 +11,19 @@ class SiretFormatValidator < ActiveModel::EachValidator
private
LA_POSTE_SIREN = '356000000'
def format_is_valid(value)
value&.match?(/^\d{14}$/)
end
def luhn_passed(value)
value.present? && (luhn_checksum(value) % 10 == 0)
# Do not enforce Luhn for La Poste SIRET numbers, the only exception to this rule
value.present? && (siret_is_attached_to_la_poste(value) || (luhn_checksum(value) % 10 == 0))
end
def siret_is_attached_to_la_poste(value)
value[0..8] == LA_POSTE_SIREN
end
def luhn_checksum(value)

View file

@ -33,6 +33,12 @@ describe Siret, type: :model do
it { is_expected.to be_invalid }
end
context 'with a lunh-invalid La Poste siret' do
let(:siret) { '35600000018723' }
it { is_expected.to be_valid }
end
context 'with a valid siret' do
let(:siret) { '41816609600051' }