fun_with_json_api 0.0.8.2 → 0.0.9

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: d71babdb90d947dbd0f2bd870df7f3533d02f53d
4
- data.tar.gz: bc0ac2fced2bcf61227c181f7014e88bbae8ef90
3
+ metadata.gz: 24f1af89657a79d42e1aec3122486e3196635738
4
+ data.tar.gz: 8631e1b1c88dc60f130a3abfbe4c68c0fc067287
5
5
  SHA512:
6
- metadata.gz: 00eeedfbd311bf68aea7b4436fe25c9076bc4f32e4c6cda9df06dec54a33356fe875ea488f20f811a2417f024a86d485c3c920bc89e82aed2d8bfa80a3b62df4
7
- data.tar.gz: bedef994edb9b4c3cf170f1339b38d1a842d09eeb8697bb9e45f5fdc573e0feee419dfb417efcd4808477974747d54125d1fa8931613ada08c7aab74ea9ce446
6
+ metadata.gz: 935eddfa83c835df7c086db35e11a22b8df4a3fc765d482b84508cd5007eddc35231b3f9c319f79b7874bf8054ff79663c153641da02b6f9ef651be5e3d510eb
7
+ data.tar.gz: 78aa8524d664300cdc9d2baceb3e817235964afb46c7021824806ff2f97055fddd431e3168549300fb4ae8f3a806dde0494594bae242a083da44f20587a48446
@@ -10,10 +10,10 @@ en:
10
10
  invalid_resource: 'Unable to update the relationship with this resource'
11
11
  unauthorized_resource: 'Unable to access the requested resource'
12
12
  invalid_attribute: 'Request json_api attribute data is invalid'
13
- unknown_attribute: 'Request json_api attribute is unsupported by the current endpoint'
13
+ unknown_attribute: 'Request json_api attribute is not recognised by the current endpoint'
14
14
  invalid_relationship: 'Request json_api relationship data is invalid'
15
15
  missing_relationship: 'Unable to find the requested relationship'
16
- unknown_relationship: 'Request json_api relationship is unsupported by the current endpoint'
16
+ unknown_relationship: 'Request json_api relationship is not recognised by the current endpoint'
17
17
  invalid_relationship_type: 'Request json_api relationship type does not match expected resource'
18
18
  invalid_boolean_attribute: "Boolean value should only be true, false, or null"
19
19
  invalid_date_attribute: "Date value should be in the format YYYY-MM-DD"
@@ -31,8 +31,10 @@ en:
31
31
  invalid_relationship_type_in_hash: "Expected '%{relationship}' relationship to be null or a '%{relationship_type}' resource identifier Hash"
32
32
  resource_id_can_not_be_client_generated: "The current endpoint does not allow you to set an id for a new '%{resource}' resource"
33
33
  resource_id_has_already_been_assigned: "The provided id for a new '%{resource}' resource has already been used by another resource: %{id}"
34
- unknown_attribute_for_resource: "The provided attribute '%{attribute}' can not be assigned to a '%{resource}' resource from the current endpoint"
35
- unknown_relationship_for_resource: "The provided relationship '%{relationship}' can not be assigned to a '%{resource}' resource from the current endpoint"
34
+ unknown_attribute_for_resource: "The provided attribute '%{attribute}' can not be assigned to a '%{resource}' resource"
35
+ forbidden_attribute_for_request: "The provided attribute '%{attribute}' can not be assigned to a '%{resource}' resource from the current endpoint"
36
+ unknown_relationship_for_resource: "The provided relationship '%{relationship}' can not be directly assigned to a '%{resource}' resource"
37
+ forbidden_relationship_for_request: "The provided relationship '%{relationship}' can not be assigned to a '%{resource}' resource from the current endpoint"
36
38
  find_resource_from_document:
37
39
  invalid_document: "Expected data to be a Hash or null"
38
40
  invalid_document_type: "Expected data type to be a '%{resource}' resource"
@@ -6,7 +6,7 @@ module FunWithJsonApi
6
6
  payload = Array.wrap(payload).each do |unknown|
7
7
  unknown.code ||= 'unknown_attribute'
8
8
  unknown.title ||= I18n.t(:unknown_attribute, scope: 'fun_with_json_api.exceptions')
