How to disable key validation for a hash prop

In my RoR app, I’ve declared a initializer to define validate_key

Dry::Schema.config.validate_keys = true

Let’s say I’ve got the following schema

schema = Dry::Schema.Json do
  optional(:foo).maybe(:hash)
end

I wan’t to be able to send any hash to the prop foo, but when I do it I’ve got an error

schema.call({ foo: { bar: 'baz' } })
#<Dry::Schema::Result{:foo=>{:bar=>"baz"}} errors={:foo=>{:bar=>["is not allowed"]}} path=[]>

I’m not able to reproduce this behavior with the code sample you’ve provided. Can you give me exact reproduction steps?

Dry::Schema.config.validate_keys = false

schema = Dry::Schema.JSON do
  optional(:foo).maybe(:hash)
end
[1] pry(main)> schema.call({ foo: { bar: 'baz' } })
=> #<Dry::Schema::Result{:foo=>{:bar=>"baz"}} errors={} path=[]>

@alassek When validate_keys is false, the problem won’t happening.
I want to be able to validate keys, but not on my hash prop.

Dry::Schema.config.validate_keys = true

schema = Dry::Schema.JSON do
  optional(:foo).maybe(:hash)
end

r = schema.call({ bar: 1, foo: { bar: 2 } })
r.errors.to_h
# {:bar=>["is not allowed"], :foo=>{:bar=>["is not allowed"]}}

In the example above, bar shouldn’t be allowed on root, but it should be inside the hash foo prop

Okay I understand. This appears to be a bug in the key validator.

1 Like