Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix blank error message when allow_blank is false and value is blank #934

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if we can remove the !allow_nil check and the blank_forbidden? check.
I think they were added for a reason...

I don't understand what problem we are trying to fix here.

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)
Copy link
Contributor Author

@pekopekopekopayo pekopekopekopayo Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some time param_description to be "" in test_code
I'm not sure there's a case like that.
But it's a precaution

i will fix if test code is bad

if (param_description.options[:allow_nil] == false) && value.nil?
Copy link
Contributor Author

@pekopekopekopayo pekopekopekopayo Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass when param_description.options[:allow_nil] value is nil, only false

@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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

much value putting this in multiple validator ?


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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

method alias or do without entirely?


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