From fe66db02633db057c7108effdf8a3a8b91d8d603 Mon Sep 17 00:00:00 2001 From: pekopekopekopayo Date: Fri, 14 Jun 2024 00:46:57 +0900 Subject: [PATCH 1/2] fix blank error message when allow_blank is false and value is balnk --- lib/apipie/param_description.rb | 10 +++++----- lib/apipie/validator.rb | 13 +++++++++++++ spec/lib/apipie/param_description_spec.rb | 2 +- spec/support/custom_bool_validator.rb | 4 ++++ 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/apipie/param_description.rb b/lib/apipie/param_description.rb index 7b917d683..f62067edb 100644 --- a/lib/apipie/param_description.rb +++ b/lib/apipie/param_description.rb @@ -120,11 +120,11 @@ def validate(value) return true if allow_nil && value.nil? return true if allow_blank && value.blank? value = normalized_value(value) - if (!allow_nil && value.nil?) || (blank_forbidden? && value.blank?) || !validator.valid?(value) - error = validator.error - error = ParamError.new(error) unless error.is_a? StandardError - raise error - end + return true if validator.valid?(value) + + error = validator.error + error = ParamError.new(error) unless error.is_a? StandardError + raise error end def blank_forbidden? diff --git a/lib/apipie/validator.rb b/lib/apipie/validator.rb index a56f227ce..d55e9ead8 100644 --- a/lib/apipie/validator.rb +++ b/lib/apipie/validator.rb @@ -50,6 +50,16 @@ def self.raise_if_missing_params # check if value is valid def valid?(value) + if param_description.is_a?(Apipie::ParamDescription) + if (param_description.options[:allow_nil] == false) && value.nil? + @error_value = nil + return false + elsif (param_description.options[:allow_blank] == false) && value.blank? + @error_value = 'blank' + return false + end + end + if self.validate(value) @error_value = nil true @@ -480,6 +490,9 @@ def self.validate(value) end class BooleanValidator < BaseValidator + def valid?(value) + validate(value) + end def validate(value) %w[true false 1 0].include?(value.to_s) diff --git a/spec/lib/apipie/param_description_spec.rb b/spec/lib/apipie/param_description_spec.rb index c939f2b2f..a279925ca 100644 --- a/spec/lib/apipie/param_description_spec.rb +++ b/spec/lib/apipie/param_description_spec.rb @@ -305,7 +305,7 @@ let(:validation_value) { '' } it 'raises an error' do - expect { subject }.to raise_error(Apipie::ParamInvalid) + expect { subject }.not_to raise_error(Apipie::ParamInvalid) end end diff --git a/spec/support/custom_bool_validator.rb b/spec/support/custom_bool_validator.rb index 78234383b..5b7dd04a9 100644 --- a/spec/support/custom_bool_validator.rb +++ b/spec/support/custom_bool_validator.rb @@ -1,4 +1,8 @@ class CustomBoolValidator < Apipie::Validator::BaseValidator + def valid?(value) + validate(value) + end + def validate(value) value.in?([true, false]) end From 3d1c7c355df526f1f15eaa5332b4695a655e3f9c Mon Sep 17 00:00:00 2001 From: Mathieu Jobin <99191+mathieujobin@users.noreply.github.com> Date: Fri, 12 Jul 2024 09:21:43 +0900 Subject: [PATCH 2/2] checking if another Exception is raised --- spec/lib/apipie/param_description_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/lib/apipie/param_description_spec.rb b/spec/lib/apipie/param_description_spec.rb index a279925ca..cdffce79c 100644 --- a/spec/lib/apipie/param_description_spec.rb +++ b/spec/lib/apipie/param_description_spec.rb @@ -305,7 +305,7 @@ let(:validation_value) { '' } it 'raises an error' do - expect { subject }.not_to raise_error(Apipie::ParamInvalid) + expect { subject }.not_to raise_error # (Apipie::ParamInvalid) end end