@@ -477,5 +477,84 @@ def self.validate!(value)
477
477
end
478
478
end
479
479
end
480
+
481
+ describe 'exception full_path_array' do
482
+ it 'has the proper full path with single param check' do
483
+ allow ( controller ) . to receive ( :params ) . and_return ( { "price" => "abc" } )
484
+ begin
485
+ controller . param! :price , Integer , required : true
486
+ rescue RailsParam ::Param ::InvalidParameterError => e
487
+ expect ( e . full_path_array ) . to eql [ "price" ]
488
+ end
489
+ end
490
+
491
+ it 'has the proper full path when using arrays with hashes' do
492
+ params = {
493
+ 'array' => [
494
+ { 'object' => { 'num' => '1' , 'float' => '1.4' } } ,
495
+ { 'object' => { 'num' => '2' , 'float' => 'abc' } }
496
+ ]
497
+ }
498
+ allow ( controller ) . to receive ( :params ) . and_return ( params )
499
+ begin
500
+ controller . param! :array , Array do |a |
501
+ a . param! :object , Hash do |h |
502
+ h . param! :num , Integer , required : true
503
+ h . param! :float , Float , required : true
504
+ end
505
+ end
506
+ rescue RailsParam ::Param ::InvalidParameterError => e
507
+ expect ( e . full_path_array ) . to eql [ "array" , 1 , "object" , "float" ]
508
+ end
509
+ end
510
+
511
+ it 'has the proper full path when using arrays with primitive types' do
512
+ params = {
513
+ 'array' => [ "abc" ]
514
+ }
515
+ allow ( controller ) . to receive ( :params ) . and_return ( params )
516
+ begin
517
+ controller . param! :array , Array do |array , index |
518
+ array . param! index , Integer , :required => true
519
+ end
520
+ rescue RailsParam ::Param ::InvalidParameterError => e
521
+ expect ( e . full_path_array ) . to eql [ 'array' , 0 ]
522
+ end
523
+ end
524
+
525
+ it 'has the proper full path when using hashes' do
526
+ params = {
527
+ 'hash' => {
528
+ 'hash2' => { 'integers' => [ '123' , 'abc' ] }
529
+ }
530
+ }
531
+ allow ( controller ) . to receive ( :params ) . and_return ( params )
532
+ begin
533
+ controller . param! :hash , Hash do |a |
534
+ a . param! :hash2 , Hash do |h |
535
+ h . param! :integers , Array do |array , index |
536
+ array . param! index , Integer
537
+ end
538
+ end
539
+ end
540
+ rescue RailsParam ::Param ::InvalidParameterError => e
541
+ expect ( e . full_path_array ) . to eql [ "hash" , "hash2" , "integers" , 1 ]
542
+ end
543
+ end
544
+ end
545
+
546
+ describe 'full_path' do
547
+ it 'has the correct full path for root level item' do
548
+ e = RailsParam ::Param ::InvalidParameterError . new ( 'error' )
549
+ e . full_path_array = [ "price" ]
550
+ expect ( e . full_path ) . to eql "price"
551
+ end
552
+
553
+ it 'has the correct full path for nested item' do
554
+ e = RailsParam ::Param ::InvalidParameterError . new ( 'error' )
555
+ e . full_path_array = [ "hash" , "hash2" , "integers" , 1 ]
556
+ expect ( e . full_path ) . to eql "hash[hash2][integers][1]"
557
+ end
558
+ end
480
559
end
481
560
end
0 commit comments