dry-rb / dry-schema

Coercion and validation for data structures
https://dry-rb.org/gems/dry-schema
MIT License
425 stars 110 forks source link

Wrong key validation when empty block #377

Open patodevilla opened 3 years ago

patodevilla commented 3 years ago

Describe the bug

Upgraded from version 1.6.1 to 1.7.1 and detected error in my app's tests. Basically, when an empty block is passed, key validation is wrongly validated.

I consider the bug not to be common in most apps, I just happen to have an empty block in case we needed it in the future.

To Reproduce

schema = Dry::Schema.JSON do
  config.validate_keys = true

  required(:anyOf).array(:hash) do
    optional(:some_key)
  end
end

schema.({'anyOf' => []})
# =>  #<Dry::Schema::Result{:anyOf=>[]} errors={} path=[]>
schema = Dry::Schema.JSON do
  config.validate_keys = true

  required(:anyOf).array(:hash) do
  end
end

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

Expected behavior

anyOf key should be valid

My environment