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

Parameter validation: Raises error for all missing #886

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 5 additions & 4 deletions lib/apipie/dsl_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,11 @@ def _apipie_define_validators(description)
method_params = self.class._apipie_get_method_params(action_name)

if Apipie.configuration.validate_presence?
method_params.each do |_, param|
# check if required parameters are present
raise ParamMissing.new(param) if param.required && !params.key?(param.name)
Validator::BaseValidator.raise_if_missing_params do |missing|
method_params.each do |_, param|
# check if required parameters are present
missing << param if param.required && !params.key?(param.name)
end
end
end

Expand Down Expand Up @@ -285,7 +287,6 @@ def _apipie_define_validators(description)
old_method.bind(self).call(*args)
end
end

end
end

Expand Down
14 changes: 14 additions & 0 deletions lib/apipie/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ def initialize(param)
end
end

class ParamMultipleMissing < ParamError
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would be nice to inherit from the same error as ParamMissing, but we do not have the defined param here.

attr_accessor :params

def initialize(params)
@params = params
end

def to_s
params.map do |param|
ParamMissing.new(param).to_s
end.join("\n")
end
end

class ParamMissing < DefinedParamError
def to_s
unless @param.options[:missing_message].nil?
Expand Down
26 changes: 20 additions & 6 deletions lib/apipie/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ def self.find(param_description, argument, options, block)
return nil
end

def self.raise_if_missing_params
missing_params = []
yield missing_params
if missing_params.size > 1
raise ParamMultipleMissing.new(missing_params)
elsif missing_params.size == 1
raise ParamMissing.new(missing_params.first)
end
end

# check if value is valid
def valid?(value)
if self.validate(value)
Expand Down Expand Up @@ -345,14 +355,18 @@ def params_ordered

def validate(value)
return false if !value.is_a? Hash
@hash_params&.each do |k, p|
if Apipie.configuration.validate_presence?
raise ParamMissing.new(p) if p.required && !value.key?(k)
end
if Apipie.configuration.validate_value?
p.validate(value[k]) if value.key?(k)

BaseValidator.raise_if_missing_params do |missing|
@hash_params&.each do |k, p|
if Apipie.configuration.validate_presence?
missing << p if p.required && !value.key?(k)
end
if Apipie.configuration.validate_value?
p.validate(value[k]) if value.key?(k)
end
end
end

return true
end

Expand Down
10 changes: 10 additions & 0 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def compare_hashes(h1, h2)
expect(methods.keys).to include(:update)
expect(methods.keys).to include(:two_urls)
expect(methods.keys).to include(:action_with_headers)
expect(methods.keys).to include(:multiple_required_params)
end

it "should contain info about resource" do
Expand Down Expand Up @@ -101,6 +102,10 @@ def reload_controllers
expect { get :show, :params => { :id => 5 }}.to raise_error(Apipie::ParamMissing, /session_parameter_is_required/)
end

it "should fail if multiple required parameters are missing" do
expect { get :multiple_required_params }.to raise_error(Apipie::ParamMultipleMissing, /required_param1.*\n.*required_param2|required_param2.*\n.*required_parameter1/)
end

it "should pass if required parameter has wrong type" do
expect { get :show, :params => { :id => 5 , :session => "secret_hash" }}.not_to raise_error
expect { get :show, :params => { :id => "ten" , :session => "secret_hash" }}.not_to raise_error
Expand Down Expand Up @@ -246,6 +251,11 @@ def reload_controllers
post :create, :params => { :user => { :name => "root", :pass => "12345", :membership => "____" } }
}.to raise_error(Apipie::ParamInvalid, /membership/)

# Should include both pass and name
expect {
post :create, :params => { :user => { :membership => "standard" } }
}.to raise_error(Apipie::ParamMultipleMissing, /pass.*\n.*name|name.*\n.*pass/)

expect {
post :create, :params => { :user => { :name => "root" } }
}.to raise_error(Apipie::ParamMissing, /pass/)
Expand Down
6 changes: 6 additions & 0 deletions spec/dummy/app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,10 @@ def create_route
header :HeaderNameWithDefaultValue, 'Header with default value', required: true, default: 'default value'
def action_with_headers
end

api :GET, '/users/multiple_required_params'
param :required_param1, String, required: true
param :required_param2, String, required: true
def multiple_required_params
end
end
1 change: 1 addition & 0 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
resources :users do
collection do
post :create_route
get :multiple_required_params
end
end
resources :concerns, :only => [:index, :show]
Expand Down