hanami / controller

Complete, fast and testable actions for Rack and Hanami

Home Page:http://hanamirb.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Action params are not coerced?

wuarmin opened this issue · comments

commented
module API
  module Actions
    module Customers
      class Index < API::Action
        include Deps["repositories.customers_repo"]

        params do
          optional(:first_name).filled(:str?)
          optional(:limit).filled(:int?)
        end

        def handle(req, res)
          halt 400, req.params.errors.to_json unless req.params.valid?
          # returns {"limit":["must be an integer"]}
          # for http://localhost:2300/api/customers?limit=20
        end
      end
    end
  end
end

thanks and best regards

I can reproduce the problem.

commented

Thanks!
I tried to find and fix the problem, but didn't have enough time today. maybe tomorrow.

Can you please try defining your params schema using type specs? These are missing from your schema definition here.

commented

@timriley thanks! Following works:

module API
  module Actions
    module Customers
      class Index < API::Action
        include Deps["repositories.customers_repo"]

        params do
          optional(:first_name).filled(:string) 
          optional(:limit).filled(:integer)
        end

        def handle(req, res)
          halt 400, req.params.errors.to_json unless req.params.valid?
          res.body = "yess"
        end
      end
    end
  end
end

Is it intentional that a type predicate alone does not cause a conversion? That's new, isn't it?

@wuarmin With type specs, the code works as expected.