Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dongyeon-langdy committed Jun 4, 2024
1 parent 5c2552b commit 68fae8f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/apipie/param_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def validate(value)
return true if allow_nil && value.nil?
return true if allow_blank && value.blank?
value = normalized_value(value)
return true if !validator.valid?(value)
return true if validator.valid?(value)

error = validator.error
error = ParamError.new(error) unless error.is_a? StandardError
Expand Down
17 changes: 11 additions & 6 deletions lib/apipie/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ def self.raise_if_missing_params

# check if value is valid
def valid?(value)
if !param_description.allow_nil && value.nil?
@error_value = nil
return false
elsif !param_description.allow_blank && value.blank?
@error_value = 'blank'
return false
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)
Expand Down Expand Up @@ -488,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 68fae8f

Please sign in to comment.