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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 051a300329482a3a9e2fb6fd94b0b1524dfa4414
4
- data.tar.gz: 1ba098b3f8491e39685344c6b6d3388974cb432b
3
+ metadata.gz: d7b5954212b6f7dbef5a5d28551e572fb2b358f2
4
+ data.tar.gz: be6bda44967fe6e4744a71c4a1b99c46e48d0c5f
5
5
  SHA512:
6
- metadata.gz: 010b15497c14c7941109b8f03659736773d61a57096e586a80e118538ac918dee792f1f0e4011afffed89ba1b2926fe61302d7fc388e7cf7705ab21b75390b7d
7
- data.tar.gz: 1eb3588d68494cd4a19fa38cac85cddc67e596beff3e76ea5a768b41632253fb3fd79e3b215308dcf1ac1842ae1f3201a2ef4b5ae222a214ea29ad581aff17bc
6
+ metadata.gz: 5ee5ab84a8ff686c3e89d001a96b9926cad5c802f4286f22bdc8b8bbab9944b7393a0a7e974b210cb765a02f5c0834726294db621322f99546ea01b5249f0196
7
+ data.tar.gz: 4c032675fec6f86507cb9c9eff376d0c583318a5a62b17c4d537f0aa0bb58b9936f8e13c26910870800575493fec8ccc3e3808ca7c332bdd850fdf735aa36eb9
@@ -5,6 +5,9 @@ AllCops:
5
5
  StyleGuideCopsOnly: true
6
6
  TargetRubyVersion: 2.3
7
7
 
8
+ Metrics/ParameterLists:
9
+ Max: 6
10
+
8
11
  Style/Alias:
9
12
  Enabled: false
10
13
 
@@ -1,3 +1,10 @@
1
+ # v0.0.3 (2017-01-30)
2
+
3
+ ## Bug fixes
4
+ - Error messages from validity and contract validators (nepalez)
5
+
6
+ [Compare v0.0.2...v0.0.3](https://github.com/tram-rb/tram-validators/compare/v0.0.2...v0.0.3)
7
+
1
8
  # v0.0.2 (2017-01-27)
2
9
 
3
10
  ## New Features
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
- class PolicyObject < SimpleDelegator
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
- # collects original error messages from policy under the key `foo`
36
- validates :foo, contract: { policy: PolicyObject }
38
+ # adds message with i18 translation `foo.contract_specific_policy`
39
+ validates :foo, contract: { policy: SpecificPolicy }
37
40
 
38
- # collects the same messages from policy under their original keys (`bar`)
41
+ # collects messages from policy under their original keys (`bar`)
39
42
  validates :foo, contract: { policy: PolicyObject, original_keys: true }
40
43
 
41
- # collects the same messages from policy under nested keys (`foo[bar]`)
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
- # collects original error messages under the key `foo`
58
+ # adds message with i18 translation `foo.valid`
53
59
  validates :foo, validity: true
54
60
 
55
- # collects the same messages from policy under their original keys (`bar`)
61
+ # collects messages from invalid value under their original keys (`bar`)
56
62
  validates :foo, validity: { original_keys: true }
57
63
 
58
- # collects the same messages from policy under nested keys (`foo[bar]`)
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 the key `list[i]` (i for index of invalid item)
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
 
@@ -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
- # Provides standard key for error collected by standalone validators
23
- def error_key(source, target, nested_keys: nil, original_keys: nil, **)
24
- return source if original_keys
25
- return target unless nested_keys
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
- return unless options[:policy]
9
- options[:policy].new(value).tap(&:valid?).errors.messages.each do |key, msg|
10
- error_key = Tram::Validators.error_key(key, attribute, options)
11
- msg.each { |message| record.errors.add error_key, message }
12
- end
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, attribute, value)
16
- if !value.respond_to? :invalid?
17
- record.errors.add attribute, :invalidable, record: record,
18
- attribute: attribute,
19
- value: value
20
- elsif value.invalid?
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "tram-validators"
3
- gem.version = "0.0.2"
3
+ gem.version = "0.0.3"
4
4
  gem.author = "Andrew Kozin (nepalez)"
5
5
  gem.email = "andrew.kozin@gmail.com"
6
6
  gem.homepage = "https://github.com/tram-rb/tram-validators"
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.2
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-27 00:00:00.000000000 Z
11
+ date: 2017-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails