error_normalizer 0.1.0 → 0.1.1

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
  SHA256:
3
- metadata.gz: 3a2b14320f32c8b5b9709d2a7963941ff27cebe857533a3a38401e4ccb08709a
4
- data.tar.gz: 5d9f38f16ea5aac77e3a0318eb4cacac7b9aab5d24574e8d55c5c3de4b3cb08f
3
+ metadata.gz: 4035feeceb8c759e702989ff2d013b40a935fcf4b4ca2afe79b9742738ff39a6
4
+ data.tar.gz: f7235ca637c3503f126ff091724d80d672462b08b6b7ade550bba1ad4ab801ef
5
5
  SHA512:
6
- metadata.gz: ea13a01344de75bd4161ff70a70f268aaa2d6b393273f14e000da2b8213ca61eae426d12e97acdbe06eaf9f2dbbe4fa3d4427214e47ad46558de8422105dd058
7
- data.tar.gz: 58fd33623c68e76af2bf94fbd7ec1cd8dea6a17baae99315bfddc687862c00e6f9dbdf38d3ebd6f83f69cb06b6c5232986548bac98c9e4ddb15baf4e4e118136
6
+ metadata.gz: 252e4a3d03e3c1a1f190c77774fa0ca2ac23adb6ca5bd02afc93a9e7cc4fbe819c63fee1c98c6fee7d9d0a4a485032773a2b56f935b16b8c7d958a17bb9cc7da
7
+ data.tar.gz: e50077ea3edd89c7c091aa92138f54fd49bc41ac1f0670fbed866c829e63f7094be1b646613f058ff6ea1775713564af7fec106f0c84b85f4a2083dac6dd0e4e
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- error_normalizer (0.1.0)
4
+ error_normalizer (0.1.1)
5
5
  dry-configurable (~> 0.7.0)
6
6
 
7
7
  GEM
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.email = ['di.kondratenko@gmail.com']
10
10
 
11
11
  spec.summary = 'Normalize dry-validation and ActiveModel errors to the universal format'
12
- spec.homepage = 'https://gitlab.yalantis.com/web/error_normalizer'
12
+ spec.homepage = 'https://gitlab.yalantis.com/public-repo/error_normalizer/'
13
13
  spec.license = 'MIT'
14
14
 
15
15
  # Specify which files should be added to the gem when it is released.
@@ -1,9 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ErrorNormalizer
4
+ #
4
5
  # Error struct which makes cosmetic normalization
5
- # upon calling either #to_hash and #to_json.
6
- # Supports case equality check (#===) for hash structs.
6
+ # upon calling either {Error#to_hash} or {Error#to_json}.
7
+ # Provides case equality check {Error.===} to support plain Hash structs.
7
8
  #
8
9
  # @example
9
10
  # Error.new('not_plausible', message: "can't recognize your phone", path: 'user.phone')
@@ -29,14 +30,17 @@ class ErrorNormalizer
29
30
  @payload = payload
30
31
  end
31
32
 
33
+ # Case equality check
34
+ # @return [Boolean]
32
35
  def self.===(other)
33
36
  return true if other.is_a?(Error)
34
37
  return false unless other.is_a?(Hash)
35
38
 
36
39
  h = other.transform_keys(&:to_s)
37
- h.key?('key') & h.key?('message') && h.key?('payload') && h.key?('type')
40
+ h.key?('key') && h.key?('message') && h.key?('payload') && h.key?('type')
38
41
  end
39
42
 
43
+ # @return [Hash] error Hash representation
40
44
  def to_hash
41
45
  {
42
46
  key: @key,
@@ -46,6 +50,7 @@ class ErrorNormalizer
46
50
  }
47
51
  end
48
52
 
53
+ # @return [String] error JSON string representation
49
54
  def to_json
50
55
  to_hash.to_json
51
56
  end
@@ -8,9 +8,9 @@ class ErrorNormalizer
8
8
  # In case message isn't recognized we set error to be a simple
9
9
  # normalized message (no spaces and special characters).
10
10
  #
11
- # Here are the links to AM::Errors and Dry::Validation list of error messages:
12
- # - Dry: https://github.com/dry-rb/dry-validation/blob/8417e8/config/errors.yml
13
- # - AM: https://github.com/svenfuchs/rails-i18n/blob/70b38b/rails/locale/en-US.yml#L111
11
+ # Here are the links to ActiveModel::Errors and Dry::Validation list of error messages:
12
+ # - {https://github.com/dry-rb/dry-validation/blob/8417e8/config/errors.yml dry-validation}
13
+ # - {https://github.com/svenfuchs/rails-i18n/blob/70b38b/rails/locale/en-US.yml#L111 ActiveModel::Errors}
14
14
  #
