keygen-sh / typed_params

An alternative to Rails strong_parameters. Define structured and strongly-typed parameter schemas for your Rails controllers.
MIT License
123 stars 0 forks source link

Question about params for specific controller action #5

Closed kinsomicrote closed 1 year ago

kinsomicrote commented 1 year ago

Hello, first, thanks for this gem. This is a fit for a solution I've been looking for.

I'm trying to create a typed param specific to a controller action, so I have something like this;

typed_params on: :index do
     param :ids, type: :array, optional: true
end

Following the documentation, I expect that this should work;

{
    "ids": [1,2]
}

But it does not, I get the error; unpermitted parameter. Any hint on what I might be missing?

ezekg commented 1 year ago

What's the request's content type that you're sending? If you're using curl, feel free to include the request as an example.

Typically, index actions use the GET method, and I'm not sure GET allows a request body.

kinsomicrote commented 1 year ago

The request content type is application/json and I am making use of Postman. I get the same error when I use POST method. If I have a typed param like this:

typed_params do
  param :title, type: :string
  param :number, type: :integer
  param :period, type: :string
end

And I send a payload like this;

{
    "title": "Testing",
    "period": "Human",
    "number": 6
}

But this works;

typed_params do
  params :record, type: :hash do
    param :title, type: :string
    param :number, type: :integer
    param :period, type: :string
  end
end

With a payload like this;

{
    "record": {
        "title": "Sitepoint",
        "period": "Human",
        "number": 6
    }
}
ezekg commented 1 year ago

I can't reproduce this: https://gist.github.com/ezekg/d913007bbf93f94a6c741f08d12d8bcc

Can you provide a failing test? What does the unpermitted param error message say?

ezekg commented 1 year ago

Closing due to lack of response. Feel free to reopen with a reproduction script.