validates_identity 0.3.2 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 314621c0cd80707a6ee1f7a776307f767e167cab1e63e63c4cbe0eeeb404d92a
4
- data.tar.gz: 3e7c4f2a9406ec4ade871fe38599451bb18440466c2f17829d440cff6f282cbd
3
+ metadata.gz: 0fdb1765f269869de8c648faa317223189f37cb567b50f6a9caf2564f4611b9c
4
+ data.tar.gz: 28402419c0136ab8b501b47e96bb292201212bb9f91a2c2b7534ff4d84e83bd4
5
5
  SHA512:
6
- metadata.gz: f0c7bf51aaaeb802d817ad33d30881d2f8fcc46091915f0bba6938692d0356342fbd92755378713572dd6eb8dcf9439d60bb1eeedcfbb118e3d601df0194433c
7
- data.tar.gz: aa22f5dd5e57e3df142c183de1f993450d7fb6e338ad46325b6c8b12d733f080561a6ffd3c8d6fd9435f0029cbd02541aabd1013e0bf9f8c3fa90bf29efdcc09
6
+ metadata.gz: 376739a02e1cd0673babf91821c4cf3c44f4afdb79cb087ec91d1b31ad44fd48aa1eaa075f51399b1f356aa0dbd09784be5e38d9ef915560f5d109524e2a13a2
7
+ data.tar.gz: f99028dfc1be6a94a2f597cf4fe451c21810b9931c6a311a85482998fa5b4fd87ea3ed1045af705c9132e41bdc16d1a6bdaabaff1e950ae8ac47c760ba201904
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## [0.5.0] - 2024-02-27
2
+
3
+ ### Added
4
+
5
+ - `:only` option to restrict the validation to a specific entity (person or legal)
6
+
7
+ ## [0.4.0] - 2024-02-25
8
+
9
+ ### Added
10
+
11
+ - Shoulda Custom Matcher
12
+
1
13
  ## [0.3.2] - 2024-02-25
2
14
 
3
15
  ### Fixed
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- validates_identity (0.3.2)
4
+ validates_identity (0.5.0)
5
5
  activemodel
6
6
 
7
7
  GEM
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,39 +38,76 @@ class User < ActiveRecord::Base
29
38
  end
30
39
  ```
31
40
 
32
- ## Advanced Usage
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
- # :identity_type is the attribute that will be used to determine the identity type and is required
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
- ### Custom Validators
52
+ ### Only Option
53
+
54
+ The `only` option can be used to narrow down the validators
55
+
56
+ ```ruby
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
63
+ ```
64
+
65
+ ### Aliases
66
+
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
68
+
69
+ ```ruby
70
+ ValidatesIdentity.register_person_identity_type_alias('LegacyIdentity', 'CustomIdentity')
71
+ ValidatesIdentity.register_legal_identity_type_alias('LegacyIdentity', 'CustomIdentity')
72
+ ```
73
+
74
+ ### Adding your own Validators
46
75
 
47
76
  New Identity Validators can be registered through the public apis of `ValidatesIdentity`
48
77
 
49
78
  ```ruby
50
- ValidatesIdentity.register_identity_type('CustomIdentity', CustomIdentityValidator)
79
+ ValidatesIdentity.register_person_identity_type('CustomIdentity', CustomIdentityValidator)
80
+ ValidatesIdentity.register_legal_identity_type('CustomIdentity', CustomIdentityValidator)
51
81
  ```
52
82
 
53
- Each Validator should have:
83
+ A Validator must implement:
54
84
 
55
85
  - a constructor with 1 param: `value`
56
86
  - a `valid?` method that returns a boolean
57
- - a `formatted` method that returns the value formatted
87
+ - a `formatted` method that returns the formatted value
58
88
 
59
- ### Validators Aliases
89
+ ### Adding scenarios to the matcher
90
+
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
92
+
93
+ ```ruby
94
+ ValidatesIdentity::ShouldaMatchers.register_allowed_values('CustomIdentity', ['123456789', '123.456.789'])
95
+ ValidatesIdentity::ShouldaMatchers.register_disallowed_values('CustomIdentity', ['12345679', '12.456.789'])
96
+ ```
97
+
98
+ ## Testing
99
+
100
+ Require matcher in your `spec_helper` or `rails_helper` file:
101
+
102
+ ```ruby
103
+ require 'shoulda/matchers/active_model/require_a_valid_identity_matcher'
104
+ ```
60
105
 
61
- In case of a legacy system where keys were already defined and differ from the official ones, aliases can be registered as well
106
+ Use in your tests:
62
107
 
63
108
  ```ruby
