uk_validators 0.0.1 → 0.0.2
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 +65 -6
- data/lib/uk_validators/postcode_validator.rb +19 -0
- data/lib/uk_validators/version.rb +1 -1
- data/lib/uk_validators.rb +1 -3
- data/test/nino_test.rb +3 -3
- data/test/postcode_test.rb +118 -0
- metadata +7 -4
data/README.md
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
# UK Validators
|
2
2
|
|
3
|
-
This library contains validators for the following UK-centric data:
|
3
|
+
This library contains Rails 3 custom validators for the following UK-centric data:
|
4
4
|
|
5
5
|
* National Insurance Number
|
6
|
+
* Post Code
|
6
7
|
|
7
8
|
|
8
9
|
## Installation
|
@@ -13,15 +14,73 @@ Add this line to your application's Gemfile:
|
|
13
14
|
|
14
15
|
And then execute:
|
15
16
|
|
16
|
-
$ bundle
|
17
|
+
$ bundle install
|
17
18
|
|
18
|
-
## Usage
|
19
19
|
|
20
|
-
|
20
|
+
## National Insurance Number (Nino)
|
21
|
+
Validates the format of a UK National Insurance Number.
|
22
|
+
|
23
|
+
References: [HMRC](http://www.hmrc.gov.uk/manuals/nimmanual/nim39110.htm), [uk-osint.net](http://www.uk-osint.net/usefulbits.html#Understanding%20UK%20National%20Insurance%20Numbers), [Wikipedia](http://en.wikipedia.org/wiki/National_Insurance_number)
|
24
|
+
|
25
|
+
### Usage
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
class User < ActiveRecord::Base
|
29
|
+
validates :ni_number, nino: true
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
### I18n
|
34
|
+
|
35
|
+
The default error message is `is not a valid National Insurance No.`
|
36
|
+
|
37
|
+
This can be translated in the same was as any other Rails validation message using the key **:nino**.
|
38
|
+
|
39
|
+
For the example User model above, the customised en.yml would be:
|
40
|
+
|
41
|
+
en:
|
42
|
+
activerecord:
|
43
|
+
errors:
|
44
|
+
models:
|
45
|
+
user:
|
46
|
+
attributes:
|
47
|
+
ni_number:
|
48
|
+
nino: "is not valid"
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
## Post Code
|
53
|
+
|
54
|
+
Validates the format of a UK postal code. This does not check whether the post code actually exists as this would require a lookup against approx. 1.8 million postal codes.
|
55
|
+
|
56
|
+
References: [Royal Mail PAF Document](http://www.poweredbypaf.com/wp-content/themes/amu/paf_downloads/programmers_guide.pdf), [Wikipedia](http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom)
|
57
|
+
|
58
|
+
### Usage
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
class Address < ActiveRecord::Base
|
62
|
+
validates :postal_code, postcode: true
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
### I18n
|
67
|
+
|
68
|
+
The default error message is `is not a valid postcode`
|
69
|
+
|
70
|
+
This can be translated in the same was as any other Rails validation message using the key **:postcode**.
|
71
|
+
|
72
|
+
For the example Address model above, the customised en.yml would be:
|
73
|
+
|
74
|
+
en:
|
75
|
+
activerecord:
|
76
|
+
errors:
|
77
|
+
models:
|
78
|
+
address:
|
79
|
+
attributes:
|
80
|
+
postal_code:
|
81
|
+
postcode: "is not valid"
|
21
82
|
|
22
|
-
Simply add the following to your model
|
23
83
|
|
24
|
-
validates :my_nino_attribute, nino: true
|
25
84
|
|
26
85
|
|
27
86
|
## Contributing
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class PostcodeValidator < ActiveModel::EachValidator
|
2
|
+
|
3
|
+
def validate_each(record, attribute, value)
|
4
|
+
record.errors.add(attribute, invalid_message(record, attribute)) unless valid_postcode?(value)
|
5
|
+
end
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
def valid_postcode?(value)
|
10
|
+
value =~ /^([A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW])\s?[0-9][ABD-HJLNP-UW-Z]{2}|(GIR\s?0AA)|(AI-2640)|(SAN\s?TA1)|(BFPO\s?(C\/O\s)?[0-9]{1,4})|((ASCN|BBND|[BFS]IQQ|PCRN|STHL|TDCU|TKCA)\s?1ZZ))$/i
|
11
|
+
end
|
12
|
+
|
13
|
+
def invalid_message(record, attribute)
|
14
|
+
I18n.t :postcode,
|
15
|
+
scope: "#{record.class.i18n_scope}.errors.models.#{record.class.model_name.i18n_key}.attributes.#{attribute}",
|
16
|
+
default: "is not a valid postcode."
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
data/lib/uk_validators.rb
CHANGED
data/test/nino_test.rb
CHANGED
@@ -6,11 +6,11 @@ class TestNinoValidator < MiniTest::Unit::TestCase
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def test_valid_ninos
|
9
|
-
valid_ninos.each { |nino| assert User.new(ni_number: nino).valid?, "
|
9
|
+
valid_ninos.each { |nino| assert User.new(ni_number: nino).valid?, "Nino should be valid: #{nino}" }
|
10
10
|
end
|
11
11
|
|
12
12
|
def test_invalid_ninos
|
13
|
-
invalid_ninos.each { |nino| assert User.new(ni_number: nino).invalid
|
13
|
+
invalid_ninos.each { |nino| assert User.new(ni_number: nino).invalid?, "Nino should be invalid: #{nino}" }
|
14
14
|
end
|
15
15
|
|
16
16
|
private
|
@@ -21,7 +21,7 @@ private
|
|
21
21
|
'bb940321b',
|
22
22
|
'cc987654c',
|
23
23
|
'ee903221d',
|
24
|
-
'gg849302 ', #
|
24
|
+
'gg849302 ', # space is valid for the final character if it is not known
|
25
25
|
'hh123849a',
|
26
26
|
'jj123849b',
|
27
27
|
'kk123849c',
|
@@ -0,0 +1,118 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
|
3
|
+
class TestPostcodeValidator < MiniTest::Unit::TestCase
|
4
|
+
class User < TestModel
|
5
|
+
validates :postcode, postcode: true
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_valid_postcodes
|
9
|
+
valid_postcodes.each { |postcode| assert User.new(postcode: postcode).valid?, "Postcode should be valid: #{postcode}" }
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_invalid_postcodes
|
13
|
+
invalid_postcodes.each { |postcode| assert User.new(postcode: postcode).invalid?, "Postcode should be invalid: #{postcode}" }
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def valid_postcodes
|
19
|
+
[
|
20
|
+
'EC2N 2DB', # AA9A 9AA format
|
21
|
+
'EC1A1BB',
|
22
|
+
'W1A 1HQ', # A9A 9AA format
|
23
|
+
'N1C1AA',
|
24
|
+
'S2 4SU', # A9 9AA format
|
25
|
+
'M11AA',
|
26
|
+
'B33 8TH', # A99 9AA format
|
27
|
+
'G109WW',
|
28
|
+
'CR2 6XH', # AA9 9AA format
|
29
|
+
'BS13PE',
|
30
|
+
'RG12 1WA', # AA99 9AA format
|
31
|
+
'EH991SP',
|
32
|
+
'AI-2640', # Anguilla
|
33
|
+
'ASCN 1ZZ', # Ascension Island
|
34
|
+
'ASCN1ZZ',
|
35
|
+
'STHL 1ZZ', # St. Helena
|
36
|
+
'STHL1ZZ',
|
37
|
+
'TDCU 1ZZ', # Tristanda Cunha
|
38
|
+
'TDCU1ZZ',
|
39
|
+
'BBND 1ZZ', # British Indian Ocean Territory
|
40
|
+
'BBND1ZZ',
|
41
|
+
'BIQQ 1ZZ', # British Antarctic Territory
|
42
|
+
'BIQQ1ZZ',
|
43
|
+
'FIQQ 1ZZ', # Falkland Islands
|
44
|
+
'FIQQ1ZZ',
|
45
|
+
'GX11 1AA', # Gibraltar
|
46
|
+
'GX111ZZ',
|
47
|
+
'PCRN 1ZZ', # Pitcairn Islands
|
48
|
+
'PCRN1ZZ',
|
49
|
+
'SIQQ 1ZZ', # South Georgia and South Sandwich Islands
|
50
|
+
'SIQQ1ZZ',
|
51
|
+
'TKCA 1ZZ', # Turks and Caicos Islands
|
52
|
+
'TKCA1ZZ',
|
53
|
+
'GIR 0AA', # Girobank / Santander
|
54
|
+
'GIR0AA',
|
55
|
+
]
|
56
|
+
end
|
57
|
+
|
58
|
+
def invalid_postcodes
|
59
|
+
[
|
60
|
+
# invalid 1st pos characters - Q,V,X
|
61
|
+
'QB1 1AA',
|
62
|
+
'V2 4SU',
|
63
|
+
'XA12 2AB',
|
64
|
+
|
65
|
+
# invalid 2nd pos characters - I,J,Z
|
66
|
+
'BI9 1AA',
|
67
|
+
'CJ12 2SA',
|
68
|
+
'AZ9 9WC',
|
69
|
+
|
70
|
+
# invalid 3rd pos characters - I,L,M,N,O,Q,R,V,X,Y,Z
|
71
|
+
'W1I 1ST',
|
72
|
+
'W1L 1ST',
|
73
|
+
'W1M 1ST',
|
74
|
+
'W1N 1ST',
|
75
|
+
'W1O 1ST',
|
76
|
+
'W1Q 1ST',
|
77
|
+
'W1R 1ST',
|
78
|
+
'W1V 1ST',
|
79
|
+
'W1X 1ST',
|
80
|
+
'W1Y 1ST',
|
81
|
+
'W1Z 1ST',
|
82
|
+
|
83
|
+
# invalid 4th pos characters - C,D,F,G,I,J,K,L,O,Q,S,T,U,Z
|
84
|
+
'EC1C 2WA',
|
85
|
+
'EC1D 2WA',
|
86
|
+
'EC1F 2WA',
|
87
|
+
'EC1G 2WA',
|
88
|
+
'EC1I 2WA',
|
89
|
+
'EC1J 2WA',
|
90
|
+
'EC1K 2WA',
|
91
|
+
'EC1L 2WA',
|
92
|
+
'EC1O 2WA',
|
93
|
+
'EC1Q 2WA',
|
94
|
+
'EC1S 2WA',
|
95
|
+
'EC1T 2WA',
|
96
|
+
'EC1U 2WA',
|
97
|
+
'EC1Z 2WA',
|
98
|
+
|
99
|
+
# invalid final 2 letters, C,I,K,M,O,V
|
100
|
+
'BS15 3CE',
|
101
|
+
'BS15 3IE',
|
102
|
+
'BS15 3KE',
|
103
|
+
'BS15 3ME',
|
104
|
+
'BS15 3OE',
|
105
|
+
'BS15 3VE',
|
106
|
+
'BS15 3PC',
|
107
|
+
'BS15 3PI',
|
108
|
+
'BS15 3PK',
|
109
|
+
'BS15 3PM',
|
110
|
+
'BS15 3PO',
|
111
|
+
'BS15 3PV',
|
112
|
+
|
113
|
+
'',
|
114
|
+
' ',
|
115
|
+
'1w23 12e'
|
116
|
+
]
|
117
|
+
end
|
118
|
+
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.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-11-
|
12
|
+
date: 2013-11-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -89,8 +89,10 @@ files:
|
|
89
89
|
- Rakefile
|
90
90
|
- lib/uk_validators.rb
|
91
91
|
- lib/uk_validators/nino_validator.rb
|
92
|
+
- lib/uk_validators/postcode_validator.rb
|
92
93
|
- lib/uk_validators/version.rb
|
93
94
|
- test/nino_test.rb
|
95
|
+
- test/postcode_test.rb
|
94
96
|
- test/test_helper.rb
|
95
97
|
- uk_validators.gemspec
|
96
98
|
homepage: https://github.com/tonyheadford/uk_validators
|
@@ -108,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
108
110
|
version: '0'
|
109
111
|
segments:
|
110
112
|
- 0
|
111
|
-
hash:
|
113
|
+
hash: 4864723644629626
|
112
114
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
115
|
none: false
|
114
116
|
requirements:
|
@@ -117,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
117
119
|
version: '0'
|
118
120
|
segments:
|
119
121
|
- 0
|
120
|
-
hash:
|
122
|
+
hash: 4864723644629626
|
121
123
|
requirements: []
|
122
124
|
rubyforge_project:
|
123
125
|
rubygems_version: 1.8.25
|
@@ -126,4 +128,5 @@ specification_version: 3
|
|
126
128
|
summary: Rails validations for UK data
|
127
129
|
test_files:
|
128
130
|
- test/nino_test.rb
|
131
|
+
- test/postcode_test.rb
|
129
132
|
- test/test_helper.rb
|