Git Product home page Git Product logo

Comments (4)

bilby91 avatar bilby91 commented on August 20, 2024 7

@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

from dry-struct.

bilby91 avatar bilby91 commented on August 20, 2024

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
+

from dry-struct.

mtarnovan avatar mtarnovan commented on August 20, 2024

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)

from dry-struct.

mtarnovan avatar mtarnovan commented on August 20, 2024

I see. Thanks @bilby91

from dry-struct.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.