Closed bilby91 closed 3 years ago
This test seems to reproduce the bug:
diff --git a/spec/integration/struct_spec.rb b/spec/integration/struct_spec.rb
index 889637e..d88d8ae 100644
--- a/spec/integration/struct_spec.rb
+++ b/spec/integration/struct_spec.rb
@@ -205,6 +205,22 @@ RSpec.describe Dry::Struct do
end
end
+ describe 'optional values' do
+ subject(:struct) do
+ Class.new(Dry::Struct) do
+ attribute :person, Dry::Struct.optional do
+ attribute :name, Dry::Types['strict.string']
+ end
+ end
+ end
+
+ it 'sets missing values using default-value types' do
+ attrs = {}
+
+ expect(struct.new({}).to_h).to eql(attrs)
+ end
+ end
+
I think my problem is related:
2.5.8 :001 > module T
2.5.8 :002?> include Dry.Types
2.5.8 :003?> end
=> T
2.5.8 :006 > class TestStruct < Dry::Struct
2.5.8 :007?> attribute :foo, T::Strict::String
2.5.8 :008?> attribute :bar, T::Strict::String.optional
2.5.8 :009?> end
=> TestStruct
2.5.8 :010 > TestStruct.new(foo: "asd")
Traceback (most recent call last):
1: from (irb):10
Dry::Struct::Error ([TestStruct.new] :bar is missing in Hash input)
I had to downgrade to:
gem 'dry-types', '0.9.3'
gem 'dry-struct', '0.1.1'
to get it working again. Not sure if other, higher versions work, these are the versions that I had before upgrading to latest (struct 1.3, types 1.4)
@mtarnovan .optional
let's bar
be nil, but needs to be present in the schema. I think what you are looking is this:
class TestStruct < Dry::Struct
attribute :foo, T::Strict::String
attribute? :bar, T::Strict::String
end
I see. Thanks @bilby91
I can't use the
optional
type with structs.A clear and concise description of what the bug is.
Provide detailed steps to reproduce, an executable script would be best.
The foo will be a optional Struct type.