permit is checked before convert
Closed this issue · 1 comments
Describe the problem
When using an argument with convert :list
and permit: [...]
, the permit is checked before the conversion.
Steps to reproduce the problem
require 'tty-option'
class Command
include TTY::Option
argument :fruit do
required
convert :list
arity one_or_more
permit %w[coconut mango pear] if ENV['PERMIT']
end
def run
unless params.valid?
puts params.errors.messages.join("\n")
end
puts params
end
end
cmd = Command.new
cmd.parse
cmd.run
Actual behaviour
An error saying the values are unpermitted:
USE_PERMIT=true ruby test-command.rb coconut mango pear
# unpermitted value `["coconut", "mango", "pear"]` for 'fruit' argument: choose from coconut, mango, pear
# {:help=>false, :fruit=>nil}
Expected behaviour
Permit is checked after converting to a list:
ruby test-command.rb coconut mango pear
# {:help=>false, :fruit=>["coconut", "mango", "pear"]}
Describe your environment
- OS version: macOS 12.2 arm64
- Ruby version: ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [arm64-darwin21]
- TTY::Option version: 0.2.0
Hi Brian,
Thanks for the issue report.
I don't believe this is a precedence issue. The permit
checks if a value is contained within the permitted values array. In your case, you're checking if an array element is in permitted values array. This is what the error message that you have provided says as much:
# unpermitted value `["coconut", "mango", "pear"]` for 'fruit' argument: choose from coconut, mango, pear
When I designed permit
method my intention was for checking a single value against a set of possible values and nothing more.
Following your example you could potentially specify conversion to a hash. How would the comparison work then? Is the hash input meant to be compared with individual elements of permitted array? That' why I added validate to specify any custom validation.