solnic on master
Cast identifier to String durin… Merge pull request #169 from ad… (compare)
Dry::Struct::Error: [User.new] :name is missing in Hash input
User.new.foo # => {:meta=>{:omittable=>true}}
I'm trying to get User.new.foo # => {}
(which is what constructor_type :schema
did before)
class User < Dry::Struct
transform_types do |type|
type.optional.deafult({})
end
attribute :foo, Types::Hash
end
class User < Dry::Struct
transform_types do |type|
if type.default?
type
else
type.meta(omittable: true)
end
end
attribute :foo, Types::Hash.default({})
attribute :bar, Types::String
end
User.new
=> #<User foo={} bar=nil>
transform_types
many times
constructor_type :schema
style :P )
optional(:group_ids, [:int]).value(:array?)
but am having trouble figuring out how the element validation would work. Any advice?
Hi guys :) I am learning (and trying to implement) dry-transaction, I am going thru the Introduction and found this:Each operation shouldn’t accumulate state, instead it should receive an input and return an output without causing any side-effects.
What do you mean by that? Especially this part without causing any side-effects
?
module ApiTokens
class Create
include Dry::Transaction
step :validate
step :persist
step :regenerate_token
def validate(input)
validation = ApiTokenSchema.call(input)
validation.success? ? Success(input) : Failure(validation.errors)
end
def persist(input)
api_token = ApiToken.new(input)
api_token.save ? Success(api_token) : Failure(api_token.errors)
end
def regenerate_token(api_token)
api_token.regenerate_token!
Success(api_token)
end
end
end