uk_validators 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -5,6 +5,7 @@ This library contains Rails 3 custom validators for the following UK-centric dat
5
5
  * National Insurance Number
6
6
  * Post Code
7
7
  * Driving Licence Number
8
+ * Passport Number
8
9
 
9
10
 
10
11
  ## Installation
@@ -113,6 +114,36 @@ For the example User model above, the customised en.yml would be:
113
114
  driving_licence: "is not valid"
114
115
 
115
116
 
117
+ ## Passport Number
118
+ A UK passport number is simply made up of 9 numeric characters.
119
+
120
+ References: [NHS Data Dictionary](http://www.datadictionary.nhs.uk/version2/data_dictionary/data_field_notes/p/passport_number__o_new_c__de.asp?shownav=1), [highprogrammer.com](http://www.highprogrammer.com/alan/numbers/mrp.html)
121
+
122
+ ### Usage
123
+
124
+ ```ruby
125
+ class HolidayMaker < ActiveRecord::Base
126
+ validates :passport, passport_number: true
127
+ end
128
+ ```
129
+
130
+ ### I18n
131
+
132
+ The default error message is `is not a valid Passport No.`
133
+
134
+ This can be translated in the same was as any other Rails validation message using the key **:passport_number**.
135
+
136
+ For the example HolidayMaker model above, the customised en.yml would be:
137
+
138
+ en:
139
+ activerecord:
140
+ errors:
141
+ models:
142
+ holiday_maker:
143
+ attributes:
144
+ passport:
145
+ passport_number: "is not valid"
146
+
116
147
 
117
148
 
118
149
  ## Contributing
data/lib/uk_validators.rb CHANGED
@@ -2,3 +2,4 @@ require "uk_validators/version"
2
2
  require "uk_validators/nino_validator"
3
3
  require "uk_validators/postcode_validator"
4
4
  require "uk_validators/driving_licence_validator"
5
+ require "uk_validators/passport_number_validator"
@@ -0,0 +1,20 @@
1
+ class PassportNumberValidator < ActiveModel::EachValidator
2
+
3
+ def validate_each(record, attribute, value)
4
+ record.errors.add(attribute, invalid_message(record, attribute)) unless valid_passport_number?(value)
5
+ end
6
+
7
+ private
8
+
9
+ # UK format is simply 9 digits
10
+ def valid_passport_number?(value)
11
+ value =~ /^[0-9]{9}$/
12
+ end
13
+
14
+ def invalid_message(record, attribute)
15
+ I18n.t :passport_number,
16
+ scope: "#{record.class.i18n_scope}.errors.models.#{record.class.model_name.i18n_key}.attributes.#{attribute}",
17
+ default: "is not a valid Passport No."
18
+ end
19
+
20
+ end
@@ -1,3 +1,3 @@
1
1
  module UkValidators
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,60 @@
1
+ require_relative 'test_helper'
2
+
3
+ class TestPassportNumberValidator < MiniTest::Unit::TestCase
4
+ class HolidayMaker < TestModel
5
+ validates :passport, passport_number: true
6
+ end
7
+
8
+ def test_valid_passport_numbers
9
+ valid_passport_numbers.each { |passport| assert HolidayMaker.new(passport: passport).valid?, "Passport No. should be valid: #{passport}" }
10
+ end
11
+
12
+ def test_invalid_passport_numbers
13
+ invalid_passport_numbers.each { |passport| assert HolidayMaker.new(passport: passport).invalid?, "Passport No. should be invalid: #{passport}" }
14
+ end
15
+
16
+ private
17
+
18
+ def valid_passport_numbers
19
+ [
20
+ '012345678',
21
+ '987654321',
22
+ '000000000',
23
+ '111111111',
24
+ '222222222',
25
+ '333333333',
26
+ '444444444',
27
+ '555555555',
28
+ '666666666',
29
+ '777777777',
30
+ '888888888',
31
+ '999999999',
32
+ ]
33
+ end
34
+
35
+ def invalid_passport_numbers
36
+ [
37
+ '01234567',
38
+ '9876543',
39
+ '012345',
40
+ '98765',
41
+ '0123',
42
+ '987',
43
+ '01',
44
+ '9',
45
+ '',
46
+ ' ',
47
+ 'a12345678',
48
+ '0b2345678',
49
+ '01b345678',
50
+ '012c45678',
51
+ '0123d5678',
52
+ '01234e678',
53
+ '012345f78',
54
+ '0123456g8',
55
+ '01234567h',
56
+ 'ijklmnopq',
57
+ '1234&*()['
58
+ ]
59
+ end
60
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uk_validators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -90,10 +90,12 @@ files:
90
90
  - lib/uk_validators.rb
91
91
  - lib/uk_validators/driving_licence_validator.rb
92
92
  - lib/uk_validators/nino_validator.rb
93
+ - lib/uk_validators/passport_number_validator.rb
93
94
  - lib/uk_validators/postcode_validator.rb
94
95
  - lib/uk_validators/version.rb
95
96
  - test/driving_licence_test.rb
96
97
  - test/nino_test.rb
98
+ - test/passport_number_test.rb
97
99
  - test/postcode_test.rb
98
100
  - test/test_helper.rb
99
101
  - uk_validators.gemspec
@@ -112,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
112
114
  version: '0'
113
115
  segments:
114
116
  - 0
115
- hash: -224961422204427896
117
+ hash: -1062937670582392056
116
118
  required_rubygems_version: !ruby/object:Gem::Requirement
117
119
  none: false
118
120
  requirements:
@@ -121,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
123
  version: '0'
122
124
  segments:
123
125
  - 0
124
- hash: -224961422204427896
126
+ hash: -1062937670582392056
125
127
  requirements: []
126
128
  rubyforge_project:
127
129
  rubygems_version: 1.8.25
@@ -131,5 +133,6 @@ summary: Rails validations for UK data
131
133
  test_files:
132
134
  - test/driving_licence_test.rb
133
135
  - test/nino_test.rb
136
+ - test/passport_number_test.rb
134
137
  - test/postcode_test.rb
135
138
  - test/test_helper.rb