64
- ValidatesIdentity.register_identity_type_alias('LegacyIdentity', 'CustomIdentity')
109
+ it { is_expected.to require_a_valid_identity } # It will test the attributes :identity and :identity_type by default
110
+ it { is_expected.to require_a_valid_identity(:id, :my_type) }
65
111
  ```
66
112
 
67
113
  ## Development
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'shoulda-matchers'
4
+
5
+ module Shoulda
6
+ module Matchers
7
+ module ActiveModel
8
+ def require_a_valid_identity(identity = :identity, identity_type = :identity_type)
9
+ RequireAValidIdentityMatcher.new(identity, identity_type)
10
+ end
11
+
12
+ class RequireAValidIdentityMatcher < ValidationMatcher
13
+ def initialize(identity, identity_type)
14
+ super(identity)
15
+ @identity_type = identity_type
16
+ @type = :both
17
+ end
18
+
19
+ def description
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
25
+ end
26
+
27
+ def failure_message
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
33
+ end
34
+
35
+ def matches?(subject)
36
+ super(subject)
37
+
38
+ result = []
39
+
40
+ allowed_values.each do |identity_type, values|
41
+ subject.send("#{@identity_type}=", identity_type)
42
+
43
+ values.each do |value|
44
+ result << allows_value_of(value)
45
+ end
46
+ end
47
+
48
+ disallowed_values.each do |identity_type, values|
49
+ subject.send("#{@identity_type}=", identity_type)
50
+
51
+ values.each do |value|
52
+ result << disallows_value_of(value)
53
+ end
54
+ end
55
+
56
+ result.inject(:&)
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
92
+ end
93
+ end
94
+ end
95
+ 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
 
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ValidatesIdentity
4
+ class ShouldaMatchers
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
+
22
+ def allowed_values
23
+ person_allowed_values.merge(legal_allowed_values)
24
+ end
25
+
26
+ def disallowed_values
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
36
+ end
37
+
38
+ def register_person_disallowed_values(identity_type_acronym, values = [])
39
+ person_disallowed_values[identity_type_acronym] = values
40
+ end
41
+
42
+ def register_legal_disallowed_values(identity_type_acronym, values = [])
43
+ legal_disallowed_values[identity_type_acronym] = values
44
+ end
45
+ end
46
+ end
47
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module ValidatesIdentity
4
- VERSION = '0.3.2'
3
+ class ValidatesIdentity
4
+ VERSION = '0.5.0'
5
5
  end
@@ -5,30 +5,62 @@ require 'identity_validator'
5
5
 
6
6
  class ValidatesIdentity
7
7
  autoload :Identity, 'validates_identity/identity'
8
+ autoload :ShouldaMatchers, 'validates_identity/shoulda_matchers'
8
9
 
9
10
  class << self
10
11
  private
11
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
+
12
21
  def identity_types
13
- @identity_types ||= {}
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 ||= {}
14
31
  end
15
32
 
16
33
  def identity_type_aliases
17
- @identity_type_aliases ||= {}
34
+ person_identity_type_aliases.merge(legal_identity_type_aliases)
18
35
  end
19
36
  end
20
37
 
21
- def self.register_identity_type(identity_type_acronym, identity_type_validator)
22
- identity_types[identity_type_acronym] = identity_type_validator
23
- identity_type_aliases[identity_type_acronym] = identity_type_acronym
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)
24
46
  end
25
47
 
26
- def self.register_identity_type_alias(identity_type, identity_type_alias)
27
- identity_type_aliases[identity_type_alias] = identity_type
48
+ def self.register_person_identity_type_alias(identity_type, identity_type_alias)
49
+ person_identity_type_aliases[identity_type_alias] = identity_type
28
50
  end
29
51
 
30
- def self.get_validator(identity_type)
31
- identity_alias = identity_type_aliases[identity_type]
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
+
32
64
  identity_types[identity_alias]
33
65
  end
34
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.3.2
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-25 00:00:00.000000000 Z
11
+ date: 2024-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -43,8 +43,10 @@ files:
43
43
  - README.md
44
44
  - Rakefile
45
45
  - lib/identity_validator.rb
46
+ - lib/shoulda/matchers/active_model/require_a_valid_identity_matcher.rb
46
47
  - lib/validates_identity.rb
47
48
  - lib/validates_identity/identity.rb
49
+ - lib/validates_identity/shoulda_matchers.rb
48
50
  - lib/validates_identity/version.rb
49
51
  - validates_identity.gemspec
50
52
  homepage: https://github.com/plribeiro3000/validates_identity