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

Support passing a callable to EnumValidator #946

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions lib/apipie/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,23 +177,23 @@ def description
class EnumValidator < BaseValidator
def initialize(param_description, argument)
super(param_description)
@array = argument
@argument = argument
end

def validate(value)
@array.include?(value)
values.include?(value)
end

def self.build(param_description, argument, options, proc)
self.new(param_description, argument) if argument.is_a?(Array)
self.new(param_description, argument) if argument.is_a?(Array) || argument.respond_to?(:call)
Copy link
Member Author

Choose a reason for hiding this comment

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

@mathieujobin I'm not sure how to disambiguate this from the ProcValidator here:

self.new(param_description, argument) if argument.is_a?(Proc) && argument.arity == 1

If you have any thoughts, I'd love to hear them.

I thought about introducing a class that somehow wraps the callable, but it feels rather heavy.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Because EnumValidator will receive a Proc as well ?
Can you check that is it an Enum before?

.respond_to?(:call) can be a lot of things... so, you accept to build a Enum validator with either an Array or a Proc? not an Enum ? is a Enum a Proc underneath?

it would be good to add a new test where the confusion between the two validator can be demonstrated and the right one is being selected. this will help finding a working implementation.

end

def values
@array
@argument.respond_to?(:call) ? @argument.call : @argument
end

def description
string = @array.map { |value| format_description_value(value) }.join(', ')
string = values.map { |value| format_description_value(value) }.join(', ')
"Must be one of: #{string}."
end
end
Expand Down
35 changes: 26 additions & 9 deletions spec/lib/apipie/validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,34 @@
end

describe 'EnumValidator' do
it "validates by object class" do
validator = Apipie::Validator::EnumValidator.new(params_desc, ['first', 'second & third'])
expect(validator.validate("first")).to be_truthy
expect(validator.validate("second & third")).to be_truthy
expect(validator.validate(1)).to be_falsey
expect(validator.validate({ 1 => 1 })).to be_falsey
context 'with an array' do
subject(:validator) { Apipie::Validator::EnumValidator.new(params_desc, ['first', 'second & third']) }

it "validates by object class" do
expect(validator.validate("first")).to be_truthy
expect(validator.validate("second & third")).to be_truthy
expect(validator.validate(1)).to be_falsey
expect(validator.validate({ 1 => 1 })).to be_falsey
end

it "has a valid description" do
expect(validator.description).to eq('Must be one of: <code>first</code>, <code>second &amp; third</code>.')
end
end

it "has a valid description" do
validator = Apipie::Validator::EnumValidator.new(params_desc, ['first', 'second & third'])
expect(validator.description).to eq('Must be one of: <code>first</code>, <code>second &amp; third</code>.')
context 'with a callable' do
subject(:validator) { Apipie::Validator::EnumValidator.new(params_desc, -> { ['first', 'second & third'] }) }

it "validates by object class" do
expect(validator.validate("first")).to be_truthy
expect(validator.validate("second & third")).to be_truthy
expect(validator.validate(1)).to be_falsey
expect(validator.validate({ 1 => 1 })).to be_falsey
end

it "has a valid description" do
expect(validator.description).to eq('Must be one of: <code>first</code>, <code>second &amp; third</code>.')
end
end
end
end
Loading