diff --git a/lib/dry/validation/values.rb b/lib/dry/validation/values.rb index ee8b16a7..a58aff7e 100644 --- a/lib/dry/validation/values.rb +++ b/lib/dry/validation/values.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'dry/equalizer' +require 'dry/schema/path' require 'dry/validation/constants' module Dry @@ -40,16 +41,13 @@ def initialize(data) # # @api public def [](*args) - if args.size.equal?(1) - case (key = args[0]) - when Symbol then data[key] - when String then self[*key.split(DOT).map(&:to_sym)] - when Array then self[*key] - else - raise ArgumentError, '+key+ must be a symbol, string, array, or a list of keys for dig' - end + return data.dig(*args) if args.size > 1 + + case (key = args[0]) + when Symbol, String, Array, Hash + data.dig(*Schema::Path[key].to_a) else - data.dig(*args) + raise ArgumentError, '+key+ must be a valid path specification' end end diff --git a/spec/unit/values_spec.rb b/spec/unit/values_spec.rb index 4537ae0c..beb3e7e4 100644 --- a/spec/unit/values_spec.rb +++ b/spec/unit/values_spec.rb @@ -24,14 +24,18 @@ expect(values[:address, :city]).to eql('Paris') end + it 'works with a hash' do + expect(values[address: :city]).to eql('Paris') + end + it 'works with an array' do expect(values[%i[address city]]).to eql('Paris') end it 'raises on unpexpected argument type' do - expect { values[{}] } + expect { values[123] } .to raise_error( - ArgumentError, '+key+ must be a symbol, string, array, or a list of keys for dig' + ArgumentError, '+key+ must be a valid path specification' ) end end