error_normalizer 0.1.0 → 0.1.1
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 +4 -4
- data/Gemfile.lock +1 -1
- data/error_normalizer.gemspec +1 -1
- data/lib/error_normalizer/error.rb +8 -3
- data/lib/error_normalizer/message_parser.rb +6 -3
- data/lib/error_normalizer/normalizer.rb +12 -7
- data/lib/error_normalizer/version.rb +1 -1
- data/lib/error_normalizer.rb +1 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4035feeceb8c759e702989ff2d013b40a935fcf4b4ca2afe79b9742738ff39a6
|
4
|
+
data.tar.gz: f7235ca637c3503f126ff091724d80d672462b08b6b7ade550bba1ad4ab801ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 252e4a3d03e3c1a1f190c77774fa0ca2ac23adb6ca5bd02afc93a9e7cc4fbe819c63fee1c98c6fee7d9d0a4a485032773a2b56f935b16b8c7d958a17bb9cc7da
|
7
|
+
data.tar.gz: e50077ea3edd89c7c091aa92138f54fd49bc41ac1f0670fbed866c829e63f7094be1b646613f058ff6ea1775713564af7fec106f0c84b85f4a2083dac6dd0e4e
|
data/Gemfile.lock
CHANGED
data/error_normalizer.gemspec
CHANGED
@@ -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/
|
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
|
6
|
-
#
|
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')
|
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
|
12
|
-
#
|
13
|
-
#
|
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
|
-
#
|
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
|
-
|
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
|
-
|
44
|
-
when Hash
|
48
|
+
if @input.is_a?(Hash)
|
45
49
|
normalize_hash(@input.dup)
|
46
|
-
|
50
|
+
elsif @input.respond_to?(:to_hash)
|
47
51
|
normalize_hash(@input.to_hash)
|
48
52
|
else
|
49
|
-
raise
|
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
|
77
|
+
raise UnsupportedInputTypeError
|
73
78
|
end
|
74
79
|
end
|
75
80
|
end
|
data/lib/error_normalizer.rb
CHANGED
@@ -5,7 +5,7 @@ require 'error_normalizer/version'
|
|
5
5
|
require 'error_normalizer/normalizer'
|
6
6
|
|
7
7
|
#
|
8
|
-
#
|
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.
|
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/
|
118
|
+
homepage: https://gitlab.yalantis.com/public-repo/error_normalizer/
|
119
119
|
licenses:
|
120
120
|
- MIT
|
121
121
|
metadata: {}
|