validates_identity 0.2.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/CHANGELOG.md +11 -1
- data/Gemfile.lock +1 -1
- data/README.md +17 -1
- data/lib/identity_validator.rb +11 -9
- data/lib/validates_identity/identity.rb +5 -4
- data/lib/validates_identity/version.rb +1 -1
- data/lib/validates_identity.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f932507ebec9a185edee94cebdf2f6ca7ff6b8142ade9243d197654fd36201d2
|
4
|
+
data.tar.gz: b484245e99d1429b632d46b729deb0683042da47ec87b59010d6f6970e952a89
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c9945d350a984600a079149e126fbf6a234110f5d1faba2415fd87d92dcb1d52704d018442e149047e2285bcfbaee15a15a07e32dafa055c99e13f4948cd45e4
|
7
|
+
data.tar.gz: 2c1cab2c85a42f3d0494838624b3d794a3730a70dd937ef33c1bd2c1b23955c12b06837a9ed36c55b498730a6fce01148b8c738333da621f0d6c12c3dd497d2d
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -31,6 +31,19 @@ end
|
|
31
31
|
|
32
32
|
## Advanced Usage
|
33
33
|
|
34
|
+
### Format
|
35
|
+
|
36
|
+
The `format` option can be used to format the final identity value.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
class User < ActiveRecord::Base
|
40
|
+
# :identity_type is the attribute that will be used to determine the identity type and is required
|
41
|
+
validates :identity, identity: { identity_type: :identity_type, format: true }
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
### Custom Validators
|
46
|
+
|
34
47
|
New Identity Validators can be registered through the public apis of `ValidatesIdentity`
|
35
48
|
|
36
49
|
```ruby
|
@@ -39,8 +52,11 @@ ValidatesIdentity.register_identity_type('CustomIdentity', CustomIdentityValidat
|
|
39
52
|
|
40
53
|
Each Validator should have:
|
41
54
|
|
42
|
-
- a constructor with
|
55
|
+
- a constructor with 1 param: `value`
|
43
56
|
- a `valid?` method that returns a boolean
|
57
|
+
- a `formatted` method that returns the value formatted
|
58
|
+
|
59
|
+
### Validators Aliases
|
44
60
|
|
45
61
|
In case of a legacy system where keys were already defined and differ from the official ones, aliases can be registered as well
|
46
62
|
|
data/lib/identity_validator.rb
CHANGED
@@ -2,17 +2,19 @@
|
|
2
2
|
|
3
3
|
class IdentityValidator < ActiveModel::EachValidator
|
4
4
|
def validate_each(record, attribute, value)
|
5
|
-
identity = ValidatesIdentity::Identity.new(record, value, options)
|
5
|
+
identity = ValidatesIdentity::Identity.new(record, attribute, value, options)
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
ruby_prior_version_three =
|
10
|
-
Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.0.0')
|
11
|
-
|
12
|
-
if ruby_prior_version_three
|
13
|
-
record.errors.add(attribute, :invalid, options)
|
7
|
+
if identity.valid?
|
8
|
+
record.send("#{attribute}=", identity.formatted) if options[:format]
|
14
9
|
else
|
15
|
-
|
10
|
+
ruby_prior_version_three =
|
11
|
+
Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.0.0')
|
12
|
+
|
13
|
+
if ruby_prior_version_three
|
14
|
+
record.errors.add(attribute, :invalid, options)
|
15
|
+
else
|
16
|
+
record.errors.add(attribute, :invalid, **options)
|
17
|
+
end
|
16
18
|
end
|
17
19
|
end
|
18
20
|
end
|
@@ -1,11 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
class ValidatesIdentity
|
4
4
|
class Identity
|
5
|
-
attr_reader :record, :value, :options
|
5
|
+
attr_reader :record, :attribute, :value, :options
|
6
6
|
|
7
|
-
def initialize(record, value, options)
|
7
|
+
def initialize(record, attribute, value, options)
|
8
8
|
@record = record
|
9
|
+
@attribute = attribute
|
9
10
|
@value = value
|
10
11
|
@options = options
|
11
12
|
end
|
@@ -18,7 +19,7 @@ module ValidatesIdentity
|
|
18
19
|
|
19
20
|
return false if validator_class.nil?
|
20
21
|
|
21
|
-
validator = validator_class.new(value
|
22
|
+
validator = validator_class.new(value)
|
22
23
|
validator.valid?
|
23
24
|
end
|
24
25
|
|
data/lib/validates_identity.rb
CHANGED