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

Error when using hash for key path with validate/macro #547

Closed
trentclowater opened this issue Jun 13, 2019 · 0 comments

Comments

Projects
None yet
2 participants
@trentclowater
Copy link

commented Jun 13, 2019

Using this (note the hash format for key path in the rule):

class TestContract < Dry::Validation::Contract
  register_macro(:email_format) do
    unless /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i.match?(value)
      key.failure('not a valid email format')
    end
  end

  params do
    required(:name).filled(:string)

    required(:contacts).hash do
      required(:phone).filled(:string)
      required(:email).filled(:string)
    end
  end

  rule(contacts: :email).validate(:email_format)
end

results in:

contract = TestContract.new.(name: 'Test', contacts: { phone: '555-5555', email: 'test@example.com' })

=> ArgumentError (+key+ must be a symbol, string, array, or a list of keys for dig)

but changing the rule to use dot notation format for the key path:

class TestContract < Dry::Validation::Contract
  register_macro(:email_format) do
    unless /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i.match?(value)
      key.failure('not a valid email format')
    end
  end

  params do
    required(:name).filled(:string)

    required(:contacts).hash do
      required(:phone).filled(:string)
      required(:email).filled(:string)
    end
  end

  rule('contacts.email').validate(:email_format)
end

works fine:

 => #<Dry::Validation::Result{:name=>"Test", :contacts=>{:phone=>"555-5555", :email=>"test@example.com"}} errors={}>

@trentclowater trentclowater changed the title Error when using hash for key path with validate Error when using hash for key path with validate/macro Jun 13, 2019

@solnic solnic added the 🐛 bug label Jun 13, 2019

@solnic solnic added this to the 1.0.1 milestone Jun 13, 2019

@solnic solnic closed this in #549 Jun 14, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.