@@ -94,11 +94,13 @@ class Scheme
9494 #
9595 # @see MissingValidation
9696 #
97- def initialize ( allow_empty : false , expected_type : ::Object , &block )
97+ def initialize ( allow_empty : false , expected_type : ::Object , current_type : nil , registry : nil , &block )
9898 self . rules = Rules . new ( allow_empty : allow_empty , expected_type : expected_type )
9999 self . type_attributes = { }
100100 self . fixtures = [ ]
101101 self . asserted_sane = false
102+ @registry = registry
103+ @current_type = current_type
102104
103105 instance_exec ( &block ) if block_given?
104106 end
@@ -270,7 +272,7 @@ def any(scheme = nil, expected_type: ::Hash, allow_empty: false, &block)
270272 return rules . default = Attribute . new ( scheme )
271273 end
272274
273- rules . default = Scheme . new ( allow_empty : allow_empty , expected_type : expected_type , &block )
275+ rules . default = Scheme . new ( allow_empty : allow_empty , expected_type : expected_type , registry : @registry , current_type : @current_type , &block )
274276 end
275277
276278 ##
@@ -348,10 +350,20 @@ def not_strict
348350 # MyMedia.valid?({ foo: [{ required: 'test', number: 42 }, { required: 'other', number: 0 }] })
349351 # # => true
350352 #
351- def collection ( key , scheme = nil , allow_empty : false , expected_type : ::Array , optional : false , &block )
353+ def collection ( key , scheme = nil , view : nil , allow_empty : false , expected_type : ::Array , optional : false , &block )
352354 raise ConflictingTypeDefinitionError , 'You cannot apply a block to a non-hash typed collection, either remove the type or the block' if scheme != ::Hash && block_given? && !scheme . nil?
353355
354356 unless block_given?
357+ if scheme . nil?
358+ dependent_key = @current_type . as_key . dup
359+ dependent_key [ 1 ] = view
360+
361+ unless @registry . has_key? dependent_key
362+ raise Errors ::CollectionDefinitionNotFound . new ( @current_type . override_suffix ( 'json' ) . to_s , @current_type . view ( view ) . override_suffix ( 'json' ) . to_s )
363+ end
364+ scheme = @registry [ dependent_key ]
365+ end
366+
355367 return rules . add (
356368 key ,
357369 EnumerationOfType . new (
@@ -363,7 +375,16 @@ def collection(key, scheme = nil, allow_empty: false, expected_type: ::Array, op
363375 )
364376 end
365377
366- rules . add ( key , Scheme . new ( allow_empty : allow_empty , expected_type : expected_type , &block ) , optional : optional )
378+ rules . add ( key , Scheme . new ( allow_empty : allow_empty , expected_type : expected_type , registry : @registry , current_type : @current_type , &block ) , optional : optional )
379+ end
380+
381+ ##
382+ # Expect an index of links
383+ #
384+ def index ( optional : false )
385+ collection ( :_links , optional : optional ) do
386+ link :_self
387+ end
367388 end
368389
369390 ##
0 commit comments