metadata
Instead of trying to disable validate_keys
in the type, you define metadata
as a hash that doesn’t declare its keys i.e., an “open” hash. In dry-validation DSL, that means just using .hash
with no nested schema, or .hash(any: :any)
.
Here’s the clean approach:
class MyContract < Dry::Validation::Contract
config.validate_keys = true
params do
required(:allowed_param).filled(:string)
optional(:metadata).hash
end
end
That’s it.
No nested schema means dry-schema will only check “is this a hash?” and won’t validate keys inside it even with validate_keys = true
.
class MyContract < Dry::Validation::Contract
config.validate_keys = true
params do
required(:allowed_param).filled(:string)
optional(:metadata).hash do
any(:any)
end
end
end
Both of these achieve your goal: metadata
can have any keys and values, but the top-level still rejects unexpected keys.