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

Fix Undefined method `key?' for nil:NilClass error. #672

Merged
merged 2 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/dry/validation/values.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def key?(key, hash = data)
return result
elsif e.is_a?(Symbol) && a.is_a?(Array)
return false
elsif a.is_a?(String) || a.nil?
return false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a.is_a?(String) was needed? It shouldn't be, at this point all keys should be symbolized.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It for the case when root value is a string. I have checked and it is not symbolized.
It covered by this test case

  let(:data) do
    {
      name: "Jane",
      address: {city: "Paris", geo: {lat: 1, lon: 2}},
      phones: [123, 431],
      billing_address: nil,
      card: ""
    }
  end

    it "returns false when a nested key is not present and root key is string" do
      expect(values.key?([:card, :not_here])).to be(false)
    end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I should check for valid types instead?

          elsif !(a.is_a?(Array) || a.is_a?(Hash))
            return false

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexxty7 aahh that's right, this can happen! I think it should be handled separately though, so:

# ...
elsif a.nil?
  return false
elsif a.is_a?(String)
  return false
# ...
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@solnic updated

else
return false unless a.is_a?(Array) ? (e >= 0 && e < a.size) : a.key?(e)
end
Expand Down
16 changes: 15 additions & 1 deletion spec/unit/values_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
end

let(:data) do
{name: "Jane", address: {city: "Paris", geo: {lat: 1, lon: 2}}, phones: [123, 431]}
{
name: "Jane",
address: {city: "Paris", geo: {lat: 1, lon: 2}},
phones: [123, 431],
billing_address: nil,
card: ""
}
end

describe "#[]" do
Expand Down Expand Up @@ -84,6 +90,14 @@
it "returns false when a path to an array is a symbol as its last segment" do
expect(values.key?([:phones, :foo])).to be(false)
end

it "returns false when a nested key is not present and root key is nil" do
expect(values.key?([:billing_address, :not_here])).to be(false)
end

it "returns false when a nested key is not present and root key is string" do
expect(values.key?([:card, :not_here])).to be(false)
end
end

describe "#dig" do
Expand Down