15
15
  class MessageParser
16
16
  VALUE_MATCHERS = [
@@ -41,6 +41,8 @@ class ErrorNormalizer
41
41
  @payload = {}
42
42
  end
43
43
 
44
+ # Parse error message
45
+ # @return (see #to_a)
44
46
  def parse
45
47
  parse_value_message
46
48
  return to_a if @key
@@ -52,6 +54,7 @@ class ErrorNormalizer
52
54
  to_a
53
55
  end
54
56
 
57
+ # @return [Array] a tuple of parsed [key, message, payload]
55
58
  def to_a
56
59
  [@key, @message, @payload]
57
60
  end
@@ -5,7 +5,7 @@ require_relative 'message_parser'
5
5
 
6
6
  class ErrorNormalizer
7
7
  #
8
- # Responsible for converting input to the array of normalized errors.
8
+ # Convert given input to the array of normalized errors.
9
9
  #
10
10
  # @example
11
11
  # errors = { phone: ['not plausible'] }
@@ -18,7 +18,7 @@ class ErrorNormalizer
18
18
  # # }]
19
19
  #
20
20
  class Normalizer
21
- UnsupportedInputType = Class.new(StandardError)
21
+ UnsupportedInputTypeError = Class.new(StandardError)
22
22
 
23
23
  attr_reader :errors
24
24
 
@@ -29,6 +29,9 @@ class ErrorNormalizer
29
29
  @config = config
30
30
  end
31
31
 
32
+ # Add new error object to the collection of processed errors.
33
+ # This is a more low-level method which is used by {#normalize}.
34
+ # @return [Error, Hash]
32
35
  def add_error(error, path: nil, **options)
33
36
  @errors <<
34
37
  case error
@@ -39,19 +42,21 @@ class ErrorNormalizer
39
42
  end
40
43
  end
41
44
 
45
+ # Primary method to normalize the given input
46
+ # @return [self]
42
47
  def normalize
43
- case @input
44
- when Hash
48
+ if @input.is_a?(Hash)
45
49
  normalize_hash(@input.dup)
46
- when ActiveModel::Errors
50
+ elsif @input.respond_to?(:to_hash)
47
51
  normalize_hash(@input.to_hash)
48
52
  else
49
- raise "Don't know how to normalize errors"
53
+ raise UnsupportedInputTypeError
50
54
  end
51
55
 
52
56
  self
53
57
  end
54
58
 
59
+ # @return [Array<Hash>] normalized errors of {Error#to_hash}
55
60
  def to_a
56
61
  @errors.map(&:to_hash)
57
62
  end
@@ -69,7 +74,7 @@ class ErrorNormalizer
69
74
  ns = namespaced_path(key)
70
75
  Normalizer.new(value, namespace: ns).normalize.errors.each { |e| add_error(e) }
71
76
  else
72
- raise UnsupportedInputType
77
+ raise UnsupportedInputTypeError
73
78
  end
74
79
  end
75
80
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ErrorNormalizer
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
@@ -5,7 +5,7 @@ require 'error_normalizer/version'
5
5
  require 'error_normalizer/normalizer'
6
6
 
7
7
  #
8
- # Intended to normalize errors to the single format:
8
+ # This class provides high-level API to normalize errors to the single format:
9
9
  #
10
10
  # {
11
11
  # key: 'has_already_been_taken',
@@ -16,9 +16,6 @@ require 'error_normalizer/normalizer'
16
16
  # }
17
17
  # }
18
18
  #
19
- # We shall be able to automatically convert dry-validation output to this format
20
- # and since we're using rails also automatically convert ActiveModel::Errors.
21
- #
22
19
  class ErrorNormalizer
23
20
  extend Dry::Configurable
24
21
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: error_normalizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Kondratenko
@@ -115,7 +115,7 @@ files:
115
115
  - lib/error_normalizer/message_parser.rb
116
116
  - lib/error_normalizer/normalizer.rb
117
117
  - lib/error_normalizer/version.rb
118
- homepage: https://gitlab.yalantis.com/web/error_normalizer
118
+ homepage: https://gitlab.yalantis.com/public-repo/error_normalizer/
119
119
  licenses:
120
120
  - MIT
121
121
  metadata: {}