Skip to content

Commit

Permalink
Support passing a callable to EnumValidator
Browse files Browse the repository at this point in the history
In case the possible values are dynamic. This probably can't ever be
made to work reliably with statically generating HTML documentation.

It's expected that the proc never throws an exception.
  • Loading branch information
ekohl committed Nov 5, 2024
1 parent eeef4de commit a07a525
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
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)
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

0 comments on commit a07a525

Please sign in to comment.