Skip to content

Instantly share code, notes, and snippets.

@adis-io
Created May 4, 2020 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adis-io/67b49b8e2abf1362054100bd55a6021d to your computer and use it in GitHub Desktop.
Save adis-io/67b49b8e2abf1362054100bd55a6021d to your computer and use it in GitHub Desktop.
How to solve Dry::Struct::Error <attribute> is missing in Hash input

I'm using dry-rb and dry-struct. And I wanted make some attributes optional, but I was confused with optional.

So the thing is, you can't use optional for skipping attributes. Optional means you can pass nil values, that's all.

So if you want to skip attributes, use this attribute by calling attribute?

class User < Dry::Struct
  attribute :name, Types::String
  attribute? :age, Types::Integer
end

user = User.new(name: 'Jane')
# => #<User name="Jane" age=nil>
user.age
# => nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment