(there is no dry-schema tag)
Hi,
This is what I’m trying to do: I have a configuration structure that can come from either command-line parameters or a file. There are also defaults to this configuration, and some of the members need to be coerced into objects. I would like to make one single authoritative schema for this structure.
The structure also has a couple nested hashes in it, which may not be supplied in either configuration or command-line switches, so I want to supply defaults. My problem is I have no idea how to say “this member of the schema is a hash which itself should have $THESE
members with their own defaults, and if you omit the whole thing, use $THIS
for a default.” Neither the tutorial nor reference documentation are clear on how to handle this use case.
module Types
include Dry::Types()
end
DEFAULT_HASH = Types::Hash.default({ inner: 'value'.freeze }.freeze)
HashSchema = Dry::Schema.Params do
optional(:outer).value(DEFAULT_HASH).hash do
optional(:inner).value(Types::String.default 'value'.freeze)
end
optional(:other).value(DEFAULT_HASH)
end
p HashSchema.call({})
# => #<Dry::Schema::Result{:other=>{:inner=>"value"}} errors={}>
My expectation here is that :outer
should be populated in addition to :other
. I have tried another configuration creating a hash type with a schema (which crashes) but let’s just consider this one for now.