-
-
Notifications
You must be signed in to change notification settings - Fork 190
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
schema do | ||||||
optional(:kilometers).filled(:integer) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
optional(:miles).filled(:integer) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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. | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we want to support the same type of args as def key?(*args)
args.empty? ? values.key?(key_name) : values.key?(*args)
end There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was intending this to work with The 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @alassek ah right, doesn't have to be |
||
values.key?(name) | ||
end | ||
|
||
# Check if there are any errors on the schema under the provided path | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.