Open bitberry-dev opened 4 years ago
An interesting thing this bug occurs only when .value(:hash)
is used. When .hash
used all is okay.
require "dry/schema"
SCHEMA1 = Dry::Schema.Params do
optional(:integer).value(:integer)
optional(:hash).value(:hash)
end
# => #<Dry::Schema::Params keys=["integer", "hash"] rules={:integer=>"key?(:integer) THEN key[integer](int?)", :hash=>"key?(:hash) THEN key[hash](hash?)"}>
SCHEMA2 = Dry::Schema.Params do
optional(:integer).value(:integer)
optional(:hash).hash
end
# => #<Dry::Schema::Params keys=["integer", "hash"] rules={:integer=>"key?(:integer) THEN key[integer](int?)", :hash=>"key?(:hash) THEN key[hash](hash?)"}>
data = {
integer: '1',
hash: nil
}
SCHEMA1.(data)
# => #<Dry::Schema::Result{:integer=>"1", :hash=>nil} errors={:integer=>["must be an integer"], :hash=>["must be a hash"]}>
SCHEMA2.(data)
# => #<Dry::Schema::Result{:integer=>1, :hash=>nil} errors={:hash=>["must be a hash"]}>
.hash
works as expected
That's an interesting bug, thanks for the report! What if you use different key names?
It does not depend on the name of the keys, I got it with completely different keys, in the example I used them for clarity :)
After a little research I found interesting place https://github.com/dry-rb/dry-schema/blob/f67d54ec0c884bd6261c508018bbf1def756635c/lib/dry/schema/macros/value.rb#L46
At this point call optional(:hash).value(:hash)
ignored
Only calls with block will processed optional(:hash).value(:hash) {}
So if I write optional(:hash).value(:hash) {}
then schemas works as expected
Hope this helps!
@bitberry-dev hmm ok, thanks! I'll take a look
@solnic is this bug already reported? It appears empty hashes are not detected by validate_keys
schema = Dry::Schema.JSON do
config.validate_keys = true
end
schema.({hello: {}})
=> #<Dry::Schema::Result{} errors={}>
schema.({hello: 1})
=> #<Dry::Schema::Result{} errors={:hello=>["is not allowed"]}>
Hello, Piotr! I found a bug
Describe the bug
In
Params
processor when validation for a key with hash value fails then we get another error for a key with integer value (string containing integer).To Reproduce
Expected behavior
The last test should return only one error for hash
Your environment
P.S. Great gem btw, thank you for your work