9
- unknown.status ||= '422'
9
+ unknown.status ||= '400'
10
10
  end
11
11
  super
12
12
  end
@@ -6,7 +6,7 @@ module FunWithJsonApi
6
6
  payload = Array.wrap(payload).each do |unknown|
7
7
  unknown.code ||= 'unknown_relationship'
8
8
  unknown.title ||= I18n.t(:unknown_relationship, scope: 'fun_with_json_api.exceptions')
9
- unknown.status ||= '422'
9
+ unknown.status ||= '400'
10
10
  end
11
11
  super
12
12
  end
@@ -26,19 +26,39 @@ module FunWithJsonApi
26
26
  @resource_attributes ||= deserializer.attributes.map(&:name).map(&:to_s)
27
27
  end
28
28
 
29
+ def known_attributes
30
+ @known_attributes ||= deserializer.class.attribute_names.map(&:to_s)
31
+ end
32
+
29
33
  private
30
34
 
31
35
  def build_unknown_attribute_error(unknown_attributes)
32
36
  payload = unknown_attributes.map do |attribute|
33
- ExceptionPayload.new(
34
- detail: unknown_attribute_error(attribute),
35
- pointer: "/data/attributes/#{attribute}"
36
- )
37
+ if known_attributes.include?(attribute)
38
+ build_forbidden_attribute_payload(attribute)
39
+ else
40
+ build_unknown_attribute_payload(attribute)
41
+ end
37
42
  end
38
43
  message = 'Unknown attributes were provided by endpoint'
39
44
  FunWithJsonApi::Exceptions::UnknownAttribute.new(message, payload)
40
45
  end
41
46
 
47
+ def build_unknown_attribute_payload(attribute)
48
+ ExceptionPayload.new(
49
+ detail: unknown_attribute_error(attribute),
50
+ pointer: "/data/attributes/#{attribute}"
51
+ )
52
+ end
53
+
54
+ def build_forbidden_attribute_payload(attribute)
55
+ ExceptionPayload.new(
56
+ detail: forbidden_attribute_error(attribute),
57
+ pointer: "/data/attributes/#{attribute}",
58
+ status: '403'
59
+ )
60
+ end
61
+
42
62
  def unknown_attribute_error(attribute)
43
63
  I18n.t(
44
64
  :unknown_attribute_for_resource,
@@ -47,6 +67,15 @@ module FunWithJsonApi
47
67
  scope: 'fun_with_json_api.schema_validators'
48
68
  )
49
69
  end
70
+
71
+ def forbidden_attribute_error(attribute)
72
+ I18n.t(
73
+ :forbidden_attribute_for_request,
74
+ attribute: attribute,
75
+ resource: deserializer.type,
76
+ scope: 'fun_with_json_api.schema_validators'
77
+ )
78
+ end
50
79
  end
51
80
  end
52
81
  end
