For my use case, I need to enable Dry::Schema.config.validate_keys = true
to report any key typos and extra keys. This seems to work fine, except for one issue. I have a certain key that should allow an arbitrary nested structure within it, but :any
unexpectedly seems to trigger validation errors if the structure is anything other than a primitive or a 1d array.
Here’s a minimal example of the problematic behavior:
require 'dry/schema'
Dry::Schema.config.validate_keys = true # must keep
AnyKeySchema = Dry::Schema.Params do
# I also tried this to opt out of validation locally
# for this node, it seems to have no effect
config.validate_keys = false
required(:any_type).filled(:any)
end
TestSchema = Dry::Schema.Params do
required(:any_key).filled(AnyKeySchema)
end
config = {
# these work OK:
# 'any_key' => {'any_type' => [1, 2, 3]},
# 'any_key' => {'any_type' => 41},
# 'any_key' => {'any_type' => 'str'},
# these fail:
'any_key' => {'any_type' => [[1, 2, 3]]}
# {'any_type' => {'foo': 42}}
}
result = TestSchema.call(config)
p result.success?
p result.errors.to_h
Output:
false
{:any_key=>{:any_type=>{0=>{1=>["is not allowed"], 2=>["is not allowed"], 3=>["is not allowed"]}}}}
I’m expecting:
true
{}
I’m comfortable using config.validate_keys = false
to locally disable all validation for the node in question, but this seems to have no effect.
To be clear, this type needs to support literally anything: plain strings, integers, booleans, arrays, hashes with any subtree structure (2d arrays, hashes of arrays, hashes of hashes, 33d arrays, etc).
How can I create a genuine “any” type that disregards all validation recursively on its entire subtree?