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

TypeError: no implicit conversion of Symbol into Integer using nested hash and rule #676

Closed
TimoMoss opened this issue Nov 29, 2020 · 1 comment · Fixed by #711
Closed

Comments

@TimoMoss
Copy link

TimoMoss commented Nov 29, 2020

Hello,
I'm getting an error in the following combination

  • gem 'dry-validation', '~> 1.4
  • Affects my production application: YES.
  • Ruby version: '2.5.7'
  • OS: Mac OS
class ValidateParams < Dry::Validation::Contract
   schema do
     required(:data).hash do
       required(:relationships).hash do
           required(:object).hash do
             required(:data).hash do
                required(:id).filled(:string)
            end
          end
        end
      end
   end

  register_macro(:format) do |macro:|
     if value&.!~ macro.args.first
       message = 'nvalid format'
       key(keys.flatten).failure(message)
     end
  end

  rule(%i[data relationships object data id]).validate(format: /some reg exp/)
end

scenario:
I'm trying to check that the type of the nested hash is Hash,
for this purpose, in specs, I assign to it an Array, so I expect to get a message:
{data: {relationships: {object: {data: ['must be a hash']}}}}

RSpec.describe ValidateParams do
 let(:validation) { described_class.new }
 context 'invalid type of inested hash' do
    before do
      params[:data][:relationships][:object][:data] = []
    end

    it 'looks like failure' do
       message = {data: {relationships: {object: {data: ['must be a hash']}}}}
       expect(validation.call(params).errors.to_h).to eq(message)
    end
 end

instead of that, I get the following error:

  Failure/Error: if value&.!~ macro.args.first
     
     TypeError:
       no implicit conversion of Symbol into Integer

Please note that the error started to occur only after adding the rule which checks for the format of a key included in the nested hash.

if I change the checking type to sting or integer I get another error:

before do
  params[:data][:relationships][:object][:data] = 'some string'
end
Failure/Error: if value&.!~ macro.args.first
     
     TypeError:
       String does not have #dig method

Also, it might be important that the point where the error happens is the method 'value'

@TimoMoss TimoMoss added the bug label Nov 29, 2020
@solnic solnic added this to the 1.6.1 milestone Dec 5, 2020
@solnic
Copy link
Member

solnic commented Dec 8, 2020

hey @TimoMoss thanks for reporting this. I didn't manage to fix it in 1.6.0 but we'll get to it eventually in 1.6.1 🙂

@solnic solnic removed this from the 1.6.1 milestone Feb 19, 2022
solnic added a commit that referenced this issue Jun 17, 2022
[changelog]

version: unreleased
fixed: "Duplicated keys mishandling in rule evaluation (issue #676 fixed via #711) (@mereghost)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants