Skip to content

Commit

Permalink
Merge pull request #378 from dry-rb/reject-string-keys
Browse files Browse the repository at this point in the history
Reject non-symbol keys in schemas
  • Loading branch information
flash-gordon authored Dec 14, 2019
2 parents 0316097 + 1af5729 commit a55a469
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/dry/types/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Dry
module Types
extend Dry::Core::ClassAttributes
extend ::Dry::Core::ClassAttributes

# @!attribute [r] namespace
# @return [Container{String => Nominal}]
Expand All @@ -12,7 +12,7 @@ module Types

# Base class for coercion errors raise by dry-types
#
class CoercionError < StandardError
class CoercionError < ::StandardError
# @api private
def self.handle(exception, meta: Undefined)
if block_given?
Expand All @@ -34,7 +34,7 @@ def self.handle(exception, meta: Undefined)
# @api private
def initialize(message, meta: Undefined, backtrace: Undefined)
unless message.is_a?(::String)
raise ArgumentError, "message must be a string, #{message.class} given"
raise ::ArgumentError, "message must be a string, #{message.class} given"
end

super(message)
Expand Down Expand Up @@ -74,9 +74,9 @@ def initialize(key, value, result)
end
end

MapError = Class.new(CoercionError)
MapError = ::Class.new(CoercionError)

SchemaKeyError = Class.new(CoercionError)
SchemaKeyError = ::Class.new(CoercionError)
private_constant(:SchemaKeyError)

class MissingKeyError < SchemaKeyError
Expand Down
4 changes: 4 additions & 0 deletions lib/dry/types/schema/key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def initialize(type, name, required: Undefined, **options)
type.meta.fetch(:required) { !type.meta.fetch(:omittable, false) }
end

unless name.is_a?(::Symbol)
raise ArgumentError, "Schemas can only contain symbol keys, #{name.inspect} given"
end

super(type, name, required: required, **options)
@name = name
end
Expand Down
11 changes: 11 additions & 0 deletions spec/dry/types/errors_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

RSpec.describe 'dry types errors' do
describe Dry::Types::CoercionError do
it 'requires a string message' do
expect {
described_class.new(:invalid)
}.to raise_error(ArgumentError, /message must be a string/)
end
end
end
14 changes: 14 additions & 0 deletions spec/dry/types/hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,18 @@
specify { expect(type.transform_types?).to be(true) }
end
end

describe '#schema' do
let(:hash) { Dry::Types['hash'] }

it 'accepts only symbol keys' do
['string_key', 42, Object.new, {}, []].each do |invalid_key|
expect {
hash.schema(valid: 'symbol', invalid_key => 'integer')
}.to raise_error(
ArgumentError, /Schemas can only contain symbol keys/
)
end
end
end
end
10 changes: 10 additions & 0 deletions spec/dry/types/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,16 @@ def self.meta
string = Dry::Types['coercible.string'].meta(transformed: true)
expect(extended.key(:city)).to eql(Dry::Types::Schema::Key.new(string, :city))
end

it 'accepts only symbol keys' do
['string_key', 42, Object.new, {}, []].each do |invalid_key|
expect {
hash.schema(valid: 'symbol', invalid_key => 'integer')
}.to raise_error(
ArgumentError, /Schemas can only contain symbol keys/
)
end
end
end

describe 'keys ending with question mark' do
Expand Down

0 comments on commit a55a469

Please sign in to comment.