demarches-normaliennes/app/validators/siret_format_validator.rb

34 lines
845 B
Ruby
Raw Normal View History

2015-12-03 15:02:22 +01:00
class SiretFormatValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if !format_is_valid(value)
record.errors.add(attribute, :length)
elsif !luhn_passed(value)
2015-12-03 15:02:22 +01:00
record.errors.add(attribute, :checksum)
end
end
private
LA_POSTE_SIREN = '356000000'
def format_is_valid(value)
2022-07-25 18:17:30 +02:00
value&.match?(/^\d{14}$/)
end
def luhn_passed(value)
# Do not enforce Luhn for La Poste SIRET numbers, the only exception to this rule
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
2015-12-03 15:02:22 +01:00
def luhn_checksum(value)
value.reverse.each_char.map(&:to_i).map.with_index do |digit, index|
2015-12-03 15:02:22 +01:00
t = index.even? ? digit : digit * 2
t < 10 ? t : t - 9
end.sum
2015-12-03 15:02:22 +01:00
end
end