vat_id_validator 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ecc7209a52f4b1c5e1adfa7d690465d17d609c8d
4
+ data.tar.gz: 8669e92aad4b428409e17e514c1f69c2ede1565b
5
+ SHA512:
6
+ metadata.gz: 7a89c19e89adfb022ee15bd024bd97040459a97dd2b9c9bce747c6e894b62180565f58a5f567f8004250796dd92c898439867c59090f522a8f7cd8510996824b
7
+ data.tar.gz: 283e16f0d41c6dc96728c7485b60089a64799375ba275fd76d71bfd703bc7ea5ac26cf23eba47998cf55f49ad90e4316f3df79a0694d4c6ee944f1c45984082a
data/.cane ADDED
@@ -0,0 +1,3 @@
1
+ --abc-glob */{lib,spec}/**/*.rb
2
+ --style-glob */{lib,spec}/**/*.rb
3
+ --doc-glob */{lib,spec}/**/*.rb'
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.yardopts ADDED
@@ -0,0 +1,2 @@
1
+ lib/**/*.rb
2
+ lib/*.rb
data/Fudgefile ADDED
@@ -0,0 +1,5 @@
1
+ build :default do
2
+ yard 'stats --list-undoc -q', :coverage => 100
3
+ cane :max_width => 200
4
+ rspec :coverage => 100
5
+ end
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard 'rspec', :version => 2, :cli => '--drb' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
data/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2013 Sage
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # Vat-Id validator
2
+
3
+ A validator for VAT-ID based on ActiveModel::EachValidator.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'vat_id_validator'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install vat_id_validator
18
+
19
+ ## Usage Example
20
+
21
+ Your model must respond to #errors with an instance of ActiveModel::Errors. Then include ActiveModel::Validations:
22
+
23
+ class FinancialSettings
24
+ extend ActiveModel::Naming
25
+ include ActiveModel::Validations
26
+
27
+ attr_reader :errors
28
+ attr_accessor :tax_number
29
+
30
+ def initialize
31
+ @errors = ActiveModel::Errors.new(self)
32
+ end
33
+
34
+ validates :tax_number, :vat_id => true
35
+
36
+ # ...
37
+ end
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,89 @@
1
+ # encoding: UTF-8
2
+ require 'active_model/validator'
3
+ require 'active_support/core_ext/object/blank'
4
+
5
+ # Namespace module.
6
+ module ActiveModel
7
+ # Namespace module.
8
+ module Validations
9
+ # Validator for VAT-ID based on ActiveModel::EachValidator.
10
+ class VatIdValidator < ::ActiveModel::EachValidator
11
+
12
+ # All Countries with the vat pattern
13
+ VAT_PATTERNS = {
14
+ 'AL' => /\AAL(J|K)[0-9]{8}[A-Z]\Z/, # Albania
15
+ 'AR' => /\AAR[0-9]{11}\Z/, # Argentina
16
+ 'AT' => /\AATU[0-9]{8}\Z/, # Austria
17
+ 'AU' => /\AAU[0-9]{9}\Z/, # Australia
18
+ 'BE' => /\ABE0[0-9]{9}\Z/, # Belgium
19
+ 'BG' => /\ABG[0-9]{9,10}\Z/, # Bulgaria
20
+ 'BO' => /\ABO/, # Bolivia
21
+ 'BR' => /\ABR[0-9]{8}\Z/, # Brasil
22
+ 'BY' => /\ABY[0-9]{9}\Z/, # Belarus
23
+ 'CA' => /\ACA[0-9]{15}\Z/, # Canada
24
+ 'CH' => /\ACHE[0-9]{9}(MWST|TVA|IVA)\Z/, # Switzerland
25
+ 'CL' => /\ACL[0-9]{9}\Z/, # Chile
26
+ 'CO' => /\ACO[0-9]{10}\Z/, # Colombia
27
+ 'CR' => /\ACR/, # Costa Rica
28
+ 'CY' => /\ACY[0-9]{8}[A-Z]\Z/, # Cyprus
29
+ 'CZ' => /\ACZ[0-9]{8,10}\Z/, # Czech Republic
30
+ 'DE' => /\ADE[0-9]{9}\Z/, # Germany
31
+ 'DK' => /\ADK[0-9]{8}\Z/, # Denmark
32
+ 'DO' => /\ADO/, # Dominican Republic
33
+ 'EC' => /\AEC[0-9]{13}\Z/, # Ecuador
34
+ 'EE' => /\AEE[0-9]{9}\Z/, # Estonia
35
+ 'EL' => /\AEL[0-9]{9}\Z/, # Greece
36
+ 'ES' => /\AES([A-Z][0-9]{8}|[0-9]{8}[A-Z]|[A-Z][0-9]{7}[A-Z])\Z/, # Spain
37
+ 'FI' => /\AFI[0-9]{8}\Z/, # Finland
38
+ 'FR' => /\AFR[A-Z0-9]{2}[0-9]{9}\Z/, # France
39
+ 'GB' => /\AGB([0-9]{9}|[0-9]{12}|(HA|GD)[0-9]{3})\Z/, # United Kingdom
40
+ 'GT' => /\AGT[0-9]{8}\Z/, # Guatemala
41
+ 'HN' => /\AHN/, # Honduras
42
+ 'HR' => /\AHR[0-9]{11}\Z/, # Croatia
43
+ 'HU' => /\AHU[0-9]{8}\Z/, # Hungary
44
+ 'IE' => /\AIE([0-9][A-Z][0-9]{5}|[0-9]{7})[A-Z]\Z/, # Ireland
45
+ 'IT' => /\AIT[0-9]{11}\Z/, # Italy
46
+ 'LT' => /\ALT([0-9]{9}|[0-9]{12})\Z/, # Lithuania
47
+ 'LU' => /\ALU[0-9]{8}\Z/, # Luxembourg
48
+ 'LV' => /\ALV[0-9]{11}\Z/, # Latvia
49
+ 'MT' => /\AMT[0-9]{8}\Z/, # Malta
50
+ 'MX' => /\AMX[0-9]{12}\Z/, # Mexico
51
+ 'NI' => /\ANI/, # Nicaragua
52
+ 'NL' => /\ANL[0-9]{9}B[0-9]{2}\Z/, # Netherlands
53
+ 'NO' => /\ANO[0-9]{9}\Z/, # Norway
54
+ 'PA' => /\APA/, # Panama
55
+ 'PE' => /\APE/, # Peru
56
+ 'PH' => /\APH[0-9]{12}\Z/, # Philippines
57
+ 'PL' => /\APL[0-9]{10}\Z/, # Poland
58
+ 'PT' => /\APT[0-9]{9}\Z/, # Portugal
59
+ 'PY' => /\APY/, # Paraguay
60
+ 'RO' => /\ARO[1-9][0-9]{1,9}\Z/, # Romania
61
+ 'RU' => /\ARU[0-9]{10}\Z/, # Russia
62
+ 'SE' => /\ASE[0-9]{10}01\Z/, # Sweden
63
+ 'SI' => /\ASI[0-9]{8}\Z/, # Slovenia
64
+ 'SK' => /\ASK[0-9]{10}\Z/, # Slovakia
65
+ 'SM' => /\ASM[0-9]{5}\Z/, # San Marino
66
+ 'SV' => /\ASV/, # El Salvador
67
+ 'TR' => /\ATR[0-9]{10}\Z/, # Turkey
68
+ 'UA' => /\AUA[0-9]{12}\Z/, # Ukraine
69
+ 'UY' => /\AUY/, # Uruguay
70
+ 'VE' => /\AVE(J|G|V|E)[0-9]{9}\Z/ # Venezuela
71
+ }
72
+
73
+ # Validates an attribute of a record if it contains a valid VAT-ID.
74
+ #
75
+ # If it does not, an error is added for that attribute to the record's errors array.
76
+ #
77
+ # @param [#errors] record The record which is validated.
78
+ # @param [Symbol] attribute The record's attribute which is validated.
79
+ # @param [String, nil] value The value which is supposed to be a VAT-ID.
80
+ def validate_each(record, attribute, value)
81
+ if value.present?
82
+ value = value.gsub(/[^A-z0-9]/, '').upcase
83
+ country_code = value[0..1]
84
+ record.errors.add(attribute, :invalid) unless value =~ VAT_PATTERNS[country_code]
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,49 @@
1
+ module VatIdValidator # :nodoc:
2
+ module Matchers # :nodoc:
3
+ # Ensure that the attribute is a vat id.
4
+ #
5
+ # Examples:
6
+ # it { should validate_vat_id_of(:tax_number) }
7
+ #
8
+ def validate_vat_id_of(expected)
9
+ ValidateVatIdOf.new(expected)
10
+ end
11
+
12
+ # Represents the rspec matcher for the vat_id validator
13
+ class ValidateVatIdOf
14
+ def initialize(expected)
15
+ @expected = expected
16
+ end
17
+
18
+ # Checks if the given Objects has a validator for the expected attribute
19
+ #
20
+ # @param [Object] actual the object with the expected attribute
21
+ # @return [boolean]
22
+ def matches?(actual)
23
+ validators = actual._validators[@expected]
24
+ validators.select { |val| val.kind_of?(ActiveModel::Validations::VatIdValidator) }.count > 0
25
+ end
26
+
27
+ # Returns a error message for should matches
28
+ #
29
+ # @return [String] the error message
30
+ def failure_message_for_should
31
+ "expected that #{@expected} would be a vat id"
32
+ end
33
+
34
+ # Returns a error message for should_not matches
35
+ #
36
+ # @return [String] the error message
37
+ def failure_message_for_should_not
38
+ "expected that #{@expected} would not be a vat id"
39
+ end
40
+
41
+ # Returns the description for the matcher
42
+ #
43
+ # @return [String] the description
44
+ def description
45
+ "be a vat id"
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,7 @@
1
+ require 'vat_id_validator/version'
2
+ require 'active_model/validations/vat_id_validator'
3
+ require 'rspec/matchers/validate_vat_id_of'
4
+
5
+ # Namespace module.
6
+ module VatIdValidator
7
+ end
@@ -0,0 +1,5 @@
1
+ # Namespace module.
2
+ module VatIdValidator
3
+ # :nodoc:
4
+ VERSION = "0.0.2"
5
+ end
@@ -0,0 +1,944 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe ActiveModel::Validations::VatIdValidator do
5
+ subject { ActiveModelTestClass.new }
6
+
7
+ shared_examples_for 'tax_number with 8 digits' do
8
+ it 'should be valid with 8 digits' do
9
+ subject.tax_number = prefix + '12345678'
10
+ subject.should be_valid
11
+ end
12
+
13
+ it 'should not be valid with less then 8 digits' do
14
+ subject.tax_number = prefix + '1234567'
15
+ subject.should_not be_valid
16
+ end
17
+
18
+ it 'should not be valid with more then 8 digits' do
19
+ subject.tax_number = prefix + '123456789'
20
+ subject.should_not be_valid
21
+ end
22
+ end
23
+
24
+ shared_examples_for 'tax_number with 9 digits' do
25
+ it 'should be valid with 9 digits' do
26
+ subject.tax_number = prefix + '123456789'
27
+ subject.should be_valid
28
+ end
29
+
30
+ it 'should not be valid with less then 9 digits' do
31
+ subject.tax_number = prefix + '12345678'
32
+ subject.should_not be_valid
33
+ end
34
+
35
+ it 'should not be valid with more then 9 digits' do
36
+ subject.tax_number = prefix + '1234567890'
37
+ subject.should_not be_valid
38
+ end
39
+ end
40
+
41
+ shared_examples_for 'tax_number with 10 digits' do
42
+ it 'should be valid with 10 digits' do
43
+ subject.tax_number = prefix + '1234567890'
44
+ subject.should be_valid
45
+ end
46
+
47
+ it 'should not be valid with less then 10 digits' do
48
+ subject.tax_number = prefix + '123456789'
49
+ subject.should_not be_valid
50
+ end
51
+
52
+ it 'should not be valid with more then 10 digits' do
53
+ subject.tax_number = prefix + '12345678901'
54
+ subject.should_not be_valid
55
+ end
56
+ end
57
+
58
+ shared_examples_for 'tax_number with 11 digits' do
59
+ it 'should be valid with 11 digits' do
60
+ subject.tax_number = prefix + '12345678901'
61
+ subject.should be_valid
62
+ end
63
+
64
+ it 'should not be valid with less then 11 digits' do
65
+ subject.tax_number = prefix + '1234567890'
66
+ subject.should_not be_valid
67
+ end
68
+
69
+ it 'should not be valid with more then 11 digits' do
70
+ subject.tax_number = prefix + '123456789012'
71
+ subject.should_not be_valid
72
+ end
73
+ end
74
+
75
+ shared_examples_for 'tax_number with 12 digits' do
76
+ it 'should be valid with 12 digits' do
77
+ subject.tax_number = prefix + '123456789012'
78
+ subject.should be_valid
79
+ end
80
+
81
+ it 'should not be valid with less then 12 digits' do
82
+ subject.tax_number = prefix + '12345678901'
83
+ subject.should_not be_valid
84
+ end
85
+
86
+ it 'should not be valid with more then 12 digits' do
87
+ subject.tax_number = prefix + '1234567890123'
88
+ subject.should_not be_valid
89
+ end
90
+ end
91
+
92
+ shared_examples_for 'tax_number with only prefix check' do
93
+ it 'should be valid with the given prefix' do
94
+ subject.tax_number = prefix + '123ABC'
95
+ subject.should be_valid
96
+ end
97
+ end
98
+
99
+ it 'should be valid without tax_number' do
100
+ should be_valid
101
+ end
102
+
103
+ it 'should not be valid without prefix' do
104
+ subject.tax_number = '123456789'
105
+ subject.should_not be_valid
106
+ end
107
+
108
+ it 'should ignore special character and spaces for validation (formatting)' do
109
+ subject.tax_number = 'DE 12-345.678/9'
110
+ subject.should be_valid
111
+ end
112
+
113
+ context 'when the tax_number starts with' do
114
+
115
+ context 'AL (Albania)' do
116
+ let(:prefix) { 'AL' }
117
+
118
+ it 'should be valid with "K" + 8 digits + 1 char' do
119
+ subject.tax_number = prefix + 'K12345678X'
120
+ subject.should be_valid
121
+ end
122
+
123
+ it 'should be valid with "J" + 8 digits + 1 char' do
124
+ subject.tax_number = prefix + 'J12345678X'
125
+ subject.should be_valid
126
+ end
127
+
128
+ it 'should not be valid if the first char is not "K" or "J"' do
129
+ subject.tax_number = prefix + 'X12345678X'
130
+ subject.should_not be_valid
131
+ end
132
+
133
+ it 'should not be valid with less then 8 digits' do
134
+ subject.tax_number = prefix + 'J1234567X'
135
+ subject.should_not be_valid
136
+ end
137
+
138
+ it 'should not be valid with more then 8 digits' do
139
+ subject.tax_number = prefix + 'K123456789X'
140
+ subject.should_not be_valid
141
+ end
142
+ end
143
+
144
+ context 'AR (Argentina)' do
145
+ let(:prefix) { 'AR' }
146
+
147
+ it_behaves_like 'tax_number with 11 digits'
148
+ end
149
+
150
+ context 'ATU (Austria)' do
151
+ let(:prefix) { 'ATU' }
152
+
153
+ it_behaves_like 'tax_number with 8 digits'
154
+ end
155
+
156
+ context 'AU (Australia)' do
157
+ let(:prefix) { 'AU' }
158
+
159
+ it_behaves_like 'tax_number with 9 digits'
160
+ end
161
+
162
+ context 'BE (Belgium)' do
163
+ let(:prefix) { 'BE' }
164
+
165
+ it 'should be valid with 10 digits (first digit == 0)' do
166
+ subject.tax_number = prefix + '0123456789'
167
+ subject.should be_valid
168
+ end
169
+
170
+ it 'should not be valid with 10 digits (first digit != 0)' do
171
+ subject.tax_number = prefix + '1123456789'
172
+ subject.should_not be_valid
173
+ end
174
+
175
+ it 'should not be valid with less then 10 digits' do
176
+ subject.tax_number = prefix + '012345678'
177
+ subject.should_not be_valid
178
+ end
179
+
180
+ it 'should not be valid with more then 10 digits' do
181
+ subject.tax_number = prefix + '01234567890'
182
+ subject.should_not be_valid
183
+ end
184
+ end
185
+
186
+ context 'BG (Bulgaria)' do
187
+ let(:prefix) { 'BG' }
188
+
189
+ it 'should be valid with 9 digits' do
190
+ subject.tax_number = prefix + '123456789'
191
+ subject.should be_valid
192
+ end
193
+
194
+ it 'should be valid with 10 digits' do
195
+ subject.tax_number = prefix + '1234567890'
196
+ subject.should be_valid
197
+ end
198
+
199
+ it 'should not be valid with less then 9 digits' do
200
+ subject.tax_number = prefix + '12345678'
201
+ subject.should_not be_valid
202
+ end
203
+
204
+ it 'should not be valid with more then 10 digits' do
205
+ subject.tax_number = prefix + '12345678901'
206
+ subject.should_not be_valid
207
+ end
208
+ end
209
+
210
+ context 'BO (Bolivia)' do
211
+ let(:prefix) { 'BO' }
212
+
213
+ it_behaves_like 'tax_number with only prefix check'
214
+ end
215
+
216
+ context 'BR (Brasil)' do
217
+ let(:prefix) { 'BR' }
218
+
219
+ it_behaves_like 'tax_number with 8 digits'
220
+ end
221
+
222
+ context 'BY (Belarus)' do
223
+ let(:prefix) { 'BY' }
224
+
225
+ it_behaves_like 'tax_number with 9 digits'
226
+ end
227
+
228
+ context 'CA (Canada)' do
229
+ let(:prefix) { 'CA' }
230
+
231
+ it 'should be valid with 15 digits' do
232
+ subject.tax_number = prefix + '123456789012345'
233
+ subject.should be_valid
234
+ end
235
+
236
+ it 'should not be valid with less then 15 digits' do
237
+ subject.tax_number = prefix + '12345678901234'
238
+ subject.should_not be_valid
239
+ end
240
+
241
+ it 'should not be valid with more then 15 digits' do
242
+ subject.tax_number = prefix + '1234567890123456'
243
+ subject.should_not be_valid
244
+ end
245
+ end
246
+
247
+ context 'CHE (Switzerland)' do
248
+ let(:prefix) { 'CHE' }
249
+
250
+ it 'should not be valid without suffix' do
251
+ subject.tax_number = prefix + '123456789'
252
+ subject.should_not be_valid
253
+ end
254
+
255
+ context 'and the tax_number ends with MWST (de-CH)' do
256
+ let(:suffix) { 'MWST' }
257
+ it 'should be valid with 9 digits' do
258
+ subject.tax_number = prefix + '123456789' + suffix
259
+ subject.should be_valid
260
+ end
261
+
262
+ it 'should not be valid with less then 9 digits' do
263
+ subject.tax_number = prefix + '12345678' + suffix
264
+ subject.should_not be_valid
265
+ end
266
+
267
+ it 'should not be valid with more then 9 digits' do
268
+ subject.tax_number = prefix + '1234567890' + suffix
269
+ subject.should_not be_valid
270
+ end
271
+ end
272
+
273
+ context 'and the tax_number ends with TVA (de-FR)' do
274
+ let(:suffix) { 'TVA' }
275
+
276
+ it 'should be valid with 9 digits' do
277
+ subject.tax_number = prefix + '123456789' + suffix
278
+ subject.should be_valid
279
+ end
280
+
281
+ it 'should not be valid with less then 9 digits' do
282
+ subject.tax_number = prefix + '12345678' + suffix
283
+ subject.should_not be_valid
284
+ end
285
+
286
+ it 'should not be valid with more then 9 digits' do
287
+ subject.tax_number = prefix + '1234567890' + suffix
288
+ subject.should_not be_valid
289
+ end
290
+ end
291
+
292
+ context 'and the tax_number ends with IVA (de-IT)' do
293
+ let(:suffix) { 'IVA' }
294
+ it 'should be valid with 9 digits' do
295
+ subject.tax_number = prefix + '123456789' + suffix
296
+ subject.should be_valid
297
+ end
298
+
299
+ it 'should not be valid with less then 9 digits' do
300
+ subject.tax_number = prefix + '12345678' + suffix
301
+ subject.should_not be_valid
302
+ end
303
+
304
+ it 'should not be valid with more then 9 digits' do
305
+ subject.tax_number = prefix + '1234567890' + suffix
306
+ subject.should_not be_valid
307
+ end
308
+ end
309
+
310
+
311
+ end
312
+
313
+ context 'CL (Chile)' do
314
+ let(:prefix) { 'CL' }
315
+
316
+ it_behaves_like 'tax_number with 9 digits'
317
+ end
318
+
319
+ context 'CO (Colombia)' do
320
+ let(:prefix) { 'CO' }
321
+
322
+ it_behaves_like 'tax_number with 10 digits'
323
+ end
324
+
325
+ context 'CR (Costa Rica)' do
326
+ let(:prefix) { 'CR' }
327
+
328
+ it_behaves_like 'tax_number with only prefix check'
329
+ end
330
+
331
+ context 'CY (Cyprus)' do
332
+ let(:prefix) { 'CY' }
333
+ let(:suffix) { 'L' } # A-Z
334
+
335
+ it 'should not be valid without suffix' do
336
+ subject.tax_number = prefix + '12345678'
337
+ subject.should_not be_valid
338
+ end
339
+
340
+ it 'should be valid with 8 digits' do
341
+ subject.tax_number = prefix + '12345678' + suffix
342
+ subject.should be_valid
343
+ end
344
+
345
+ it 'should not be valid with less then 8 digits' do
346
+ subject.tax_number = prefix + '1234567' + suffix
347
+ subject.should_not be_valid
348
+ end
349
+
350
+ it 'should not be valid with more then 8 digits' do
351
+ subject.tax_number = prefix + '123456789' + suffix
352
+ subject.should_not be_valid
353
+ end
354
+ end
355
+
356
+ context 'CZ (Czech Republic)' do
357
+ let(:prefix) { 'CZ' }
358
+
359
+ it 'should be valid with 8 digits' do
360
+ subject.tax_number = prefix + '12345678'
361
+ subject.should be_valid
362
+ end
363
+
364
+ it 'should be valid with 9 digits' do
365
+ subject.tax_number = prefix + '123456789'
366
+ subject.should be_valid
367
+ end
368
+
369
+ it 'should be valid with 10 digits' do
370
+ subject.tax_number = prefix + '1234567890'
371
+ subject.should be_valid
372
+ end
373
+
374
+ it 'should not be valid with less then 8 digits' do
375
+ subject.tax_number = prefix + '1234567'
376
+ subject.should_not be_valid
377
+ end
378
+
379
+ it 'should not be valid with more then 10 digits' do
380
+ subject.tax_number = prefix + '12345678901'
381
+ subject.should_not be_valid
382
+ end
383
+ end
384
+
385
+ context 'DE (Germany)' do
386
+ let(:prefix) { 'DE' }
387
+
388
+ it_behaves_like 'tax_number with 9 digits'
389
+ end
390
+
391
+ context 'DK (Denmark)' do
392
+ let(:prefix) { 'DK' }
393
+
394
+ it_behaves_like 'tax_number with 8 digits'
395
+ end
396
+
397
+ context 'DO (Dominican Republic)' do
398
+ let(:prefix) { 'DO' }
399
+
400
+ it_behaves_like 'tax_number with only prefix check'
401
+ end
402
+
403
+ context 'EC (Ecuador)' do
404
+ let(:prefix) { 'EC' }
405
+
406
+ it 'should be valid with 13 digits' do
407
+ subject.tax_number = prefix + '1234567890123'
408
+ subject.should be_valid
409
+ end
410
+
411
+ it 'should not be valid with less then 13 digits' do
412
+ subject.tax_number = prefix + '123456789012'
413
+ subject.should_not be_valid
414
+ end
415
+
416
+ it 'should not be valid with more then 13 digits' do
417
+ subject.tax_number = prefix + '12345678901234'
418
+ subject.should_not be_valid
419
+ end
420
+ end
421
+
422
+ context 'EE (Estonia)' do
423
+ let(:prefix) { 'EE' }
424
+
425
+ it_behaves_like 'tax_number with 9 digits'
426
+ end
427
+
428
+ context 'EL (Greece)' do
429
+ let(:prefix) { 'EL' }
430
+
431
+ it_behaves_like 'tax_number with 9 digits'
432
+ end
433
+
434
+ context 'ES (Spain)' do
435
+ let(:prefix) { 'ES' }
436
+
437
+ it 'should be valid with 1 char + 8 digits' do
438
+ subject.tax_number = prefix + 'X12345678'
439
+ subject.should be_valid
440
+ end
441
+
442
+ it 'should be valid with 8 digits + 1 char' do
443
+ subject.tax_number = prefix + '12345678X'
444
+ subject.should be_valid
445
+ end
446
+
447
+ it 'should be valid with 1 char + 7 digits + 1 char' do
448
+ subject.tax_number = prefix + 'X1234567X'
449
+ subject.should be_valid
450
+ end
451
+
452
+ it 'should not be valid with less then 9 digits/chars' do
453
+ subject.tax_number = prefix + 'X1234567'
454
+ subject.should_not be_valid
455
+ end
456
+
457
+ it 'should not be valid with more then 9 digits/chats' do
458
+ subject.tax_number = prefix + '123456789X'
459
+ subject.should_not be_valid
460
+ end
461
+ end
462
+
463
+ context 'FI (Finland)' do
464
+ let(:prefix) { 'FI' }
465
+
466
+ it_behaves_like 'tax_number with 8 digits'
467
+ end
468
+
469
+ context 'FR (France)' do
470
+ let(:prefix) { 'FR' }
471
+
472
+ it 'should be valid with 2 digits/chars + 9 digits' do
473
+ subject.tax_number = prefix + '1X123456789'
474
+ subject.should be_valid
475
+ end
476
+
477
+ it 'should not be valid with less then 11 digits' do
478
+ subject.tax_number = prefix + '1234567890'
479
+ subject.should_not be_valid
480
+ end
481
+
482
+ it 'should not be valid with more then 11 digits' do
483
+ subject.tax_number = prefix + '123456789012'
484
+ subject.should_not be_valid
485
+ end
486
+ end
487
+
488
+ context 'GB (United Kingdom)' do
489
+ let(:prefix) { 'GB' }
490
+
491
+ context 'with digets only' do
492
+ it 'should be valid with 9 digits (standard)' do
493
+ subject.tax_number = prefix + '123456789'
494
+ subject.should be_valid
495
+ end
496
+
497
+ it 'should be valid with 12 digits (branch traders)' do
498
+ subject.tax_number = prefix + '123456789012'
499
+ subject.should be_valid
500
+ end
501
+
502
+ it 'should not be valid with less then 9 digits' do
503
+ subject.tax_number = prefix + '12345678'
504
+ subject.should_not be_valid
505
+ end
506
+
507
+ it 'should not be valid with 10 digits' do
508
+ subject.tax_number = prefix + '1234567890'
509
+ subject.should_not be_valid
510
+ end
511
+
512
+ it 'should not be valid with 11 digits' do
513
+ subject.tax_number = prefix + '12345678901'
514
+ subject.should_not be_valid
515
+ end
516
+
517
+ it 'should not be valid with more then 12 digits' do
518
+ subject.tax_number = prefix + '1234567890123'
519
+ subject.should_not be_valid
520
+ end
521
+ end
522
+
523
+ context 'with prefix addition "HA" (health authorities)' do
524
+ let(:addition) { 'HA' }
525
+
526
+ it 'should be valid with 3 digits' do
527
+ subject.tax_number = prefix + addition + '123'
528
+ subject.should be_valid
529
+ end
530
+
531
+ it 'should not be valid with less then 3 digits' do
532
+ subject.tax_number = prefix + addition + '12'
533
+ subject.should_not be_valid
534
+ end
535
+
536
+ it 'should not be valid with more then 3 digits' do
537
+ subject.tax_number = prefix + addition + '1234'
538
+ subject.should_not be_valid
539
+ end
540
+ end
541
+
542
+ context 'with prefix addition "GD" (government departments)' do
543
+ let(:addition) { 'GD' }
544
+
545
+ it 'should be valid with 3 digits' do
546
+ subject.tax_number = prefix + addition + '123'
547
+ subject.should be_valid
548
+ end
549
+
550
+ it 'should not be valid with less then 3 digits' do
551
+ subject.tax_number = prefix + addition + '12'
552
+ subject.should_not be_valid
553
+ end
554
+
555
+ it 'should not be valid with more then 3 digits' do
556
+ subject.tax_number = prefix + addition + '1234'
557
+ subject.should_not be_valid
558
+ end
559
+ end
560
+ end
561
+
562
+ context 'GT (Guatemala' do
563
+ let(:prefix) { 'GT' }
564
+
565
+ it_behaves_like 'tax_number with 8 digits'
566
+ end
567
+
568
+ context 'HN (Honduras)' do
569
+ let(:prefix) { 'HN' }
570
+
571
+ it_behaves_like 'tax_number with only prefix check'
572
+ end
573
+
574
+ context 'HR (Croatia)' do
575
+ let(:prefix) { 'HR' }
576
+
577
+ it_behaves_like 'tax_number with 11 digits'
578
+ end
579
+
580
+ context 'HU (Hungary)' do
581
+ let(:prefix) { 'HU' }
582
+
583
+ it_behaves_like 'tax_number with 8 digits'
584
+ end
585
+
586
+ context 'IE (Ireland)' do
587
+ let(:prefix) { 'IE' }
588
+
589
+ it 'should be valid with 1 digit + 1 char + 5 digets + 1 char' do
590
+ subject.tax_number = prefix + '1X12345L'
591
+ subject.should be_valid
592
+ end
593
+
594
+ it 'should be valid with 7 digits + 1 char' do
595
+ subject.tax_number = prefix + '1234567L'
596
+ subject.should be_valid
597
+ end
598
+
599
+ it 'should not be valid if last is not a char' do
600
+ subject.tax_number = prefix + '12345678'
601
+ subject.should_not be_valid
602
+ end
603
+
604
+ it 'should not be valid with less then 7 digits + 1 char' do
605
+ subject.tax_number = prefix + '123456L'
606
+ subject.should_not be_valid
607
+ end
608
+
609
+ it 'should not be valid with more then 7 digits + 1 char' do
610
+ subject.tax_number = prefix + '12345678L'
611
+ subject.should_not be_valid
612
+ end
613
+ end
614
+
615
+ context 'IT (Italy)' do
616
+ let(:prefix) { 'IT' }
617
+
618
+ it_behaves_like 'tax_number with 11 digits'
619
+ end
620
+
621
+ context 'LT (Lithuania)' do
622
+ let(:prefix) { 'LT' }
623
+
624
+ it 'should be valid with 9 digits' do
625
+ subject.tax_number = prefix + '123456789'
626
+ subject.should be_valid
627
+ end
628
+
629
+ it 'should be valid with 12 digits' do
630
+ subject.tax_number = prefix + '123456789012'
631
+ subject.should be_valid
632
+ end
633
+
634
+ it 'should not be valid with less then 9 digits' do
635
+ subject.tax_number = prefix + '12345678'
636
+ subject.should_not be_valid
637
+ end
638
+
639
+ it 'should not be valid with 10 digits' do
640
+ subject.tax_number = prefix + '1234567890'
641
+ subject.should_not be_valid
642
+ end
643
+
644
+ it 'should not be valid with 11 digits' do
645
+ subject.tax_number = prefix + '12345678901'
646
+ subject.should_not be_valid
647
+ end
648
+
649
+ it 'should not be valid with more then 12 digits' do
650
+ subject.tax_number = prefix + '1234567890123'
651
+ subject.should_not be_valid
652
+ end
653
+ end
654
+
655
+ context 'LU (Luxembourg)' do
656
+ let(:prefix) { 'LU' }
657
+
658
+ it_behaves_like 'tax_number with 8 digits'
659
+ end
660
+
661
+ context 'LV (Latvia)' do
662
+ let(:prefix) { 'LV' }
663
+
664
+ it_behaves_like 'tax_number with 11 digits'
665
+ end
666
+
667
+ context 'MT (Malta)' do
668
+ let(:prefix) { 'MT' }
669
+
670
+ it_behaves_like 'tax_number with 8 digits'
671
+ end
672
+
673
+ context 'MX (Mexico)' do
674
+ let(:prefix) { 'MX' }
675
+
676
+ it_behaves_like 'tax_number with 12 digits'
677
+ end
678
+
679
+ context 'NI (Nicaragua)' do
680
+ let(:prefix) { 'NI' }
681
+
682
+ it_behaves_like 'tax_number with only prefix check'
683
+ end
684
+
685
+ context 'NL (Netherlands)' do
686
+ let(:prefix) { 'NL' }
687
+
688
+ it 'should be valid with 9 digits + "B" + 2 digits' do
689
+ subject.tax_number = prefix + '123456789B12'
690
+ subject.should be_valid
691
+ end
692
+
693
+ it 'should not be valid without "B" on the 10 position' do
694
+ subject.tax_number = prefix + '123456789X12'
695
+ subject.should_not be_valid
696
+ end
697
+
698
+ it 'should not be valid with less 9 digits + "B" + 2 digits' do
699
+ subject.tax_number = prefix + '12345678B12'
700
+ subject.should_not be_valid
701
+ end
702
+
703
+ it 'should not be valid with more 9 digits + "B" + 2 digits' do
704
+ subject.tax_number = prefix + '1234567890B12'
705
+ subject.should_not be_valid
706
+ end
707
+
708
+ it 'should not be valid with 8 digits + "B" + less then 2 digits' do
709
+ subject.tax_number = prefix + '12345678B1'
710
+ subject.should_not be_valid
711
+ end
712
+
713
+ it 'should not be valid with more 8 digits + "B" + more then 2 digits' do
714
+ subject.tax_number = prefix + '12345678B123'
715
+ subject.should_not be_valid
716
+ end
717
+ end
718
+
719
+ context 'NO (Norway)' do
720
+ let(:prefix) { 'NO' }
721
+
722
+ it_behaves_like 'tax_number with 9 digits'
723
+ end
724
+
725
+ context 'PA (Panama)' do
726
+ let(:prefix) { 'PA' }
727
+
728
+ it_behaves_like 'tax_number with only prefix check'
729
+ end
730
+
731
+ context 'PE (Peru)' do
732
+ let(:prefix) { 'SV' }
733
+
734
+ it_behaves_like 'tax_number with only prefix check'
735
+ end
736
+
737
+ context 'PH (Philippines)' do
738
+ let(:prefix) { 'PH' }
739
+
740
+ it_behaves_like 'tax_number with 12 digits'
741
+ end
742
+
743
+ context 'PL (Poland)' do
744
+ let(:prefix) { 'PL' }
745
+
746
+ it_behaves_like 'tax_number with 10 digits'
747
+ end
748
+
749
+ context 'PT (Portugal)' do
750
+ let(:prefix) { 'PT' }
751
+
752
+ it_behaves_like 'tax_number with 9 digits'
753
+ end
754
+
755
+ context 'PY (Paraguay)' do
756
+ let(:prefix) { 'PY' }
757
+
758
+ it_behaves_like 'tax_number with only prefix check'
759
+ end
760
+
761
+ context 'RO (Romania)' do
762
+ let(:prefix) { 'RO' }
763
+
764
+ it 'should be valid with 2 digits' do
765
+ subject.tax_number = prefix + '12'
766
+ subject.should be_valid
767
+ end
768
+
769
+ it 'should be valid with 3 digits' do
770
+ subject.tax_number = prefix + '123'
771
+ subject.should be_valid
772
+ end
773
+
774
+ it 'should be valid with 4 digits' do
775
+ subject.tax_number = prefix + '1234'
776
+ subject.should be_valid
777
+ end
778
+
779
+ it 'should be valid with 5 digits' do
780
+ subject.tax_number = prefix + '12345'
781
+ subject.should be_valid
782
+ end
783
+
784
+ it 'should be valid with 6 digits' do
785
+ subject.tax_number = prefix + '123456'
786
+ subject.should be_valid
787
+ end
788
+
789
+ it 'should be valid with 7 digits' do
790
+ subject.tax_number = prefix + '1234567'
791
+ subject.should be_valid
792
+ end
793
+
794
+ it 'should be valid with 8 digits' do
795
+ subject.tax_number = prefix + '12345678'
796
+ subject.should be_valid
797
+ end
798
+
799
+ it 'should be valid with 9 digits' do
800
+ subject.tax_number = prefix + '123456789'
801
+ subject.should be_valid
802
+ end
803
+
804
+ it 'should be valid with 10 digits' do
805
+ subject.tax_number = prefix + '123456789'
806
+ subject.should be_valid
807
+ end
808
+
809
+ it 'should not be valid if the first digit is 0' do
810
+ subject.tax_number = prefix + '01'
811
+ subject.should_not be_valid
812
+ end
813
+
814
+ it 'should not be valid with less then 2 digits' do
815
+ subject.tax_number = prefix + '1'
816
+ subject.should_not be_valid
817
+ end
818
+
819
+ it 'should not be valid with more then 10 digits' do
820
+ subject.tax_number = prefix + '12345678901'
821
+ subject.should_not be_valid
822
+ end
823
+ end
824
+
825
+ context 'RU (Russia)' do
826
+ let(:prefix) { 'RU' }
827
+
828
+ it_behaves_like 'tax_number with 10 digits'
829
+ end
830
+
831
+ context 'SE (Sweden)' do
832
+ let(:prefix) { 'SE' }
833
+
834
+ it 'should be valid with 10 digits + "01"' do
835
+ subject.tax_number = prefix + '123456789001'
836
+ subject.should be_valid
837
+ end
838
+
839
+ it 'should not be valid without "01" at the end' do
840
+ subject.tax_number = prefix + '123456789012'
841
+ subject.should_not be_valid
842
+ end
843
+
844
+ it 'should not be valid with less then 10 digits + "01"' do
845
+ subject.tax_number = prefix + '12345678901'
846
+ subject.should_not be_valid
847
+ end
848
+
849
+ it 'should not be valid with more then 10 digits + "01"' do
850
+ subject.tax_number = prefix + '1234567890101'
851
+ subject.should_not be_valid
852
+ end
853
+ end
854
+
855
+ context 'SI (Slovenia)' do
856
+ let(:prefix) { 'SI' }
857
+
858
+ it_behaves_like 'tax_number with 8 digits'
859
+ end
860
+
861
+ context 'SK (Slovakia)' do
862
+ let(:prefix) { 'SK' }
863
+
864
+ it_behaves_like 'tax_number with 10 digits'
865
+ end
866
+
867
+ context 'SM (San Marino)' do
868
+ let(:prefix) { 'SM' }
869
+
870
+ it 'should be valid with 5 digits' do
871
+ subject.tax_number = prefix + '12345'
872
+ subject.should be_valid
873
+ end
874
+
875
+ it 'should not be valid with less then 5 digits' do
876
+ subject.tax_number = prefix + '1234'
877
+ subject.should_not be_valid
878
+ end
879
+
880
+ it 'should not be valid with more then 5 digits' do
881
+ subject.tax_number = prefix + '123456'
882
+ subject.should_not be_valid
883
+ end
884
+ end
885
+
886
+ context 'SV (El Salvador)' do
887
+ let(:prefix) { 'SV' }
888
+
889
+ it_behaves_like 'tax_number with only prefix check'
890
+ end
891
+
892
+ context 'TR (Turkey)' do
893
+ let(:prefix) { 'TR' }
894
+
895
+ it_behaves_like 'tax_number with 10 digits'
896
+ end
897
+
898
+ context 'UA (Ukraine)' do
899
+ let(:prefix) { 'UA' }
900
+
901
+ it_behaves_like 'tax_number with 12 digits'
902
+ end
903
+
904
+ context 'UY (Uruguay)' do
905
+ let(:prefix) { 'UY' }
906
+
907
+ it_behaves_like 'tax_number with only prefix check'
908
+ end
909
+
910
+ context 'VE (Venezuala)' do
911
+ let(:prefix) { 'VE' }
912
+
913
+ it 'should be valid with "J" + 9 digits' do
914
+ subject.tax_number = prefix + 'J123456789'
915
+ subject.should be_valid
916
+ end
917
+
918
+ it 'should be valid with "G" + 9 digits' do
919
+ subject.tax_number = prefix + 'G123456789'
920
+ subject.should be_valid
921
+ end
922
+
923
+ it 'should be valid with "V" + 9 digits' do
924
+ subject.tax_number = prefix + 'V123456789'
925
+ subject.should be_valid
926
+ end
927
+
928
+ it 'should be valid with "E" + 9 digits' do
929
+ subject.tax_number = prefix + 'E123456789'
930
+ subject.should be_valid
931
+ end
932
+
933
+ it 'should not be valid with less then 9 digits' do
934
+ subject.tax_number = prefix + 'E12345678'
935
+ subject.should_not be_valid
936
+ end
937
+
938
+ it 'should not be valid with more then 9 digits' do
939
+ subject.tax_number = prefix + 'E1234567890'
940
+ subject.should_not be_valid
941
+ end
942
+ end
943
+ end
944
+ end