From df3ecdeb848a552579caec3e140be43d7d788e63 Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Tue, 5 Nov 2024 15:35:24 +0100 Subject: [PATCH] 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. --- lib/apipie/validator.rb | 10 ++++----- spec/lib/apipie/validator_spec.rb | 35 +++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 14 deletions(-) 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