@@ -0,0 +1,86 @@
1
+ module FunWithJsonApi
2
+ module SchemaValidators
3
+ # Ensures all provided relationship names are known
4
+ class CheckRelationshipNames
5
+ def self.call(*args)
6
+ new(*args).call
7
+ end
8
+
9
+ attr_reader :document
10
+ attr_reader :deserializer
11
+ attr_reader :relationship_keys
12
+
13
+ def initialize(document, deserializer, relationship_keys)
14
+ @document = document
15
+ @deserializer = deserializer
16
+ @relationship_keys = relationship_keys
17
+ end
18
+
19
+ def call
20
+ unknown = relationship_keys.reject { |rel| resource_relationships.include?(rel) }
21
+ return if unknown.empty?
22
+
23
+ raise build_unknown_relationship_error(unknown)
24
+ end
25
+
26
+ protected
27
+
28
+ def build_unknown_relationship_error(unknown_relationships)
29
+ payload = unknown_relationships.map do |relationship|
30
+ if known_relationships.include?(relationship)
31
+ build_forbidden_relationship_payload(relationship)
32
+ else
33
+ build_unknown_relationship_payload(relationship)
34
+ end
35
+ end
36
+ message = 'Unknown relationships were provided by endpoint'
37
+ FunWithJsonApi::Exceptions::UnknownRelationship.new(message, payload)
38
+ end
39
+
40
+ def resource_relationships
41
+ @resource_relationships ||= deserializer.relationships.map(&:name).map(&:to_s)
42
+ end
43
+
44
+ def known_relationships
45
+ @known_relationships ||= deserializer.class.relationship_names.map(&:to_s)
46
+ end
47
+
48
+ private
49
+
50
+ # Relationship is known, but not supported by this request
51
+ def build_forbidden_relationship_payload(relationship)
52
+ ExceptionPayload.new(
53
+ detail: forbidden_relationship_error(relationship),
54
+ pointer: "/data/relationships/#{relationship}",
55
+ status: '403'
56
+ )
57
+ end
58
+
59
+ # Relationship is completely unknown, can cannot be assigned to this resource type (ever!)
60
+ def build_unknown_relationship_payload(relationship)
61
+ ExceptionPayload.new(
62
+ detail: unknown_relationship_error(relationship),
63
+ pointer: "/data/relationships/#{relationship}"
64
+ )
65
+ end
66
+
67
+ def unknown_relationship_error(relationship)
68
+ I18n.t(
69
+ :unknown_relationship_for_resource,
70
+ relationship: relationship,
71
+ resource: deserializer.type,
72
+ scope: 'fun_with_json_api.schema_validators'
73
+ )
74
+ end
75
+
76
+ def forbidden_relationship_error(relationship)
77
+ I18n.t(
78
+ :forbidden_relationship_for_request,
79
+ relationship: relationship,
80
+ resource: deserializer.type,
81
+ scope: 'fun_with_json_api.schema_validators'
82
+ )
83
+ end
84
+ end
85
+ end
86
+ end
@@ -1,3 +1,5 @@
1
+ require 'fun_with_json_api/schema_validators/check_relationship_names'
2
+
1
3
  module FunWithJsonApi
2
4
  module SchemaValidators
3
5
  class CheckRelationships
@@ -16,19 +18,13 @@ module FunWithJsonApi
16
18
  def call
17
19
  relationships = document['data'].fetch('relationships', {})
18
20
 
19
- check_for_unknown_relationships! relationships.keys
21
+ CheckRelationshipNames.call(document, deserializer, relationships.keys)
22
+
20
23
  check_for_invalid_relationship_type! relationships
21
24
 
22
25
  true
23
26
  end
24
27
 
25
- def check_for_unknown_relationships!(relationship_keys)
26
- unknown = relationship_keys.reject { |rel| resource_relationships.include?(rel) }
27
- return if unknown.empty?
28
-
29
- raise build_unknown_relationship_error(unknown)
30
- end
31
-
32
28
  def check_for_invalid_relationship_type!(relationships_hash)
33
29
  payload = build_invalid_relationship_type_payload(relationships_hash)
34
30
  return if payload.empty?
@@ -54,10 +50,6 @@ module FunWithJsonApi
54
50
  build_invalid_relationship_item_payload(relationship)
55
51
  end
56
52
 
57
- def resource_relationships
58
- @resource_relationships ||= deserializer.relationships.map(&:name).map(&:to_s)
59
- end
60
-
61
53
  private
62
54
 
63
55
  def invalid_relationship_type_in_array_message(relationship)
@@ -102,26 +94,6 @@ module FunWithJsonApi
102
94
  pointer: "/data/relationships/#{relationship.name}/data/type"
103
95
  )
104
96
  end
105
-
106
- def build_unknown_relationship_error(unknown_relationships)
107
- payload = unknown_relationships.map do |relationship|
108
- ExceptionPayload.new(
109
- detail: unknown_relationship_error(relationship),
110
- pointer: "/data/relationships/#{relationship}"
111
- )
112
- end
113
- message = 'Unknown relationships were provided by endpoint'
114
- FunWithJsonApi::Exceptions::UnknownRelationship.new(message, payload)
115
- end
116
-
117
- def unknown_relationship_error(relationship)
118
- I18n.t(
119
- :unknown_relationship_for_resource,
120
- relationship: relationship,
121
- resource: deserializer.type,
122
- scope: 'fun_with_json_api.schema_validators'
123
- )
124
- end
125
97
  end
126
98
  end
127
99
  end
@@ -1,3 +1,3 @@
1
1
  module FunWithJsonApi
2
- VERSION = '0.0.8.2'.freeze
2
+ VERSION = '0.0.9'.freeze
3
3
  end