diff --git a/lib/apipie/validator.rb b/lib/apipie/validator.rb
index a56f227c..a20e1c62 100644
--- a/lib/apipie/validator.rb
+++ b/lib/apipie/validator.rb
@@ -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
diff --git a/spec/lib/apipie/validator_spec.rb b/spec/lib/apipie/validator_spec.rb
index 10eca9b1..c7479932 100644
--- a/spec/lib/apipie/validator_spec.rb
+++ b/spec/lib/apipie/validator_spec.rb
@@ -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: first
, second & third
.')
+ 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: first
, second & third
.')
+ 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: first
, second & third
.')
+ end
end
end
end