@@ -36,21 +36,11 @@ def collection?
3636 private
3737
3838 def validate!
39- case
40- when !@data_defined && !@meta_defined && !@errors_defined
41- fail InvalidDocument ,
42- "a document MUST contain at least one of 'data', 'meta', or" \
43- " or 'errors' at top-level"
44- when @data_defined && @errors_defined
45- fail InvalidDocument ,
46- "'data' and 'errors' MUST NOT coexist in the same document"
47- when !@data_defined && @included_defined
48- fail InvalidDocument , "'included' MUST NOT be present unless 'data' is"
49- when @options [ :verify_duplicates ] && duplicates?
50- fail InvalidDocument ,
39+ if @options [ :verify_duplicates ] && duplicates?
40+ raise JSONAPI ::Validator ::InvalidDocument ,
5141 "resources MUST NOT appear both in 'data' and 'included'"
52- when @options [ :verify_linkage ] && !full_linkage?
53- fail InvalidDocument ,
42+ elsif @options [ :verify_linkage ] && !full_linkage?
43+ raise JSONAPI :: Validator :: InvalidDocument ,
5444 "resources in 'included' MUST respect full-linkage"
5545 end
5646 end
@@ -69,8 +59,7 @@ def full_linkage?
6959 return true unless @included
7060
7161 reachable = Set . new
72- # NOTE(lucas): Does Array() already dup?
73- queue = Array ( data ) . dup
62+ queue = Array ( data )
7463 included_resources = Hash [ included . map { |r | [ [ r . type , r . id ] , r ] } ]
7564 queue . each { |resource | reachable << [ resource . type , resource . id ] }
7665
@@ -94,33 +83,23 @@ def full_linkage?
9483 def parse_data ( data_hash )
9584 collection = data_hash . is_a? ( Array )
9685 if collection
97- data_hash . map { |h | Resource . new ( h , @options . merge ( id_optional : true ) ) }
86+ data_hash . map { |h | Resource . new ( h , @options ) }
9887 elsif data_hash . nil?
9988 nil
10089 else
101- Resource . new ( data_hash , @options . merge ( id_optional : true ) )
90+ Resource . new ( data_hash , @options )
10291 end
10392 end
10493
10594 def parse_meta ( meta_hash )
106- fail InvalidDocument , "the value of 'meta' MUST be an object" unless
107- meta_hash . is_a? ( Hash )
10895 meta_hash
10996 end
11097
11198 def parse_included ( included_hash )
112- fail InvalidDocument ,
113- "the value of 'included' MUST be an array of resource objects" unless
114- included_hash . is_a? ( Array )
115-
11699 included_hash . map { |h | Resource . new ( h , @options ) }
117100 end
118101
119102 def parse_errors ( errors_hash )
120- fail InvalidDocument ,
121- "the value of 'errors' MUST be an array of error objects" unless
122- errors_hash . is_a? ( Array )
123-
124103 errors_hash . map { |h | Error . new ( h , @options ) }
125104 end
126105 end
0 commit comments