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

Evaluator#key? forwards key name argument #664

Merged
merged 1 commit into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 17 additions & 0 deletions docsite/source/rules.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,23 @@ contract.call(email: 'jane@doe.org', login: 'jane', password: "").errors.to_h
# => {:password=>["password is required"]}
```

The `key?` method supports passing an explicit key name for rules that have multiple keys.

```ruby
class DistanceContract < Dry::Validation::Contract
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we have a spec at contract level (/spec/integration/contract/) mimicking this same scenario where we are asking of an specific keys?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I think the test coverage is pretty comprehensive already but that would probably better communicate the intent of this.

Copy link
Member

Choose a reason for hiding this comment

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

I merged it as-is because having this example in the docs already covers the intended usage and the implementation is so simple that the unit test will suffice.

schema do
optional(:kilometers).filled(:integer)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
optional(:kilometers).filled(:integer)
optional(:kilometers).value(:integer)

optional(:miles).filled(:integer)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
optional(:miles).filled(:integer)
optional(:miles).value(:integer)

end

rule(:kilometers, :miles) do
if key?(:kilometers) ^ key?(:miles)
base.failure("must only contain one of: kilometers, miles")
end
end
end
```

### Checking for previous errors

Sometimes you may be interested in adding an error when some other error has happened.
Expand Down
13 changes: 10 additions & 3 deletions lib/dry/validation/evaluator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,23 @@ def value
#
# This is useful when dealing with rules for optional keys
#
# @example
# @example use the default key name
# rule(:age) do
# key.failure(:invalid) if key? && value < 18
# end
#
# @example specify the key name
# rule(:start_date, :end_date) do
# if key?(:start_date) && !key?(:end_date)
# key(:end_date).failure("must provide an end_date with start_date")
# end
# end
#
# @return [Boolean]
#
# @api public
def key?
values.key?(key_name)
def key?(name = key_name)
Copy link
Member

@solnic solnic Aug 12, 2020

Choose a reason for hiding this comment

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

I think we want to support the same type of args as Values#key?, WDYT? Values supports symbols, arrays, path objects etc. so this could be something like:

def key?(*args)
  args.empty? ? values.key?(key_name) : values.key?(*args)
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.

I was intending this to work with Schema::Path, yes.

The Values implementation doensn't rely on splatting to work, is that necessary here?

      def key?(key, hash = data)
        return hash.key?(key) if key.is_a?(Symbol)

        Schema::Path[key].reduce(hash) do |a, e|
          if e.is_a?(Array)
            result = e.all? { |k| key?(k, a) }
            return result
          elsif e.is_a?(Symbol) && a.is_a?(Array)
            return false
          else
            return false unless a.is_a?(Array) ? (e >= 0 && e < a.size) : a.key?(e)
          end
          a[e]
        end

        true
      end

Copy link
Member

Choose a reason for hiding this comment

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

@alassek ah right, doesn't have to be *args, good point!

values.key?(name)
end

# Check if there are any errors on the schema under the provided path
Expand Down
18 changes: 17 additions & 1 deletion spec/integration/evaluator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
end

let(:options) do
{keys: [:email], result: {}, values: values, _context: {}}
{keys: [:email, :name], result: {}, values: values, _context: {}}
end

let(:values) do
Expand Down Expand Up @@ -46,4 +46,20 @@
end
end
end

describe "#key?" do
subject(:evaluator) do
Dry::Validation::Evaluator.new(contract, **options)
end

it "delegates to #values" do
expect(values).to receive(:key?).with(:name)
evaluator.key?(:name)
end

it "uses #key_name as the default argument" do
expect(values).to receive(:key?).with(:email)
evaluator.key?
end
end
end