79734756

Date: 2025-08-13 21:37:27
Score: 0.5
Natty:
Report link

Use an open hash schema for 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.

If you want to be explicit about “any key, any value”

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.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: Kurt Goodwin