validates_identity 0.4.0 → 0.5.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/Gemfile.lock +1 -1
- data/README.md +37 -15
- data/lib/shoulda/matchers/active_model/require_a_valid_identity_matcher.rb +47 -4
- data/lib/validates_identity/identity.rb +1 -1
- data/lib/validates_identity/shoulda_matchers.rb +30 -6
- data/lib/validates_identity/version.rb +1 -1
- data/lib/validates_identity.rb +40 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0fdb1765f269869de8c648faa317223189f37cb567b50f6a9caf2564f4611b9c
|
4
|
+
data.tar.gz: 28402419c0136ab8b501b47e96bb292201212bb9f91a2c2b7534ff4d84e83bd4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 376739a02e1cd0673babf91821c4cf3c44f4afdb79cb087ec91d1b31ad44fd48aa1eaa075f51399b1f356aa0dbd09784be5e38d9ef915560f5d109524e2a13a2
|
7
|
+
data.tar.gz: f99028dfc1be6a94a2f597cf4fe451c21810b9931c6a311a85482998fa5b4fd87ea3ed1045af705c9132e41bdc16d1a6bdaabaff1e950ae8ac47c760ba201904
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# ValidatesIdentity
|
2
2
|
|
3
3
|
This projects aims to validate several countries person identification documents.
|
4
|
+
It works on top of 2 attributes, so that the identity can be determined by a second value and thus validate the first one accordingly
|
4
5
|
|
5
6
|
## Installation
|
6
7
|
|
@@ -18,6 +19,14 @@ Or install it yourself as:
|
|
18
19
|
|
19
20
|
$ gem install validates_identity
|
20
21
|
|
22
|
+
## Official Plugins
|
23
|
+
|
24
|
+
Just require the plugins you want/need in your Gemfile and you will be good to go
|
25
|
+
|
26
|
+
- [Brazilian CPF](https://github.com/plribeiro3000/validates_identity-br_cpf)
|
27
|
+
- [Brazilian CNPJ](https://github.com/plribeiro3000/validates_identity-br_cnpj)
|
28
|
+
- [Guatemala DPI](https://github.com/plribeiro3000/validates_identity-gt_dpi)
|
29
|
+
|
21
30
|
## Usage
|
22
31
|
|
23
32
|
Just use as any other validator:
|
@@ -29,42 +38,55 @@ class User < ActiveRecord::Base
|
|
29
38
|
end
|
30
39
|
```
|
31
40
|
|
32
|
-
|
33
|
-
|
34
|
-
### Format
|
41
|
+
### Format Option
|
35
42
|
|
36
43
|
The `format` option can be used to format the final identity value.
|
37
44
|
|
38
45
|
```ruby
|
39
46
|
class User < ActiveRecord::Base
|
40
|
-
#
|
47
|
+
# all values will be formatted after successful validation
|
41
48
|
validates :identity, identity: { identity_type: :identity_type, format: true }
|
42
49
|
end
|
43
50
|
```
|
44
51
|
|
45
|
-
###
|
52
|
+
### Only Option
|
46
53
|
|
47
|
-
|
54
|
+
The `only` option can be used to narrow down the validators
|
48
55
|
|
49
56
|
```ruby
|
50
|
-
|
57
|
+
class User < ActiveRecord::Base
|
58
|
+
# will accept only person identity types
|
59
|
+
validates :identity, identity: { identity_type: :identity_type, only: :person }
|
60
|
+
# will accept only legal identity types
|
61
|
+
validates :identity, identity: { identity_type: :identity_type, only: :legal }
|
62
|
+
end
|
51
63
|
```
|
52
64
|
|
53
|
-
|
65
|
+
### Aliases
|
54
66
|
|
55
|
-
|
56
|
-
- a `valid?` method that returns a boolean
|
57
|
-
- a `formatted` method that returns the value formatted
|
67
|
+
In case of a legacy system where keys were already defined and differ from the official ones, aliases can be registered to easy the transition
|
58
68
|
|
59
|
-
|
69
|
+
```ruby
|
70
|
+
ValidatesIdentity.register_person_identity_type_alias('LegacyIdentity', 'CustomIdentity')
|
71
|
+
ValidatesIdentity.register_legal_identity_type_alias('LegacyIdentity', 'CustomIdentity')
|
72
|
+
```
|
60
73
|
|
61
|
-
|
74
|
+
### Adding your own Validators
|
75
|
+
|
76
|
+
New Identity Validators can be registered through the public apis of `ValidatesIdentity`
|
62
77
|
|
63
78
|
```ruby
|
64
|
-
ValidatesIdentity.
|
79
|
+
ValidatesIdentity.register_person_identity_type('CustomIdentity', CustomIdentityValidator)
|
80
|
+
ValidatesIdentity.register_legal_identity_type('CustomIdentity', CustomIdentityValidator)
|
65
81
|
```
|
66
82
|
|
67
|
-
|
83
|
+
A Validator must implement:
|
84
|
+
|
85
|
+
- a constructor with 1 param: `value`
|
86
|
+
- a `valid?` method that returns a boolean
|
87
|
+
- a `formatted` method that returns the formatted value
|
88
|
+
|
89
|
+
### Adding scenarios to the matcher
|
68
90
|
|
69
91
|
When adding a new validator, cases of success and error can be added through and API so that the `Matcher` will test against them
|
70
92
|
|
@@ -13,14 +13,23 @@ module Shoulda
|
|
13
13
|
def initialize(identity, identity_type)
|
14
14
|
super(identity)
|
15
15
|
@identity_type = identity_type
|
16
|
+
@type = :both
|
16
17
|
end
|
17
18
|
|
18
19
|
def description
|
19
|
-
|
20
|
+
case @type
|
21
|
+
when :person then 'require a valid person identity'
|
22
|
+
when :legal then 'require a valid legal identity'
|
23
|
+
else 'require a valid identity'
|
24
|
+
end
|
20
25
|
end
|
21
26
|
|
22
27
|
def failure_message
|
23
|
-
|
28
|
+
case @type
|
29
|
+
when :person then 'does not require a valid person identity'
|
30
|
+
when :legal then 'does not require a valid legal identity'
|
31
|
+
else 'expected to require a valid identity'
|
32
|
+
end
|
24
33
|
end
|
25
34
|
|
26
35
|
def matches?(subject)
|
@@ -28,7 +37,7 @@ module Shoulda
|
|
28
37
|
|
29
38
|
result = []
|
30
39
|
|
31
|
-
|
40
|
+
allowed_values.each do |identity_type, values|
|
32
41
|
subject.send("#{@identity_type}=", identity_type)
|
33
42
|
|
34
43
|
values.each do |value|
|
@@ -36,7 +45,7 @@ module Shoulda
|
|
36
45
|
end
|
37
46
|
end
|
38
47
|
|
39
|
-
|
48
|
+
disallowed_values.each do |identity_type, values|
|
40
49
|
subject.send("#{@identity_type}=", identity_type)
|
41
50
|
|
42
51
|
values.each do |value|
|
@@ -46,6 +55,40 @@ module Shoulda
|
|
46
55
|
|
47
56
|
result.inject(:&)
|
48
57
|
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def allowed_values
|
62
|
+
case @type
|
63
|
+
when :person then person_allowed_values
|
64
|
+
when :legal then legal_allowed_values
|
65
|
+
else ValidatesIdentity::ShouldaMatchers.allowed_values
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def disallowed_values
|
70
|
+
case @type
|
71
|
+
when :person then person_disallowed_values + legal_allowed_values
|
72
|
+
when :legal then legal_disallowed_values + person_allowed_values
|
73
|
+
else ValidatesIdentity::ShouldaMatchers.disallowed_values
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def person_allowed_values
|
78
|
+
ValidatesIdentity::ShouldaMatchers.person_allowed_values
|
79
|
+
end
|
80
|
+
|
81
|
+
def person_disallowed_values
|
82
|
+
ValidatesIdentity::ShouldaMatchers.person_disallowed_values
|
83
|
+
end
|
84
|
+
|
85
|
+
def legal_allowed_values
|
86
|
+
ValidatesIdentity::ShouldaMatchers.legal_allowed_values
|
87
|
+
end
|
88
|
+
|
89
|
+
def legal_disallowed_values
|
90
|
+
ValidatesIdentity::ShouldaMatchers.legal_disallowed_values
|
91
|
+
end
|
49
92
|
end
|
50
93
|
end
|
51
94
|
end
|
@@ -15,7 +15,7 @@ class ValidatesIdentity
|
|
15
15
|
return true if value.blank?
|
16
16
|
return false if options[:identity_type].blank?
|
17
17
|
|
18
|
-
validator_class = ValidatesIdentity.get_validator(identity_type)
|
18
|
+
validator_class = ValidatesIdentity.get_validator(identity_type, type: options[:only])
|
19
19
|
|
20
20
|
return false if validator_class.nil?
|
21
21
|
|
@@ -3,20 +3,44 @@
|
|
3
3
|
class ValidatesIdentity
|
4
4
|
class ShouldaMatchers
|
5
5
|
class << self
|
6
|
+
def person_allowed_values
|
7
|
+
@person_allowed_values ||= {}
|
8
|
+
end
|
9
|
+
|
10
|
+
def legal_allowed_values
|
11
|
+
@legal_allowed_values ||= {}
|
12
|
+
end
|
13
|
+
|
14
|
+
def person_disallowed_values
|
15
|
+
@person_disallowed_values ||= {}
|
16
|
+
end
|
17
|
+
|
18
|
+
def legal_disallowed_values
|
19
|
+
@legal_disallowed_values ||= {}
|
20
|
+
end
|
21
|
+
|
6
22
|
def allowed_values
|
7
|
-
|
23
|
+
person_allowed_values.merge(legal_allowed_values)
|
8
24
|
end
|
9
25
|
|
10
26
|
def disallowed_values
|
11
|
-
|
27
|
+
person_disallowed_values.merge(legal_disallowed_values)
|
28
|
+
end
|
29
|
+
|
30
|
+
def register_person_allowed_values(identity_type_acronym, values = [])
|
31
|
+
person_allowed_values[identity_type_acronym] = values
|
32
|
+
end
|
33
|
+
|
34
|
+
def register_legal_allowed_values(identity_type_acronym, values = [])
|
35
|
+
legal_allowed_values[identity_type_acronym] = values
|
12
36
|
end
|
13
37
|
|
14
|
-
def
|
15
|
-
|
38
|
+
def register_person_disallowed_values(identity_type_acronym, values = [])
|
39
|
+
person_disallowed_values[identity_type_acronym] = values
|
16
40
|
end
|
17
41
|
|
18
|
-
def
|
19
|
-
|
42
|
+
def register_legal_disallowed_values(identity_type_acronym, values = [])
|
43
|
+
legal_disallowed_values[identity_type_acronym] = values
|
20
44
|
end
|
21
45
|
end
|
22
46
|
end
|
data/lib/validates_identity.rb
CHANGED
@@ -10,26 +10,57 @@ class ValidatesIdentity
|
|
10
10
|
class << self
|
11
11
|
private
|
12
12
|
|
13
|
+
def person_identity_types
|
14
|
+
@person_identity_types ||= {}
|
15
|
+
end
|
16
|
+
|
17
|
+
def legal_identity_types
|
18
|
+
@legal_identity_types ||= {}
|
19
|
+
end
|
20
|
+
|
13
21
|
def identity_types
|
14
|
-
|
22
|
+
person_identity_types.merge(legal_identity_types)
|
23
|
+
end
|
24
|
+
|
25
|
+
def person_identity_type_aliases
|
26
|
+
@person_identity_type_aliases ||= {}
|
27
|
+
end
|
28
|
+
|
29
|
+
def legal_identity_type_aliases
|
30
|
+
@legal_identity_type_aliases ||= {}
|
15
31
|
end
|
16
32
|
|
17
33
|
def identity_type_aliases
|
18
|
-
|
34
|
+
person_identity_type_aliases.merge(legal_identity_type_aliases)
|
19
35
|
end
|
20
36
|
end
|
21
37
|
|
22
|
-
def self.
|
23
|
-
|
24
|
-
|
38
|
+
def self.register_person_identity_type(identity_type_acronym, identity_type_validator)
|
39
|
+
person_identity_types[identity_type_acronym] = identity_type_validator
|
40
|
+
register_person_identity_type_alias(identity_type_acronym, identity_type_acronym)
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.register_legal_identity_type(identity_type_acronym, identity_type_validator)
|
44
|
+
legal_identity_types[identity_type_acronym] = identity_type_validator
|
45
|
+
register_legal_identity_type_alias(identity_type_acronym, identity_type_acronym)
|
25
46
|
end
|
26
47
|
|
27
|
-
def self.
|
28
|
-
|
48
|
+
def self.register_person_identity_type_alias(identity_type, identity_type_alias)
|
49
|
+
person_identity_type_aliases[identity_type_alias] = identity_type
|
29
50
|
end
|
30
51
|
|
31
|
-
def self.
|
32
|
-
|
52
|
+
def self.register_legal_identity_type_alias(identity_type, identity_type_alias)
|
53
|
+
legal_identity_type_aliases[identity_type_alias] = identity_type
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.get_validator(identity_type, type: :both)
|
57
|
+
identity_alias =
|
58
|
+
case type
|
59
|
+
when :person then person_identity_type_aliases[identity_type]
|
60
|
+
when :legal then legal_identity_type_aliases[identity_type]
|
61
|
+
else identity_type_aliases[identity_type]
|
62
|
+
end
|
63
|
+
|
33
64
|
identity_types[identity_alias]
|
34
65
|
end
|
35
66
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_identity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paulo Ribeiro
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-02-
|
11
|
+
date: 2024-02-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|