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

Rule executed despite schema being invalid #576

Closed
vladra opened this issue Jul 30, 2019 · 1 comment · Fixed by #579
Closed

Rule executed despite schema being invalid #576

vladra opened this issue Jul 30, 2019 · 1 comment · Fixed by #579
Labels
Milestone

Comments

@vladra
Copy link

vladra commented Jul 30, 2019

Hi ✌️ Not sure if I use rules correctly, but looks like its a bug. Please take a look. Thank you.

Describe the bug

Rule for nested attributes is being executed despite schema contains invalid values.

To Reproduce

require 'dry/validation'

class ExampleContract < Dry::Validation::Contract
  params do
    required(:contract).schema do
      required(:active_from).value(:date)
      required(:active_until).value(:date)
    end
  end

  rule(contract: [:active_from, :active_until]) do
    key.failure('ops') if value[0] < value[1]
  end
end

f = (Date.today - 1000).to_s
u = (Date.today + 1000).to_s

puts ExampleContract.new.call(contract: { active_from: f, active_until: u }).inspect
#<Dry::Validation::Result{:contract=>{:active_from=>#<Date: 2016-11-02 ((2457695j,0s,0n),+0s,2299161j)>, :active_until=>#<Date: 2022-04-25 ((2459695j,0s,0n),+0s,2299161j)>}} errors={:contract=>{[:active_from, :active_until]=>["ops"]}}>

puts ExampleContract.new.call(contract: { active_from: 'abc', active_until: u }).inspect
# comparison of String with Date failed (ArgumentError)

Expected behavior

Rule should not be executed when schema is invalid.

@solnic
Copy link
Member

solnic commented Jul 30, 2019

Thanks for the report. I'll fix it in the next bug fix release.

For now, as a workaround, use dot-notation:

rule("contract.active_from", "contract.active_until") do
  key.failure('ops') if values["contract.active_from"] < values["contract.active_until"]
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants