Skip to content

Commit

Permalink
fix blank error message when allow_blank is false and value is balnk
Browse files Browse the repository at this point in the history
  • Loading branch information
pekopekopekopayo committed Jun 13, 2024
1 parent eef4770 commit fe66db0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
10 changes: 5 additions & 5 deletions lib/apipie/param_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
13 changes: 13 additions & 0 deletions lib/apipie/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/apipie/param_description_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions spec/support/custom_bool_validator.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class CustomBoolValidator < Apipie::Validator::BaseValidator
def valid?(value)
validate(value)
end

def validate(value)
value.in?([true, false])
end
Expand Down

0 comments on commit fe66db0

Please sign in to comment.