tram-validators 0.0.2 → 0.0.3
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 +7 -0
- data/README.md +15 -9
- data/lib/tram/validators.rb +19 -4
- data/lib/tram/validators/contract_validator.rb +8 -5
- data/lib/tram/validators/validity_validator.rb +6 -10
- data/tram-validators.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d7b5954212b6f7dbef5a5d28551e572fb2b358f2
|
4
|
+
data.tar.gz: be6bda44967fe6e4744a71c4a1b99c46e48d0c5f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ee5ab84a8ff686c3e89d001a96b9926cad5c802f4286f22bdc8b8bbab9944b7393a0a7e974b210cb765a02f5c0834726294db621322f99546ea01b5249f0196
|
7
|
+
data.tar.gz: 4c032675fec6f86507cb9c9eff376d0c583318a5a62b17c4d537f0aa0bb58b9936f8e13c26910870800575493fec8ccc3e3808ca7c332bdd850fdf735aa36eb9
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -26,22 +26,28 @@ Checks that a value satisfies a contract, represented by a standalone validator
|
|
26
26
|
It applies policy validator, and collects its messages under corresponding keys.
|
27
27
|
|
28
28
|
```ruby
|
29
|
-
|
29
|
+
require "tram-validators" # defines `validity` validator
|
30
|
+
|
31
|
+
class SpecificPolicy < SimpleDelegator
|
30
32
|
include ActiveModel::Validations
|
31
33
|
validates :bar, presence: true
|
34
|
+
validates :itself, validity: true # validates wrapped object per se
|
32
35
|
end
|
33
36
|
|
34
37
|
# PolicyObject.new(record.foo).valid? == true
|
35
|
-
#
|
36
|
-
validates :foo, contract: { policy:
|
38
|
+
# adds message with i18 translation `foo.contract_specific_policy`
|
39
|
+
validates :foo, contract: { policy: SpecificPolicy }
|
37
40
|
|
38
|
-
# collects
|
41
|
+
# collects messages from policy under their original keys (`bar`)
|
39
42
|
validates :foo, contract: { policy: PolicyObject, original_keys: true }
|
40
43
|
|
41
|
-
# collects
|
44
|
+
# collects messages from policy under nested keys (`foo[bar]`)
|
42
45
|
validates :foo, contract: { policy: PolicyObject, nested_keys: true }
|
43
46
|
```
|
44
47
|
|
48
|
+
When you use `:nested_keys`, the keys `:base` and `:itself` will be excluded from chain of nesting.
|
49
|
+
That's why when `PolicyObject` is invalid at `:itself`, the last definition will collect error under the key `foo`, not the `foo[itself]`.
|
50
|
+
|
45
51
|
### Validity Validator
|
46
52
|
|
47
53
|
Checks that an attribute is valid per se.
|
@@ -49,13 +55,13 @@ It collects original error messages under corresponding keys.
|
|
49
55
|
|
50
56
|
```ruby
|
51
57
|
# record.foo.valid? == true
|
52
|
-
#
|
58
|
+
# adds message with i18 translation `foo.valid`
|
53
59
|
validates :foo, validity: true
|
54
60
|
|
55
|
-
# collects
|
61
|
+
# collects messages from invalid value under their original keys (`bar`)
|
56
62
|
validates :foo, validity: { original_keys: true }
|
57
63
|
|
58
|
-
# collects
|
64
|
+
# collects messages from invalid value under nested keys (`foo[bar]`)
|
59
65
|
validates :foo, validity: { nested_keys: true }
|
60
66
|
```
|
61
67
|
|
@@ -65,7 +71,7 @@ Applies validation rule to every element of the collection (that responds to `to
|
|
65
71
|
|
66
72
|
```ruby
|
67
73
|
# Checks that every element of record.list is present
|
68
|
-
# collects original errors under
|
74
|
+
# collects original errors under keys `list[i]` (i for index of invalid item)
|
69
75
|
validates :list, each: { presence: true }
|
70
76
|
```
|
71
77
|
|
data/lib/tram/validators.rb
CHANGED
@@ -19,13 +19,28 @@ module Tram
|
|
19
19
|
chain.to_s.split(".").inject(record) { |obj, name| obj&.send(name) }
|
20
20
|
end
|
21
21
|
|
22
|
-
#
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
# Copies errors from source to target
|
23
|
+
# if either nested or original keys selected
|
24
|
+
def copy_errors(source, target, name, key, value, **opts)
|
25
|
+
nested = opts[:nested_keys]
|
26
|
+
original = opts[:original_keys]
|
27
|
+
|
28
|
+
if !nested && !original
|
29
|
+
target.errors.add(name, key, record: target, value: value)
|
30
|
+
else
|
31
|
+
source.errors.messages.each do |k, texts|
|
32
|
+
target_key = nested ? nested_key(name, k) : k
|
33
|
+
texts.each { |text| target.errors.add(target_key, text) }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Builds nested key
|
39
|
+
def nested_key(target, source)
|
26
40
|
source.to_s
|
27
41
|
.split(/\[|\]/)
|
28
42
|
.compact
|
43
|
+
.reject { |key| %w(base itself).include? key.to_s }
|
29
44
|
.inject(target) { |obj, key| "#{obj}[#{key}]" }
|
30
45
|
.to_sym
|
31
46
|
end
|
@@ -5,10 +5,13 @@
|
|
5
5
|
#
|
6
6
|
class ContractValidator < ActiveModel::EachValidator
|
7
7
|
def validate_each(record, attribute, value)
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
8
|
+
policy = options[:policy]
|
9
|
+
return unless policy
|
10
|
+
|
11
|
+
source = policy.new(value)
|
12
|
+
return if source.valid?
|
13
|
+
|
14
|
+
key = "contract_#{policy.name.underscore}"
|
15
|
+
Tram::Validators.copy_errors(source, record, attribute, key, value, options)
|
13
16
|
end
|
14
17
|
end
|
@@ -12,16 +12,12 @@
|
|
12
12
|
# validates :user, validity: { nested_keys: true }
|
13
13
|
#
|
14
14
|
class ValidityValidator < ActiveModel::EachValidator
|
15
|
-
def validate_each(record,
|
16
|
-
if !value.respond_to? :
|
17
|
-
record.errors
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
value.errors.messages.each do |key, messages|
|
22
|
-
error_key = Tram::Validators.error_key(key, attribute, options)
|
23
|
-
messages.each { |message| record.errors.add error_key, message }
|
24
|
-
end
|
15
|
+
def validate_each(record, key, value)
|
16
|
+
if !value.respond_to? :valid?
|
17
|
+
record.errors
|
18
|
+
.add attribute, :valid, record: record, attribute: key, value: value
|
19
|
+
elsif !value.valid?
|
20
|
+
Tram::Validators.copy_errors(value, record, key, :valid, value, options)
|
25
21
|
end
|
26
22
|
end
|
27
23
|
end
|
data/tram-validators.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tram-validators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kozin (nepalez)
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|