taiwanese_id_validator 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: d7bc7d3ead210e4f0ada15a66981b4b70fe58139
4
- data.tar.gz: 0041fce3f39879b1cfd0bfe11fe3a3f6fdd18710
2
+ SHA256:
3
+ metadata.gz: 4369f6bf917fbd32056fcb9084fd6640a083f6cea7449d0212e3d87c5830f35e
4
+ data.tar.gz: 9bd602649636f06c50ca11a22930f7621a414285ab1a447f5927a6113946b9dd
5
5
  SHA512:
6
- metadata.gz: 15c3820088f346a230802ab6723407f3ee0fa177e4af31fea9af491ed8c5ce717cacb3cc09192903713c12227530df6a90f34089003bcf9f5129a32f6e00dde5
7
- data.tar.gz: 47115e8e91c29985274f7d8a475e8aa426c9e52c11661a94cdd1866c76dbd57278f2d78fa575a99a727763559f543eea5240ba6ecc18c48de70beb46f50a819b
6
+ metadata.gz: 8350403c7f2e5d6e80d62a193aa2d5fb46d896c07b0914309c6c718e8919123496a72550b8c321cd8596bf56cb93f09dfb197cb0738ed160c4f16cf9bcee5598
7
+ data.tar.gz: 11e07f4a93b054b8c47487a8d7e1891f880f1c35be44640e707f17f9abfb2af5c0d03b2477376b43a0556ad7d4329b49fbf8bc0cfc4ee840eddd927d56484c50
data/CHANGELOG.md ADDED
@@ -0,0 +1,14 @@
1
+ # Changelog
2
+
3
+ ## V0.0.3
4
+
5
+ - Fix `TwidValidator.valid?(nil)` raise Error problem [PR#3](https://github.com/wayne5540/taiwanese_id_validator/pull/3)
6
+
7
+ ## V0.0.2
8
+
9
+ - Support new International ID format [PR#1](https://github.com/wayne5540/taiwanese_id_validator/pull/1)
10
+ - Upgrade bundler dependency, now requires version >= 2.2.33
11
+
12
+ ## V0.0.1
13
+
14
+ First version
data/DEVELOPMENT.md ADDED
@@ -0,0 +1,37 @@
1
+ ## Get Started
2
+
3
+ 1. Fork it ( https://github.com/[my-github-username]/taiwanese_id_validator/fork )
4
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
5
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
6
+ 4. Push to the branch (`git push origin my-new-feature`)
7
+ 5. Create a new Pull Request
8
+
9
+ ## Run Tests
10
+
11
+ `rspec`
12
+
13
+ ## Build the gem
14
+
15
+ Build the gem:
16
+
17
+ ```
18
+ gem build taiwanese_id_validator.gemspec
19
+ ```
20
+
21
+ Install the gem, change the version name if needed
22
+
23
+ ```
24
+ gem install ./taiwanese_id_validator-0.0.3.gem
25
+ ```
26
+
27
+ Push to RubyGem
28
+
29
+ ```
30
+ gem push ./taiwanese_id_validator-0.0.3.gem
31
+ ```
32
+
33
+ More details about how to make the gem including build, install, push can be found [here](https://guides.rubygems.org/make-your-own-gem/)
34
+
35
+ ## TODOs
36
+
37
+ - Support Chinese ID Validation
data/README.md CHANGED
@@ -6,10 +6,10 @@
6
6
 
7
7
  ## Features
8
8
 
9
- * Rails model 內驗證 (Rails 3+/4)
10
- * Rails model 外驗證
11
- * 產生符合格式的身分證字號,可選擇男女
12
-
9
+ - Rails model 內驗證 (Rails 3+/4/5/6/7)
10
+ - Rails model 外驗證
11
+ - 新式統一證號驗證
12
+ - 產生符合格式的身分證字號,可選擇男女
13
13
 
14
14
  ## Installation
15
15
 
@@ -39,7 +39,7 @@ end
39
39
 
40
40
  #### Options
41
41
 
42
- * `allow_nil` or `allow_blank`
42
+ - `allow_nil` or `allow_blank`
43
43
 
44
44
  ```ruby
45
45
  class User < ActiveRecord::Base
@@ -47,7 +47,7 @@ class User < ActiveRecord::Base
47
47
  end
48
48
  ```
49
49
 
50
- * Case sensitive, default is true
50
+ - Case sensitive, default is true
51
51
 
52
52
  ```ruby
53
53
  class User < ActiveRecord::Base
@@ -55,7 +55,6 @@ class User < ActiveRecord::Base
55
55
  end
56
56
  ```
57
57
 
58
-
59
58
  ### Validation outside the model
60
59
 
61
60
  #### 驗證身分證字號
@@ -70,10 +69,16 @@ TwidValidator.valid?('A123456777') #=> false
70
69
 
71
70
  #### 產生假身分證字號
72
71
 
72
+ Generator 是沒有被自動 require 的,所以要使用前要先 require:
73
+
73
74
  ```ruby
75
+ require 'taiwanese_id_validator/twid_generator'
76
+
74
77
  TwidGenerator.generate #=> 隨機產生身分證字號
75
78
  ```
76
79
 
80
+ 可以指定要產生男性或是女性的身分證字號
81
+
77
82
  ```ruby
78
83
  TwidGenerator.generate("male") #=> 男性的身分證字號
79
84
  ```
@@ -82,20 +87,25 @@ TwidGenerator.generate("male") #=> 男性的身分證字號
82
87
  TwidGenerator.generate("female") #=> 女性的身分證字號
83
88
  ```
84
89
 
85
- ## Others
90
+ #### 驗證統一證號
91
+
92
+ ```ruby
93
+ ItidValidator.valid?('A800000014') #=> true
94
+ ```
86
95
 
87
- [公式](https://zh.wikipedia.org/wiki/%E4%B8%AD%E8%8F%AF%E6%B0%91%E5%9C%8B%E5%9C%8B%E6%B0%91%E8%BA%AB%E5%88%86%E8%AD%89)
96
+ ```ruby
97
+ ItidValidator.valid?('A800000015') #=> false
98
+ ```
88
99
 
100
+ ## Others
89
101
 
90
- ## Contributing
102
+ - [身分證公式](https://zh.wikipedia.org/wiki/%E4%B8%AD%E8%8F%AF%E6%B0%91%E5%9C%8B%E5%9C%8B%E6%B0%91%E8%BA%AB%E5%88%86%E8%AD%89)
103
+ - [新式統一證號](https://zh.wikipedia.org/wiki/%E4%B8%AD%E8%8F%AF%E6%B0%91%E5%9C%8B%E5%B1%85%E7%95%99%E8%AD%89)
91
104
 
92
- 1. Fork it ( https://github.com/[my-github-username]/taiwanese_id_validator/fork )
93
- 2. Create your feature branch (`git checkout -b my-new-feature`)
94
- 3. Commit your changes (`git commit -am 'Add some feature'`)
95
- 4. Push to the branch (`git push origin my-new-feature`)
96
- 5. Create a new Pull Request
105
+ ## Contributing
97
106
 
107
+ Check [DEVELOPMENT.md](DEVELOPMENT.md)
98
108
 
99
109
  ## LICENSE
100
110
 
101
- This project rocks and uses MIT-LICENSE.
111
+ This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,13 @@
1
+ module ItidMapping
2
+ ITID_LETTER = {
3
+ "A" => [1, 0], "B" => [1, 1], "C" => [1, 2], "D" => [1, 3],
4
+ "E" => [1, 4], "F" => [1, 5], "G" => [1, 6], "H" => [1, 7],
5
+ "I" => [3, 4], "J" => [1, 8], "K" => [1, 9], "L" => [2, 0],
6
+ "M" => [2, 1], "N" => [2, 2], "O" => [3, 5], "P" => [2, 3],
7
+ "Q" => [2, 4], "R" => [2, 5], "S" => [2, 6], "T" => [2, 7],
8
+ "U" => [2, 8], "V" => [2, 9], "W" => [3, 2], "X" => [3, 0],
9
+ "Y" => [3, 1], "Z" => [3, 3]
10
+ }
11
+
12
+ MULTIPLIER = [1,9,8,7,6,5,4,3,2,1].freeze
13
+ end
@@ -0,0 +1,32 @@
1
+ require 'international_id_validator/itid_mapping'
2
+
3
+ module ItidValidator
4
+ def self.valid?(itid, case_sensitive = true, allow_old = false)
5
+ return false if !itid
6
+ return false if !itid.is_a?(String)
7
+
8
+ itid = itid.upcase unless case_sensitive
9
+
10
+ return false if itid.length != 10
11
+ return true if allow_old && itid.match?(/\A[A-Z][ABCD]\d{8}\z/)
12
+ return false unless itid.match?(/[A-Z](8|9)\d{8}\z/)
13
+
14
+ characters = itid.chars
15
+ digits = ItidMapping::ITID_LETTER[characters.shift] + characters.map(&:to_i)
16
+
17
+ checked_num = calculate_checked_num(digits)
18
+
19
+ return checked_num == digits.last
20
+ end
21
+
22
+ private
23
+
24
+ def self.calculate_checked_num(digits)
25
+ sum = 0
26
+ 10.times do |i|
27
+ m = digits[i] * ItidMapping::MULTIPLIER[i]
28
+ sum += (m % 10)
29
+ end
30
+ 10 - (sum % 10)
31
+ end
32
+ end
@@ -0,0 +1,25 @@
1
+ require "international_id_validator/itid_validator"
2
+
3
+ class InternationalIdValidator < ActiveModel::EachValidator
4
+ def validate_each(record, attribute, value)
5
+ return if skip_check?
6
+
7
+ unless ItidValidator.valid?(value, case_sensitive?, allow_old?)
8
+ record.errors.add(attribute, (options[:message] || "is not a valid ID"))
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def skip_check?
15
+ options[:allow_nil].present? || options[:allow_blank].present?
16
+ end
17
+
18
+ def case_sensitive?
19
+ options[:case_sensitive].nil? ? true : options[:case_sensitive]
20
+ end
21
+
22
+ def allow_old?
23
+ options[:allow_old].nil? ? false : options[:allow_old]
24
+ end
25
+ end
@@ -2,6 +2,9 @@ require 'taiwanese_id_validator/twid_mapping'
2
2
 
3
3
  module TwidValidator
4
4
  def self.valid?(twid, case_sensitive = true)
5
+ return false if !twid
6
+ return false if !twid.is_a?(String)
7
+
5
8
  twid = twid.upcase unless case_sensitive
6
9
 
7
10
  return false if twid.length != 10
@@ -5,7 +5,7 @@ class TaiwaneseIdValidator < ActiveModel::EachValidator
5
5
  return if skip_check?
6
6
 
7
7
  unless TwidValidator.valid?(value, case_sensitive?)
8
- record.errors[attribute] << (options[:message] || "is not an valid ID")
8
+ record.errors.add(attribute, (options[:message] || "is not a valid ID"))
9
9
  end
10
10
  end
11
11
 
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe ItidValidator do
4
+
5
+ describe "#valid?" do
6
+ context "when id not valid" do
7
+ it "returns false if id too short" do
8
+ expect( ItidValidator.valid?("A8000") ).to be false
9
+ end
10
+
11
+ it "returns false if id too long" do
12
+ expect( ItidValidator.valid?("A800000014111") ).to be false
13
+ end
14
+
15
+ it "returns false if id calculation is wrong" do
16
+ expect( ItidValidator.valid?("A800000013") ).to be false
17
+ end
18
+
19
+ it "returns false if format is wrong" do
20
+ expect( ItidValidator.valid?("YAC4713412") ).to be false
21
+ end
22
+
23
+ it "returns false if id is nil" do
24
+ expect( ItidValidator.valid?(nil) ).to be false
25
+ end
26
+
27
+ it "returns false if id is nil" do
28
+ expect( ItidValidator.valid?(1) ).to be false
29
+ end
30
+ end
31
+
32
+ context "when id is valid" do
33
+ let(:id) { "A800000014" }
34
+
35
+ it "returns true" do
36
+ expect( ItidValidator.valid?(id) ).to be true
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ class ItTestUser < TestModel
4
+ validates :itid, international_id: true
5
+ end
6
+
7
+ class ItTestUserAllowsNil < TestModel
8
+ validates :itid, international_id: { allow_nil: true }
9
+ end
10
+
11
+ class ItTestUserNoCaseSensitive < TestModel
12
+ validates :itid, international_id: { case_sensitive: false }
13
+ end
14
+
15
+ class ItTestUserAllowOld < TestModel
16
+ validates :itid, international_id: { allow_old: true }
17
+ end
18
+
19
+ describe InternationalIdValidator do
20
+ describe "validation" do
21
+ context "given the valid new IDs" do
22
+ [
23
+ "A800000014"
24
+ ].each do |itid|
25
+ it "#{itid} should be valid" do
26
+ expect(ItTestUser.new(itid: itid)).to be_valid
27
+ end
28
+
29
+ it "#{itid} should be valid" do
30
+ expect(ItTestUserAllowsNil.new(itid: nil)).to be_valid
31
+ end
32
+
33
+ it "#{itid} should be valid" do
34
+ expect(ItTestUserNoCaseSensitive.new(itid: itid.downcase)).to be_valid
35
+ end
36
+
37
+ it "#{itid} should be valid" do
38
+ expect(ItTestUserNoCaseSensitive.new(itid: itid.downcase)).to be_valid
39
+ end
40
+ end
41
+ end
42
+
43
+ context "given the valid old IDs" do
44
+ [
45
+ "AD00000016"
46
+ ].each do |itid|
47
+ it "#{itid} should not be valid" do
48
+ expect(ItTestUser.new(itid: itid)).not_to be_valid
49
+ end
50
+
51
+ it "#{itid} should be valid" do
52
+ expect(ItTestUserAllowOld.new(itid: itid)).to be_valid
53
+ end
54
+ end
55
+ end
56
+
57
+ context "given the invalid IDs" do
58
+ [
59
+ "A8000014",
60
+ "A80000001411",
61
+ "Aa00000014",
62
+ "A800000015",
63
+ "a800000014"
64
+ ].each do |itid|
65
+ it "#{itid} should not be valid" do
66
+ expect(ItTestUser.new(itid: itid)).not_to be_valid
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
data/spec/spec_helper.rb CHANGED
@@ -6,6 +6,7 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
6
  $LOAD_PATH.unshift(File.dirname(__FILE__))
7
7
 
8
8
  require 'taiwanese_id_validator'
9
+ require 'international_id_validator'
9
10
 
10
11
  class TestModel
11
12
  include ActiveModel::Validations
@@ -17,4 +18,4 @@ class TestModel
17
18
  def read_attribute_for_validation(key)
18
19
  @attributes[key]
19
20
  end
20
- end
21
+ end
@@ -19,6 +19,14 @@ describe TwidValidator do
19
19
  it "returns false if format is wrong" do
20
20
  expect( TwidValidator.valid?("YAC4713412") ).to be false
21
21
  end
22
+
23
+ it "returns false if id is nil" do
24
+ expect( TwidValidator.valid?(nil) ).to be false
25
+ end
26
+
27
+ it "returns false if id is nil" do
28
+ expect( TwidValidator.valid?(1) ).to be false
29
+ end
22
30
  end
23
31
 
24
32
  context "when id is valid" do
@@ -28,8 +36,5 @@ describe TwidValidator do
28
36
  expect( TwidValidator.valid?(id) ).to be true
29
37
  end
30
38
  end
31
-
32
- pending "case sensative"
33
39
  end
34
-
35
- end
40
+ end
@@ -1,14 +1,14 @@
1
1
  require 'spec_helper'
2
2
 
3
- class TestUser < TestModel
3
+ class TwTestUser < TestModel
4
4
  validates :twid, taiwanese_id: true
5
5
  end
6
6
 
7
- class TestUserAllowsNil < TestModel
7
+ class TwTestUserAllowsNil < TestModel
8
8
  validates :twid, taiwanese_id: { allow_nil: true }
9
9
  end
10
10
 
11
- class TestUserNoCaseSensitive < TestModel
11
+ class TwTestUserNoCaseSensitive < TestModel
12
12
  validates :twid, taiwanese_id: { case_sensitive: false }
13
13
  end
14
14
 
@@ -19,15 +19,15 @@ describe TaiwaneseIdValidator do
19
19
  "Y144713411"
20
20
  ].each do |twid|
21
21
  it "#{twid} should be valid" do
22
- expect(TestUser.new(twid: twid)).to be_valid
22
+ expect(TwTestUser.new(twid: twid)).to be_valid
23
23
  end
24
24
 
25
25
  it "#{twid} should be valid" do
26
- expect(TestUserAllowsNil.new(twid: nil)).to be_valid
26
+ expect(TwTestUserAllowsNil.new(twid: nil)).to be_valid
27
27
  end
28
28
 
29
29
  it "#{twid} should be valid" do
30
- expect(TestUserNoCaseSensitive.new(twid: twid.downcase)).to be_valid
30
+ expect(TwTestUserNoCaseSensitive.new(twid: twid.downcase)).to be_valid
31
31
  end
32
32
  end
33
33
  end
@@ -40,10 +40,10 @@ describe TaiwaneseIdValidator do
40
40
  "Y144713410",
41
41
  "y144713411"
42
42
  ].each do |twid|
43
- it "#{twid} should be valid" do
44
- expect(TestUser.new(twid: twid)).not_to be_valid
43
+ it "#{twid} should not be valid" do
44
+ expect(TwTestUser.new(twid: twid)).not_to be_valid
45
45
  end
46
46
  end
47
47
  end
48
48
  end
49
- end
49
+ end
@@ -1,11 +1,11 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "taiwanese_id_validator"
3
- spec.version = "0.0.1"
3
+ spec.version = "0.0.3"
4
4
  spec.authors = ["Wayne Chu"]
5
5
  spec.email = ["wayne.5540@gmail.com"]
6
6
  spec.summary = "Taiwanese Id Validator."
7
7
  spec.description = "Taiwanese Id Validator"
8
- spec.homepage = ""
8
+ spec.homepage = "https://github.com/wayne5540/taiwanese_id_validator"
9
9
  spec.license = "MIT"
10
10
  spec.files = `git ls-files`.split("\n")
11
11
  spec.test_files = `git ls-files -- {spec}/*`.split("\n")
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
14
14
 
15
15
  spec.add_dependency "activemodel"
16
16
 
17
- spec.add_development_dependency "bundler", "~> 1.6"
17
+ spec.add_development_dependency "bundler", ">= 2.2.33"
18
18
  spec.add_development_dependency "rake"
19
19
  spec.add_development_dependency "rspec"
20
- end
20
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: taiwanese_id_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wayne Chu
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-17 00:00:00.000000000 Z
11
+ date: 2024-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '1.6'
33
+ version: 2.2.33
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '1.6'
40
+ version: 2.2.33
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -75,24 +75,31 @@ extra_rdoc_files: []
75
75
  files:
76
76
  - ".gitignore"
77
77
  - ".travis.yml"
78
+ - CHANGELOG.md
79
+ - DEVELOPMENT.md
78
80
  - Gemfile
79
81
  - LICENSE.txt
80
82
  - README.md
81
83
  - Rakefile
84
+ - lib/international_id_validator.rb
85
+ - lib/international_id_validator/itid_mapping.rb
86
+ - lib/international_id_validator/itid_validator.rb
82
87
  - lib/taiwanese_id_validator.rb
83
88
  - lib/taiwanese_id_validator/twid_generator.rb
84
89
  - lib/taiwanese_id_validator/twid_mapping.rb
85
90
  - lib/taiwanese_id_validator/twid_validator.rb
91
+ - spec/international_id_validator/itid_validator_spec.rb
92
+ - spec/international_id_validator_spec.rb
86
93
  - spec/spec_helper.rb
87
94
  - spec/taiwanese_id_validator/twid_generator_spec.rb
88
95
  - spec/taiwanese_id_validator/twid_validator_spec.rb
89
96
  - spec/taiwanese_id_validator_spec.rb
90
97
  - taiwanese_id_validator.gemspec
91
- homepage: ''
98
+ homepage: https://github.com/wayne5540/taiwanese_id_validator
92
99
  licenses:
93
100
  - MIT
94
101
  metadata: {}
95
- post_install_message:
102
+ post_install_message:
96
103
  rdoc_options: []
97
104
  require_paths:
98
105
  - lib
@@ -107,9 +114,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
114
  - !ruby/object:Gem::Version
108
115
  version: '0'
109
116
  requirements: []
110
- rubyforge_project:
111
- rubygems_version: 2.2.2
112
- signing_key:
117
+ rubygems_version: 3.4.10
118
+ signing_key:
113
119
  specification_version: 4
114
120
  summary: Taiwanese Id Validator.
115
121
  test_files: []