elixir-maru / maru

Elixir RESTful Framework
https://maru.readme.io
BSD 3-Clause "New" or "Revised" License
1.32k stars 85 forks source link

Unnamed parameter lists #65

Closed acerberus closed 7 years ago

acerberus commented 7 years ago

Hi,

I was wondering if it is possible to define an unnamed parameter list. Something in the following style:

params do
    requires  type: List do
      requires :foo, type: String
      requires :bar, type: String
    end
  end

The idea is to send json arrays, for example;

[ {"foo" : "foobar", "bar": "foobar"}, ...]

As far as I can tell, all parameters have to be named, which usually makes sense, except for the above use case. Any help or pointers are much appreciated.

falood commented 7 years ago

Hi @acerberus Plug doesn't handle list parameter. With curl -XPOST 127.0.0.1:8828/list_test -H 'Content-Type: application/json' -d '[1, 2, 3]' , I get %{"_json" => [1, 2, 3]} when run conn.params, so the name of unnamed parameter lists is _json, you can handle it with such code:

params do
  requires :_json, type: List do
    YOUR NESTED PARAMETERS
  end
end

and use it by params[:_json]

acerberus commented 7 years ago

Hi @falood, works like a charm. Thanks for the quick response and the good work on maru.