Created
October 9, 2017 04:44
-
-
Save pashagray/fc50822a43b70d13620413c41fb0e3a0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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