- valid siren on create file
This commit is contained in:
parent
6e19d3ba67
commit
2b6aba16ac
5 changed files with 67 additions and 3 deletions
21
app/validators/siret_format_validator.rb
Normal file
21
app/validators/siret_format_validator.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
class SiretFormatValidator < ActiveModel::EachValidator
|
||||
def validate_each(record,attribute,value)
|
||||
unless value =~ /^\d{14}$/
|
||||
record.errors.add(attribute, :format)
|
||||
end
|
||||
unless value!= nil && (luhn_checksum(value) % 10 == 0)
|
||||
record.errors.add(attribute, :checksum)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def luhn_checksum(value)
|
||||
accum = 0
|
||||
value.reverse.each_char.map(&:to_i).each_with_index do |digit, index|
|
||||
t = index.even? ? digit : digit * 2
|
||||
t = t - 9 if t >= 10
|
||||
accum += t
|
||||
end
|
||||
accum
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue