jsonapi_errorable 0.6.0 → 0.6.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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c83d0f33880b1ff0f1c6b69396e1aa6a22a074cf
|
4
|
+
data.tar.gz: e9182252a226267a32b05bb2b81845b9f9e91beb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b11c3d3a3ac8d174d197bfe0c49579eb96351141f977f61819ee9eb7c959d089b76159649018388aa8c967918adc4f2804844efaf11c1c6bf89cb05eddf8464
|
7
|
+
data.tar.gz: 110aa34014a6648dc4656bdddc4d3c463aac23e4828b4ce663621dbdd8921bb71aa190cd2ffa38782a5041f1de0793cd57e15811629ad6852f067d70831a55a1
|
@@ -3,23 +3,33 @@ module JsonapiErrorable
|
|
3
3
|
class Validation
|
4
4
|
attr_reader :object
|
5
5
|
|
6
|
-
def initialize(object)
|
6
|
+
def initialize(object, relationship_params, relationship_message = {})
|
7
7
|
@object = object
|
8
|
+
@relationship_params = relationship_params || {}
|
9
|
+
@relationship_message = relationship_message
|
8
10
|
end
|
9
11
|
|
10
12
|
def errors
|
11
|
-
object.errors
|
13
|
+
return [] unless object.respond_to?(:errors)
|
14
|
+
|
15
|
+
all_errors = object.errors.to_hash.map do |attribute, messages|
|
12
16
|
messages.map do |message|
|
17
|
+
meta = { attribute: attribute, message: message }.merge(@relationship_message)
|
18
|
+
meta = { relationship: meta } if @relationship_message.present?
|
13
19
|
{
|
14
20
|
code: 'unprocessable_entity',
|
15
21
|
status: '422',
|
16
22
|
title: 'Validation Error',
|
17
23
|
detail: "#{attribute.capitalize} #{message}",
|
18
24
|
source: { pointer: pointer_for(object, attribute) },
|
19
|
-
meta:
|
25
|
+
meta: meta
|
20
26
|
}
|
21
27
|
end
|
22
28
|
end.flatten
|
29
|
+
all_errors << relationship_errors(@relationship_params)
|
30
|
+
all_errors.flatten!
|
31
|
+
all_errors.compact!
|
32
|
+
all_errors
|
23
33
|
end
|
24
34
|
|
25
35
|
def relationship?(name)
|
@@ -49,6 +59,28 @@ module JsonapiErrorable
|
|
49
59
|
def activerecord?
|
50
60
|
object.is_a?(ActiveRecord::Base)
|
51
61
|
end
|
62
|
+
|
63
|
+
def relationship_errors(relationship_params)
|
64
|
+
errors = []
|
65
|
+
relationship_params.each_pair do |name, payload|
|
66
|
+
related = Array(@object.send(name))
|
67
|
+
related.each do |r|
|
68
|
+
if payload.is_a?(Array)
|
69
|
+
related_payload = payload.find { |p| p[:meta][:temp_id] === r.instance_variable_get(:@_jsonapi_temp_id) || p[:meta][:id] == r.id }
|
70
|
+
else
|
71
|
+
related_payload = payload
|
72
|
+
end
|
73
|
+
relationship_message = {
|
74
|
+
name: name,
|
75
|
+
id: r.id,
|
76
|
+
:'temp-id' => r.instance_variable_get(:@_jsonapi_temp_id)
|
77
|
+
}
|
78
|
+
|
79
|
+
errors << Validation.new(r, related_payload[:relationships], relationship_message).errors
|
80
|
+
end
|
81
|
+
end
|
82
|
+
errors
|
83
|
+
end
|
52
84
|
end
|
53
85
|
end
|
54
86
|
end
|
@@ -1,10 +1,11 @@
|
|
1
1
|
module JsonapiErrorable
|
2
2
|
module Validatable
|
3
3
|
def render_errors_for(record)
|
4
|
-
validation = Serializers::Validation.new
|
4
|
+
validation = Serializers::Validation.new \
|
5
|
+
record, deserialized_params.relationships
|
5
6
|
|
6
7
|
render \
|
7
|
-
json: validation.errors,
|
8
|
+
json: { errors: validation.errors },
|
8
9
|
status: :unprocessable_entity
|
9
10
|
end
|
10
11
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi_errorable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lee Richmond
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-04-
|
11
|
+
date: 2017-04-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -163,7 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
163
163
|
version: '0'
|
164
164
|
requirements: []
|
165
165
|
rubyforge_project:
|
166
|
-
rubygems_version: 2.6.
|
166
|
+
rubygems_version: 2.6.11
|
167
167
|
signing_key:
|
168
168
|
specification_version: 4
|
169
169
|
summary: jsonapi.org compatible error handling
|