Skip to content

Commit df3ecde

Browse files
committed
Support passing a callable to EnumValidator
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.
1 parent eeef4de commit df3ecde

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

lib/apipie/validator.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,23 +177,23 @@ def description
177177
class EnumValidator < BaseValidator
178178
def initialize(param_description, argument)
179179
super(param_description)
180-
@array = argument
180+
@argument = argument
181181
end
182182

183183
def validate(value)
184-
@array.include?(value)
184+
values.include?(value)
185185
end
186186

187187
def self.build(param_description, argument, options, proc)
188-
self.new(param_description, argument) if argument.is_a?(Array)
188+
self.new(param_description, argument) if argument.is_a?(Array) || argument.respond_to?(:call)
189189
end
190190

191191
def values
192-
@array
192+
@argument.respond_to?(:call) ? @argument.call : @argument
193193
end
194194

195195
def description
196-
string = @array.map { |value| format_description_value(value) }.join(', ')
196+
string = values.map { |value| format_description_value(value) }.join(', ')
197197
"Must be one of: #{string}."
198198
end
199199
end

spec/lib/apipie/validator_spec.rb

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,34 @@
133133
end
134134

135135
describe 'EnumValidator' do
136-
it "validates by object class" do
137-
validator = Apipie::Validator::EnumValidator.new(params_desc, ['first', 'second & third'])
138-
expect(validator.validate("first")).to be_truthy
139-
expect(validator.validate("second & third")).to be_truthy
140-
expect(validator.validate(1)).to be_falsey
141-
expect(validator.validate({ 1 => 1 })).to be_falsey
136+
context 'with an array' do
137+
subject(:validator) { Apipie::Validator::EnumValidator.new(params_desc, ['first', 'second & third']) }
138+
139+
it "validates by object class" do
140+
expect(validator.validate("first")).to be_truthy
141+
expect(validator.validate("second & third")).to be_truthy
142+
expect(validator.validate(1)).to be_falsey
143+
expect(validator.validate({ 1 => 1 })).to be_falsey
144+
end
145+
146+
it "has a valid description" do
147+
expect(validator.description).to eq('Must be one of: <code>first</code>, <code>second &amp; third</code>.')
148+
end
142149
end
143150

144-
it "has a valid description" do
145-
validator = Apipie::Validator::EnumValidator.new(params_desc, ['first', 'second & third'])
146-
expect(validator.description).to eq('Must be one of: <code>first</code>, <code>second &amp; third</code>.')
151+
context 'with a callable' do
152+
subject(:validator) { Apipie::Validator::EnumValidator.new(params_desc, -> { ['first', 'second & third'] }) }
153+
154+
it "validates by object class" do
155+
expect(validator.validate("first")).to be_truthy
156+
expect(validator.validate("second & third")).to be_truthy
157+
expect(validator.validate(1)).to be_falsey
158+
expect(validator.validate({ 1 => 1 })).to be_falsey
159+
end
160+
161+
it "has a valid description" do
162+
expect(validator.description).to eq('Must be one of: <code>first</code>, <code>second &amp; third</code>.')
163+
end
147164
end
148165
end
149166
end

0 commit comments

Comments
 (0)