File tree Expand file tree Collapse file tree 2 files changed +77
-0
lines changed
lib/graphiti/request_validators Expand file tree Collapse file tree 2 files changed +77
-0
lines changed Original file line number Diff line number Diff line change @@ -49,6 +49,13 @@ def deserialized_payload
4949 private
5050
5151 def process_relationships ( resource , relationships , payload_path )
52+ relationships . each_key do |name |
53+ unless resource . class . sideload ( name . to_sym )
54+ full_key = fully_qualified_key ( name , payload_path , :relationships )
55+ @errors . add ( full_key , :invalid_relationship , message : "is not a valid relationship" )
56+ end
57+ end
58+
5259 opts = {
5360 resource : resource ,
5461 relationships : relationships
Original file line number Diff line number Diff line change @@ -317,6 +317,76 @@ def self.name
317317 end
318318 end
319319 end
320+
321+ context "when the payload contains unknown relationships" do
322+ let ( :payload ) do
323+ {
324+ data : {
325+ type : "employees" ,
326+ attributes : { first_name : "Jane" } ,
327+ relationships : {
328+ pets : {
329+ data : { type : "pets" , id : "1" , method : "update" }
330+ }
331+ }
332+ } ,
333+ included : [
334+ {
335+ type : "pets" ,
336+ id : "1" ,
337+ attributes : { name : "mel" }
338+ }
339+ ]
340+ }
341+ end
342+
343+ it "has an unknown relationship error" do
344+ expect ( validate ) . to eq false
345+ expect ( instance . errors ) . to be_added ( :'data.relationships.pets' , :invalid_relationship )
346+ end
347+
348+ it "raises InvalidRequest when using validate!" do
349+ expect {
350+ instance . validate!
351+ } . to raise_error ( Graphiti ::Errors ::InvalidRequest )
352+ end
353+ end
354+
355+ context "when the payload contains a valid relationship" do
356+ before do
357+ root_resource_class . has_many :positions , resource : nested_resource_class
358+ end
359+
360+ let ( :payload ) do
361+ {
362+ data : {
363+ type : "employees" ,
364+ attributes : { first_name : "Jane" } ,
365+ relationships : {
366+ positions : {
367+ data : [ {
368+ 'temp-id' : "abc123" ,
369+ type : "positions" ,
370+ method : "create"
371+ } ]
372+ }
373+ }
374+ } ,
375+ included : [
376+ {
377+ 'temp-id' : "abc123" ,
378+ type : "positions" ,
379+ attributes : { title : "foo" }
380+ }
381+ ]
382+ }
383+ end
384+
385+ it "validates correctly" do
386+ expect ( validate ) . to eq true
387+ expect ( instance . errors ) . to be_blank
388+ end
389+ end
320390 end
321391 end
322392
You can’t perform that action at this time.
0 commit comments