Skip to content
Permalink
Browse files

Not allow to use weak or symbolize as constructor_type

  • Loading branch information...
GustavoCaso authored and flash-gordon committed Nov 2, 2017
1 parent 2f09f0b commit 0bc4fed0394b3e5009a98efb99b07321abb4e508
Showing with 13 additions and 8 deletions.
  1. +5 −8 lib/dry/struct.rb
  2. +8 −0 spec/dry/struct_spec.rb
@@ -70,6 +70,10 @@ class Struct
defines :schema
schema EMPTY_HASH

def self.valid_constructor_types
Dry::Types['symbol'].enum(:permissive, :schema, :strict, :strict_with_defaults)
end

# Sets or retrieves {#constructor} type as a symbol
#
# @note All examples below assume that you have defined {Struct} with
@@ -107,13 +111,6 @@ class Struct
# 3. Input contains nil for a value that specifies a default
# 4. Input includes a key that was not specified in the schema
#
# @note Don’t use `:weak` and `:symbolized` as {#constructor_type},
# and instead use [`dry-validation`][] to process and validate
# attributes, otherwise your struct will behave as a data validator
# which raises exceptions on invalid input (assuming your attributes
# types are strict)
# [`dry-validation`]: https://github.com/dry-rb/dry-validation
#
# @example `:permissive` constructor
# class User < Dry::Struct
# constructor_type :permissive
@@ -177,7 +174,7 @@ class Struct
# @overload constructor_type
# Returns the constructor type for {Struct}
# @return [Symbol] (:strict)
defines :constructor_type
defines :constructor_type, type: valid_constructor_types
constructor_type :permissive

# @return [Dry::Equalizer]
@@ -265,6 +265,14 @@ class Test::Child < Test::Parent; end
end
end

describe 'defining constructor_type with weak or symbolized' do
it 'raises InvalidClassAttributeValue' do
expect{
class Test::Parent < Dry::Struct; constructor_type(:weak); end
}.to raise_error(Dry::Core::InvalidClassAttributeValue)
end
end

describe 'with a blank schema' do
it 'works for blank structs' do
class Test::Foo < Dry::Struct; end

0 comments on commit 0bc4fed

Please sign in to comment.
You can’t perform that action at this time.