dry-rb / dry-schema

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

Unexpected behaviour with validate_keys #344

Open woto opened 3 years ago

woto commented 3 years ago

Hi. Is it documented behaviour? I think it is not.

require 'dry-validation'
require 'dry/validation/version'

class NewUserContract < Dry::Validation::Contract
  params do
    config.validate_keys = true
    required(:email).filled(:string)
    required(:age).value(:integer)
  end
end

contract = NewUserContract.new

p contract.call(email: 'jane@doe.org', age: '17', mail: rand(), unexpected_too: rand())
p Dry::Validation::VERSION
#<Dry::Validation::Result{:email=>"jane@doe.org", :age=>17} errors={:unexpected_too=>["is not allowed"]}>
"1.6.0"

It blames : unexpected_too key, but not :mail Seems it’s happened because mail is a substring of email

Sorry didn’t have time to go deeper.

patodevilla commented 3 years ago

Found the error, it appears that if the key name is a subset of a valid key the validator does not detect it,

It this case 'mail' is a subset of 'email'.

solnic commented 3 years ago

Should be easy to fix then 🙂

patodevilla commented 3 years ago

https://github.com/dry-rb/dry-schema/issues/325#issuecomment-818309219 This is another unexpected behavior I encountered

solnic commented 3 years ago

@patodevilla please report this as well