Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Result "errors(full: true).to_h" method output not expected #618

Closed
luccasmaso opened this issue Jan 19, 2020 · 8 comments
Closed

Result "errors(full: true).to_h" method output not expected #618

luccasmaso opened this issue Jan 19, 2020 · 8 comments
Labels
Milestone

Comments

@luccasmaso
Copy link

The expected value for Dry::Validation::Result instance method errors is to

The current behavior is that, for params do the key is added to the message, but for rule do that adds a failure to the key it isn't:

dry-validation: 1.4

class User::Contract < Dry::Validation::Contract
  params do
    required(:pin).value(:string, size?: 4)
  end

  rule(:pin) do
    key.failure('must be only integer') if value.scan(/\D/).present?
  end
end
contract = Validators::User::Contract.new
contract.call(pin: "12345").errors(full: true).to_h
# {:pin=>["pin length must be 4"]}
contract.call(pin: "abcd").errors(full: true).to_h
# {:pin=>["must be only integer"]}
# Should be {:pin=>["pin must be only integer"]}
@luccasmaso
Copy link
Author

luccasmaso commented Jan 19, 2020

Ok, maybe I've not checked the API documentation properly.

I should be calling failure with a meta hash parameter if I want to keep the key:
key.failure(text: "must be only integer", key: :ping)

Is that right? Still, it is kind confusing because I am calling failure on the key already

@solnic
Copy link
Member

solnic commented Jan 19, 2020

You're right. This is a bug. Thanks for reporting.

@waiting-for-dev
Copy link
Member

Shouldn't it be moved to dry-validation?

@flash-gordon flash-gordon transferred this issue from dry-rb/dry-schema Jan 20, 2020
@flash-gordon flash-gordon added this to the 1.5.0 milestone Jan 20, 2020
@solnic solnic removed this from the 1.5.0 milestone Mar 11, 2020
@korrs
Copy link

korrs commented Mar 24, 2020

I started to fix this bug and found strange behaviour: When validation fails in params and rule definitions, then only error from params will be shown, error message from rule will be skipped.

For example:

contract.call(pin: "a").errors(full: true).to_h # two errors, less than 4 and not digit
# {:pin=>["pin length must be 4"]}

But when params check passed we can see rule error

contract.call(pin: "abcd").errors(full: true).to_h
# {:pin=>["must be only integer"]}

Is it expected behaviour?

I suppose, all error messages should be shown after validation:

contract.call(pin: "a").errors(full: true).to_h
# {:pin=>["pin length must be 4", "pin must be only integer"]}

@flash-gordon
Copy link
Member

@korrs absolutely, you cannot run rule checks on ill-typed input

@korrs
Copy link

korrs commented Mar 25, 2020

👌

@solnic solnic added this to the 1.5.1 milestone Mar 25, 2020
@sirfilip
Copy link

Hello love dryrb and love to help with this one,

Will create pr with proposed solution

sirfilip pushed a commit to sirfilip/dry-validation that referenced this issue Jun 16, 2020
solnic added a commit that referenced this issue Jun 17, 2020
@solnic
Copy link
Member

solnic commented Jun 24, 2020

This was fixed in 1.5.1

@solnic solnic closed this as completed Jul 14, 2020
tadeusz-niemiec pushed a commit to tadeusz-niemiec/dry-validation that referenced this issue Aug 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants