Skip to content

Instantly share code, notes, and snippets.

@pashagray
Created October 9, 2017 04:44
Show Gist options
  • Save pashagray/fc50822a43b70d13620413c41fb0e3a0 to your computer and use it in GitHub Desktop.
Save pashagray/fc50822a43b70d13620413c41fb0e3a0 to your computer and use it in GitHub Desktop.
#
# SPECS
#
it 'validates presence of iin' do
form.validate(iin: '')
expect(form.errors[:iin]).to include('must be filled')
end
it 'validates length of iin to be 12' do
form.validate(iin: '123')
expect(form.errors[:iin]).to include('length must be 12')
end
it 'validates checksum of iin' do
form.validate(iin: '123456123456')
expect(form.errors[:iin]).to include('IIN checksum is not correct')
end
it 'bypasses validation with correct iin' do
form.validate(iin: '880909300211')
expect(form.errors[:iin]).not_to include('IIN checksum is not correct')
end
#
# user_form.rb
#
class UserForm < Reform::Form
property :first_name
property :last_name
property :iin
validation do
configure do
def correct_iin?(value)
value
.split('')
.first(11)
.map
.with_index { |n, idx| n.to_i * (idx + 1) }
.reduce(:+) % 11 == value[11].to_i
end
end
required(:first_name).filled
required(:last_name).filled
required(:iin) { filled? & size?(12) & correct_iin? }
end
end
#
# ru.yml
#
ru:
errors:
correct_iin?: "Указанный ИНН не верный. Контрольное число не сошлось."
#
# en.yml
#
en:
errors:
correct_iin?: 'IIN checksum is not correct'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment