invalid_model-serializer 0.1.0
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 +7 -0
- data/LICENSE +20 -0
- data/README.md +60 -0
- data/lib/invalid_model.rb +11 -0
- data/lib/invalid_model/error.rb +24 -0
- data/lib/invalid_model/error_serializer.rb +48 -0
- data/lib/invalid_model/serializer.rb +33 -0
- metadata +153 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: df1242852f66172d00295ccb760575223afa3eb03dc8603862d39c9ed3638f9b
|
4
|
+
data.tar.gz: 2ee8cb553eca6bcbc2c17e81cf35410d3e5f2a58de5a5deffa2f133701464e29
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 45263b982a706e315c9557a594fff5c9a23c186dafacac70fe4ccd4df8dc8535e23baea9e0d147eca3f08c1468074326d4c13ee71c8d2b888ff2a47818ee4c6b
|
7
|
+
data.tar.gz: 529e8846f8f95c50d327c08c847e2683d237f79302ba603d4dacb5987ca0516897f00e09df071a345d7ac4a9dff87a7d1a35097ad2bb8b79e09ecfe5633dc6b8
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2018 Konstantin Munteanu
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# InvalidModel Serializer
|
2
|
+
|
3
|
+
You are using one of the many ruby `json_api` serializer gems like `active_model_serializers` or `fast_jsonapi`, but are not satisfied with the rendering of validation errors?
|
4
|
+
|
5
|
+
This gem is trying to fit this gap.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
Add the gem to your Gemfile
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'invalid_model-serializer'
|
13
|
+
```
|
14
|
+
|
15
|
+
To generate a `json-api` compliant error hash simply call
|
16
|
+
```ruby
|
17
|
+
InvalidModel::Serializer.new(invalid_model).serializable_hash
|
18
|
+
```
|
19
|
+
|
20
|
+
Which should parse your `ActiveModel` compatible errors and produce something like:
|
21
|
+
|
22
|
+
```json
|
23
|
+
{
|
24
|
+
"errors": [
|
25
|
+
{
|
26
|
+
"code": "validation_error/too_short",
|
27
|
+
"detail": "Name is too short (minimum is 6 characters)",
|
28
|
+
"meta": {
|
29
|
+
"count": 6
|
30
|
+
},
|
31
|
+
"source": {
|
32
|
+
"pointer": "/data/attributes/name"
|
33
|
+
},
|
34
|
+
"status": "400"
|
35
|
+
}
|
36
|
+
]
|
37
|
+
}
|
38
|
+
```
|
39
|
+
|
40
|
+
### Rails
|
41
|
+
|
42
|
+
In a rails controller you could for example use:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
def create
|
46
|
+
...
|
47
|
+
if @record.save
|
48
|
+
head :no_content
|
49
|
+
else
|
50
|
+
render json: InvalidModel::Serializer.new(@record).serializable_hash, status: :bad_request
|
51
|
+
end
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
### Options
|
56
|
+
|
57
|
+
You can pass options to the serializer as 2nd argument. The following keys are supported:
|
58
|
+
* `code_format`: Override the default format. This string will be passed through a `format` method so you can use some placeholders like `type` and `attribute`.
|
59
|
+
* `status`: Set a different status, default is `400`
|
60
|
+
* `each_serializer`: Use your own serializer for error objects.
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
|
3
|
+
module InvalidModel
|
4
|
+
autoload :Error, 'invalid_model/error'
|
5
|
+
autoload :ErrorSerializer, 'invalid_model/error_serializer'
|
6
|
+
|
7
|
+
include ActiveSupport::Configurable
|
8
|
+
|
9
|
+
config_accessor(:default_code_format) { 'validation_error/%<type>s' }
|
10
|
+
config_accessor(:default_status) { '400' }
|
11
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module InvalidModel
|
2
|
+
class Error
|
3
|
+
def self.from_hash(errors_object, attribute, hash)
|
4
|
+
new(errors_object, attribute, hash[:error], hash.except(:error))
|
5
|
+
end
|
6
|
+
|
7
|
+
attr_reader :attribute, :errors_object, :type, :meta
|
8
|
+
|
9
|
+
def initialize(errors_object, attribute, type, meta)
|
10
|
+
@errors_object = errors_object
|
11
|
+
@attribute = attribute
|
12
|
+
@type = type
|
13
|
+
@meta = meta
|
14
|
+
end
|
15
|
+
|
16
|
+
def detail
|
17
|
+
@errors_object.full_message(attribute, errors_object.generate_message(attribute, type, meta))
|
18
|
+
end
|
19
|
+
|
20
|
+
def model_name
|
21
|
+
errors_object.instance_variable_get(:@base).model_name
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module InvalidModel
|
2
|
+
class ErrorSerializer
|
3
|
+
def initialize(error, options = {})
|
4
|
+
@error = error
|
5
|
+
@options = options
|
6
|
+
end
|
7
|
+
|
8
|
+
def serializable_hash
|
9
|
+
{
|
10
|
+
code: code,
|
11
|
+
detail: detail,
|
12
|
+
meta: meta.presence,
|
13
|
+
source: source,
|
14
|
+
status: status
|
15
|
+
}.compact
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
attr_reader :error
|
21
|
+
|
22
|
+
delegate :attribute, :detail, :meta, :model_name, :type, to: :error
|
23
|
+
|
24
|
+
def code
|
25
|
+
if @options[:code]&.respond_to?(:call)
|
26
|
+
@options[:code].call error
|
27
|
+
elsif @options[:code]
|
28
|
+
@options[:code]
|
29
|
+
else
|
30
|
+
format code_format, attribute: attribute, model: model_name.singular, type: type
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def code_format
|
35
|
+
@options[:code_format] || InvalidModel.default_code_format
|
36
|
+
end
|
37
|
+
|
38
|
+
def source
|
39
|
+
return if attribute == :base
|
40
|
+
|
41
|
+
{pointer: "/data/attributes/#{attribute}"}
|
42
|
+
end
|
43
|
+
|
44
|
+
def status
|
45
|
+
@options[:status] || InvalidModel.default_status
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'invalid_model'
|
2
|
+
|
3
|
+
module InvalidModel
|
4
|
+
class Serializer
|
5
|
+
def initialize(resource, options = {})
|
6
|
+
@resource = resource
|
7
|
+
@options = options
|
8
|
+
end
|
9
|
+
|
10
|
+
def serializable_hash
|
11
|
+
{
|
12
|
+
errors: errors_list.map { |error| each_serializer_klass.new(error, @options).serializable_hash }
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def each_serializer_klass
|
19
|
+
@options[:each_serializer] || ErrorSerializer
|
20
|
+
end
|
21
|
+
|
22
|
+
def errors_object
|
23
|
+
@resource.errors
|
24
|
+
end
|
25
|
+
|
26
|
+
def errors_list
|
27
|
+
list = errors_object.details.map do |key, array|
|
28
|
+
array.map { |error| Error.from_hash(errors_object, key, error) }
|
29
|
+
end
|
30
|
+
list.flatten
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: invalid_model-serializer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- mkon
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-02-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '6'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '4.2'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '6'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: activemodel
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 5.2.2
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 5.2.2
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: json_spec
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.1'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '1.1'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rspec
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '3.7'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '3.7'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rubocop
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - '='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: 0.63.1
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - '='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: 0.63.1
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: rubocop-rspec
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - '='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: 1.32.0
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - '='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: 1.32.0
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: simplecov
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - "~>"
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0.16'
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - "~>"
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0.16'
|
117
|
+
description:
|
118
|
+
email:
|
119
|
+
- konstantin@munteanu.de
|
120
|
+
executables: []
|
121
|
+
extensions: []
|
122
|
+
extra_rdoc_files: []
|
123
|
+
files:
|
124
|
+
- LICENSE
|
125
|
+
- README.md
|
126
|
+
- lib/invalid_model.rb
|
127
|
+
- lib/invalid_model/error.rb
|
128
|
+
- lib/invalid_model/error_serializer.rb
|
129
|
+
- lib/invalid_model/serializer.rb
|
130
|
+
homepage: https://github.com/mkon/unprocessable_entitiy_serializer
|
131
|
+
licenses:
|
132
|
+
- MIT
|
133
|
+
metadata: {}
|
134
|
+
post_install_message:
|
135
|
+
rdoc_options: []
|
136
|
+
require_paths:
|
137
|
+
- lib
|
138
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
139
|
+
requirements:
|
140
|
+
- - ">="
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - ">="
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
requirements: []
|
149
|
+
rubygems_version: 3.0.2
|
150
|
+
signing_key:
|
151
|
+
specification_version: 4
|
152
|
+
summary: Serialize models with validation errors to json-api errors.
|
153
|
+
test_files: []
|