Class: Unidom::Certificate::China::UnifiedSocialCreditIdentifierValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/unidom/certificate/china/unified_social_credit_identifier_validator.rb

Overview

GB 32100-2015《法人和其他组织统一社会信用代码编码规则》 qyj.saic.gov.cn/wjfb/201509/t20150929_162430.html

Constant Summary collapse

WEIGHTS =
[ 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28 ].freeze
VALUES =
'0123456789ABCDEFGHJKLMNPQRTUWXY'.freeze

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/validators/unidom/certificate/china/unified_social_credit_identifier_validator.rb', line 9

def validate_each(record, attribute, value)

  value = value.to_s
  sum   = 0
  value[0..16].chars.each_with_index do |char, index| sum += VALUES.index(char)*WEIGHTS[index] end
  checksum = sum % 31
  checksum = 0==checksum ? 31 : checksum
  checksum = 31-checksum

  record.errors[attribute] << (options[:message]||'is invalid') unless VALUES[checksum]==value[17]

end