Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.*
!.gitignore
!.travis.yml
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
Expand Down
2 changes: 1 addition & 1 deletion lib/validates_russian/region_numbers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ValidatesRussian
REGION_NUMBERS = %w{01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 75 41 23 24 59 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 76 77 78 79 83 86 87 89 91 92}
REGION_NUMBERS = %w{01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 75 41 23 24 59 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 76 77 78 79 83 86 87 89 91 92 99}
end
61 changes: 46 additions & 15 deletions lib/validators/inn_format_validator.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,56 @@
class InnFormatValidator < ValidatesRussian::Validator
validates_using do |inn|
next false unless ValidatesRussian::REGION_NUMBERS.include?(inn[0..1])
next false unless inn =~ /^\d+$/
class InnFormatValidator < ActiveModel::EachValidator
REGION_NUMBERS = %w{01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 75 41 23 24 59 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 76 77 78 79 83 86 87 89 91 92 99}

inn = inn.split(//).map(&:to_i)
# Причина постановки на учет
P10 = [2, 4, 10, 3, 5, 9, 4, 6, 8]
P11 = [7, 2, 4, 10, 3, 5, 9, 4, 6, 8]
P12 = [3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8]

def validate_each(record, attribute, value)
res = true
value = value.to_s
unless value =~ /^\d+$/
error record, attribute, value, :numbers_only
res = false
end

if value.length != 10 and value.length != 12
error record, attribute, value, :invalid_length
res = false
end

unless REGION_NUMBERS.include?(value[0..1])
error record, attribute, value, :unknown_region
res = false
end

return res if options[:mild]

case inn.size
when 10 then next false unless calc(P10, inn) == inn[9]
when 12 then next false unless calc(P11, inn) == inn[10] && calc(P12, inn) == inn[11]
else next false
value = value.split(//).map(&:to_i)

case value.size
when 10 then
unless calc(P10, value) == value[9]
error record, attribute, value, :invalid_pp
res = false
end
when 12 then
unless calc(P11, value) == value[10] && calc(P12, value) == value[11]
error record, attribute, value, :invalid_pp
res = false
end
end

res
end

private

def self.calc(p, inn)
p.each_with_index.inject(0){ |s, p| s + p[0] * inn[p[1]] } % 11 % 10
def error(record, attribute, value, key)
record.errors.add(attribute, key, options.merge(value: value))
end

P10 = [2, 4, 10, 3, 5, 9, 4, 6, 8]
P11 = [7, 2, 4, 10, 3, 5, 9, 4, 6, 8]
P12 = [3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8]
private_constant :P10, :P11, :P12
def calc(p, value)
p.each_with_index.inject(0){ |s, p| s + p[0] * value[p[1]] } % 11 % 10
end
end
3 changes: 1 addition & 2 deletions lib/validators/kpp_format_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ class KppFormatValidator < ValidatesRussian::Validator
validates_using do |kpp|
next false unless kpp.size == 9
next false unless ValidatesRussian::REGION_NUMBERS.include?(kpp[0..1])
next false unless kpp =~ /^\d+$/
next false unless kpp[5..6] != '00'
next false unless kpp =~ /^\d{4}([a-z]|\d){2}\d{3}$/i
end
end
4 changes: 2 additions & 2 deletions lib/validators/rs_format_validator.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class RsFormatValidator < ValidatesRussian::Validator
# see format here: http://ru.wikipedia.org/wiki/Расчётный_счёт
validates_using do |rs|
next false unless rs.size == 20
next false unless rs.size == 20 || rs.size == 25
next false unless ValidatesRussian::OKV.include?(rs[5..7])
next false unless rs =~ /^\d+$/
end
end
end
18 changes: 10 additions & 8 deletions spec/validators/rs_format_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

it 'should be valid for valid values' do
valid_rss = %w{
58187404448375647329
77252937461205123501
52534060468985601001
58187404448375647329
77252937461205123501
52534060468985601001
3030181050000100000100000
}

valid_rss.each do |rs|
Expand All @@ -20,11 +21,12 @@

it 'should not be valid for invalid values' do
invalid_rss = %w{
77150100
gsDASsda
lol
52531406168985601001
7725234016120512350
77150100
gsDASsda
lol
52531406168985601001
7725234016120512350
303018105000010000010000
}.push('', ' ', nil, [], {})

invalid_rss.each do |rs|
Expand Down