jsonapi_errorable 0.6.0 → 0.6.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
  SHA1:
3
- metadata.gz: 63b8a3652fd9414efabf6132a0f6271570b2463e
4
- data.tar.gz: c40c41cc6bacf302bdc1c11ac5a2da11a4b5f92c
3
+ metadata.gz: c83d0f33880b1ff0f1c6b69396e1aa6a22a074cf
4
+ data.tar.gz: e9182252a226267a32b05bb2b81845b9f9e91beb
5
5
  SHA512:
6
- metadata.gz: 18531dd8ed2e9f13613008feab5479778b3d364178883812865d0c794c690e48e704d304e7f68012303876a166e741ccc41f991592d9cbbfb319cc6086661dd0
7
- data.tar.gz: c0ba639d7807955bc3eead312d6c63d24de0a36f6a4d05179dc88679e44ebdc322ad40fb02eeda167d7e0b2951f67685ff040a6b73cecc3d0e5b8beec79e1559
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.to_hash.map do |attribute, messages|
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: { attribute: attribute, message: message }
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(record)
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
@@ -1,3 +1,3 @@
1
1
  module JsonapiErrorable
2
- VERSION = "0.6.0"
2
+ VERSION = "0.6.1"
3
3
  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.0
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-09 00:00:00.000000000 Z
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.7
166
+ rubygems_version: 2.6.11
167
167
  signing_key:
168
168
  specification_version: 4
169
169
  summary: jsonapi.org compatible error handling