romanianvalidators 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +1 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +1 -1
- data/README.md +12 -4
- data/lib/active_model/validations/bic_validator.rb +2 -1
- data/lib/active_model/validations/cif_validator.rb +2 -1
- data/lib/active_model/validations/cnp_validator.rb +2 -1
- data/lib/active_model/validations/iban_validator.rb +2 -1
- data/romanianvalidators.gemspec +2 -1
- data/test/validations/bic_test.rb +14 -1
- data/test/validations/cif_test.rb +16 -2
- data/test/validations/cnp_test.rb +13 -1
- data/test/validations/iban_test.rb +13 -1
- metadata +18 -26
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 77accfec82cda9b706ea0ac3fe7dda0d4c642996
|
4
|
+
data.tar.gz: 9848303566d97e623d97d6f4cf4bc0c4c7ec5410
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 47586d2962a1f8857d3dad0258331e1b9a69a99864515096fcbe7dce94947c6e0f626eb1c906406efc1c6653d9d4f8dc21b6c5d377443fa7448164f43f5a2cf9
|
7
|
+
data.tar.gz: 076077782f9438a71dd877dc97ed1aecc02b0a4e2957eb57cb9bcffd153d0462e177665844bf614bc051ee116617095a5789b5f9376070681b168f819167f41f
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
ADDED
data/Gemfile
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
gemspec
|
data/README.md
CHANGED
@@ -1,10 +1,13 @@
|
|
1
|
-
# Romanian Validators
|
1
|
+
# Romanian Validators [![Build Status](https://travis-ci.org/mtarnovan/romanianvalidators.png)](https://travis-ci.org/mtarnovan/romanianvalidators)
|
2
2
|
|
3
|
-
|
3
|
+
ActiveModel validators for:
|
4
4
|
|
5
5
|
* Cod Numeric Personal (CNP)
|
6
6
|
* Cod de identificare fiscală (CIF) and
|
7
7
|
* IBAN (only Romanian format as published by Romanian National Bank).
|
8
|
+
* BIC
|
9
|
+
|
10
|
+
Extracted from [Factureaza.ro](https://factureaza.ro), our online invoicing solution for the Romanian market.
|
8
11
|
|
9
12
|
## Installation
|
10
13
|
|
@@ -21,9 +24,13 @@ Next install it with Bundler.
|
|
21
24
|
|
22
25
|
$ bundle install
|
23
26
|
|
27
|
+
Has no other dependency than `ActiveModel`, so it should work without Rails too.
|
28
|
+
|
29
|
+
Tested with MRI 1.8.7, 1.9.3, 2.0.0, REE, Rubinius and JRuby (see `.travis.yml`)
|
30
|
+
|
24
31
|
## Usage
|
25
32
|
|
26
|
-
In your models (ActiveModel), the gem provides the following new validators:
|
33
|
+
In your models (`ActiveModel`), the gem provides the following new validators:
|
27
34
|
* CIF
|
28
35
|
* CNP
|
29
36
|
* IBAN
|
@@ -33,7 +40,7 @@ The algorithms for validation are found in the source code.
|
|
33
40
|
|
34
41
|
```ruby
|
35
42
|
class User
|
36
|
-
validates :cnp, :cnp =>
|
43
|
+
validates :cnp, :cnp => { :message => 'This is not a valid CNP'}
|
37
44
|
validates :company_cif, :cif => true
|
38
45
|
end
|
39
46
|
```
|
@@ -41,6 +48,7 @@ The algorithms for validation are found in the source code.
|
|
41
48
|
### TODO
|
42
49
|
|
43
50
|
* test more edge cases; test nil, blank; test messages; test in app
|
51
|
+
* add javascript validation ?
|
44
52
|
|
45
53
|
### Copyright
|
46
54
|
|
@@ -5,9 +5,10 @@ module ActiveModel
|
|
5
5
|
def validate_each(record, attribute, value)
|
6
6
|
allow_blank = options.fetch(:allow_blank, false)
|
7
7
|
allow_nil = options.fetch(:allow_nil, false)
|
8
|
+
message = options.fetch(:message, nil)
|
8
9
|
record.errors.add_on_empty(attribute) if value.nil? && !allow_nil
|
9
10
|
record.errors.add_on_blank(attribute) if value.blank? && !allow_blank
|
10
|
-
record.errors.add(attribute) unless Bic.valid?(value)
|
11
|
+
record.errors.add(attribute, message) unless Bic.valid?(value)
|
11
12
|
end
|
12
13
|
|
13
14
|
# This is only a basic validation of a BIC
|
@@ -5,9 +5,10 @@ module ActiveModel
|
|
5
5
|
def validate_each(record, attribute, value)
|
6
6
|
allow_blank = options.fetch(:allow_blank, false)
|
7
7
|
allow_nil = options.fetch(:allow_nil, false)
|
8
|
+
message = options.fetch(:message, nil)
|
8
9
|
record.errors.add_on_empty(attribute) if value.nil? && !allow_nil
|
9
10
|
record.errors.add_on_blank(attribute) if value.blank? && !allow_blank
|
10
|
-
record.errors.add(attribute) unless Cif.valid?(value)
|
11
|
+
record.errors.add(attribute, message) unless Cif.valid?(value)
|
11
12
|
end
|
12
13
|
|
13
14
|
# Algoritmul de validare al unui cod CUI
|
@@ -5,9 +5,10 @@ module ActiveModel
|
|
5
5
|
def validate_each(record, attribute, value)
|
6
6
|
allow_blank = options.fetch(:allow_blank, false)
|
7
7
|
allow_nil = options.fetch(:allow_nil, false)
|
8
|
+
message = options.fetch(:message, nil)
|
8
9
|
record.errors.add_on_empty(attribute) if value.nil? && !allow_nil
|
9
10
|
record.errors.add_on_blank(attribute) if value.blank? && !allow_blank
|
10
|
-
record.errors.add(attribute) unless Cnp.valid?(value)
|
11
|
+
record.errors.add(attribute, message) unless Cnp.valid?(value)
|
11
12
|
end
|
12
13
|
|
13
14
|
# Algoritm de validare CNP
|
@@ -5,9 +5,10 @@ module ActiveModel
|
|
5
5
|
def validate_each(record, attribute, value)
|
6
6
|
allow_blank = options.fetch(:allow_blank, false)
|
7
7
|
allow_nil = options.fetch(:allow_nil, false)
|
8
|
+
message = options.fetch(:message, nil)
|
8
9
|
record.errors.add_on_empty(attribute) if value.nil? && !allow_nil
|
9
10
|
record.errors.add_on_blank(attribute) if value.blank? && !allow_blank
|
10
|
-
record.errors.add(attribute) unless Iban.valid?(value)
|
11
|
+
record.errors.add(attribute, message) unless Iban.valid?(value)
|
11
12
|
end
|
12
13
|
|
13
14
|
# Descrierea algoritmului:
|
data/romanianvalidators.gemspec
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
Gem::Specification.new do |s|
|
3
3
|
s.name = 'romanianvalidators'
|
4
|
-
s.version = '0.1.
|
4
|
+
s.version = '0.1.2'
|
5
5
|
s.authors = ['Mihai Târnovan']
|
6
6
|
s.email = ['mihai.tarnovan@cubus.ro']
|
7
7
|
s.homepage = 'http://github.com/mtarnovan/romanianvalidators'
|
8
|
+
s.license = 'MIT'
|
8
9
|
s.summary = %q{Collection of validations for Cod Numeric Personal (CNP), Cod de identificare fiscală (CIF) and IBAN (only Romanian format, as published by Romanian National Bank)}
|
9
10
|
s.description = %q{Collection of validations for Cod Numeric Personal (CNP), Cod de identificare fiscală (CIF) and IBAN (only Romanian format, as published by Romanian National Bank).}
|
10
11
|
|
@@ -20,11 +20,24 @@ describe "BICs" do
|
|
20
20
|
subject.valid?.must_equal false
|
21
21
|
subject.errors.size.must_equal 1
|
22
22
|
end
|
23
|
+
|
24
|
+
it "rejects invalid BICs and allows custom error message" do
|
25
|
+
message = "Some custom error message"
|
26
|
+
subject = build_bic_record({:bic => '1'}, {:message => message})
|
27
|
+
subject.valid?.must_equal false
|
28
|
+
subject.errors.size.must_equal 1
|
29
|
+
subject.errors[:bic].must_equal Array.wrap(message)
|
30
|
+
end
|
23
31
|
end
|
24
32
|
|
25
33
|
def build_bic_record(attrs = {}, opts = {})
|
34
|
+
custom_message = opts.fetch(:message, false)
|
26
35
|
TestRecord.reset_callbacks(:validate)
|
27
|
-
|
36
|
+
if custom_message
|
37
|
+
TestRecord.validates :bic, :bic => {:message => custom_message}
|
38
|
+
else
|
39
|
+
TestRecord.validates :bic, :bic => true
|
40
|
+
end
|
28
41
|
TestRecord.new attrs
|
29
42
|
end
|
30
43
|
|
@@ -20,12 +20,26 @@ describe "Cod de identificare fiscală" do
|
|
20
20
|
subject.valid?.must_equal false
|
21
21
|
subject.errors.size.must_equal 1
|
22
22
|
end
|
23
|
+
|
24
|
+
it "rejects invalid CIFs and allows custom error message" do
|
25
|
+
message = "Some custom error message"
|
26
|
+
subject = build_cif_record({:cif => '1'}, {:message => message})
|
27
|
+
subject.valid?.must_equal false
|
28
|
+
subject.errors.size.must_equal 1
|
29
|
+
subject.errors[:cif].must_equal Array.wrap(message)
|
30
|
+
end
|
31
|
+
|
23
32
|
end
|
24
33
|
|
25
34
|
def build_cif_record(attrs = {}, opts = {})
|
35
|
+
custom_message = opts.fetch(:message, false)
|
26
36
|
TestRecord.reset_callbacks(:validate)
|
27
|
-
|
37
|
+
if custom_message
|
38
|
+
TestRecord.validates :cif, :cif => {:message => custom_message}
|
39
|
+
else
|
40
|
+
TestRecord.validates :cif, :cif => true
|
41
|
+
end
|
28
42
|
TestRecord.new attrs
|
29
43
|
end
|
30
44
|
|
31
|
-
end
|
45
|
+
end
|
@@ -20,11 +20,23 @@ describe "Cod numeric personal" do
|
|
20
20
|
subject.valid?.must_equal false
|
21
21
|
subject.errors.size.must_equal 1
|
22
22
|
end
|
23
|
+
it "rejects invalid cnps and allows custom error message" do
|
24
|
+
message = "Some custom error message"
|
25
|
+
subject = build_cnp_record({:cnp => '1'}, {:message => message})
|
26
|
+
subject.valid?.must_equal false
|
27
|
+
subject.errors.size.must_equal 1
|
28
|
+
subject.errors[:cnp].must_equal Array.wrap(message)
|
29
|
+
end
|
23
30
|
end
|
24
31
|
|
25
32
|
def build_cnp_record(attrs = {}, opts = {})
|
33
|
+
custom_message = opts.fetch(:message, false)
|
26
34
|
TestRecord.reset_callbacks(:validate)
|
27
|
-
|
35
|
+
if custom_message
|
36
|
+
TestRecord.validates :cnp, :cnp => {:message => custom_message}
|
37
|
+
else
|
38
|
+
TestRecord.validates :cnp, :cnp => true
|
39
|
+
end
|
28
40
|
TestRecord.new attrs
|
29
41
|
end
|
30
42
|
|
@@ -20,11 +20,23 @@ describe "Iban (BNR only)" do
|
|
20
20
|
subject.valid?.must_equal false
|
21
21
|
subject.errors.size.must_equal 1
|
22
22
|
end
|
23
|
+
it "rejects invalid ibans and allows custom error message" do
|
24
|
+
message = "Some custom error message"
|
25
|
+
subject = build_iban_record({:iban => '1'}, {:message => message})
|
26
|
+
subject.valid?.must_equal false
|
27
|
+
subject.errors.size.must_equal 1
|
28
|
+
subject.errors[:iban].must_equal Array.wrap(message)
|
29
|
+
end
|
23
30
|
end
|
24
31
|
|
25
32
|
def build_iban_record(attrs = {}, opts = {})
|
33
|
+
custom_message = opts.fetch(:message, false)
|
26
34
|
TestRecord.reset_callbacks(:validate)
|
27
|
-
|
35
|
+
if custom_message
|
36
|
+
TestRecord.validates :iban, :iban => {:message => custom_message}
|
37
|
+
else
|
38
|
+
TestRecord.validates :iban, :iban => true
|
39
|
+
end
|
28
40
|
TestRecord.new attrs
|
29
41
|
end
|
30
42
|
|
metadata
CHANGED
@@ -1,78 +1,69 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: romanianvalidators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Mihai Târnovan
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2013-04-15 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: bundler
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: minitest
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - '>='
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - '>='
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: rake
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- -
|
45
|
+
- - '>='
|
52
46
|
- !ruby/object:Gem::Version
|
53
47
|
version: 0.8.7
|
54
48
|
type: :runtime
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - '>='
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: 0.8.7
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
56
|
name: activemodel
|
64
57
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
58
|
requirements:
|
67
|
-
- -
|
59
|
+
- - '>='
|
68
60
|
- !ruby/object:Gem::Version
|
69
61
|
version: 3.0.0
|
70
62
|
type: :runtime
|
71
63
|
prerelease: false
|
72
64
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
65
|
requirements:
|
75
|
-
- -
|
66
|
+
- - '>='
|
76
67
|
- !ruby/object:Gem::Version
|
77
68
|
version: 3.0.0
|
78
69
|
description: Collection of validations for Cod Numeric Personal (CNP), Cod de identificare
|
@@ -86,6 +77,7 @@ extra_rdoc_files: []
|
|
86
77
|
files:
|
87
78
|
- .gitignore
|
88
79
|
- .travis.yml
|
80
|
+
- CHANGELOG.md
|
89
81
|
- Gemfile
|
90
82
|
- LICENSE
|
91
83
|
- README.md
|
@@ -106,28 +98,28 @@ files:
|
|
106
98
|
- test/validations/cnp_test.rb
|
107
99
|
- test/validations/iban_test.rb
|
108
100
|
homepage: http://github.com/mtarnovan/romanianvalidators
|
109
|
-
licenses:
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
metadata: {}
|
110
104
|
post_install_message:
|
111
105
|
rdoc_options: []
|
112
106
|
require_paths:
|
113
107
|
- lib
|
114
108
|
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
-
none: false
|
116
109
|
requirements:
|
117
|
-
- -
|
110
|
+
- - '>='
|
118
111
|
- !ruby/object:Gem::Version
|
119
112
|
version: '0'
|
120
113
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
|
-
none: false
|
122
114
|
requirements:
|
123
|
-
- -
|
115
|
+
- - '>='
|
124
116
|
- !ruby/object:Gem::Version
|
125
117
|
version: '0'
|
126
118
|
requirements: []
|
127
119
|
rubyforge_project: romanianvalidators
|
128
|
-
rubygems_version:
|
120
|
+
rubygems_version: 2.0.0
|
129
121
|
signing_key:
|
130
|
-
specification_version:
|
122
|
+
specification_version: 4
|
131
123
|
summary: Collection of validations for Cod Numeric Personal (CNP), Cod de identificare
|
132
124
|
fiscală (CIF) and IBAN (only Romanian format, as published by Romanian National
|
133
125
|
Bank)
|