grape icon indicating copy to clipboard operation
grape copied to clipboard

Doc Update Request: Nested re-usable params

Open rwilliams3088 opened this issue 2 years ago • 1 comments

I'm attempting was attempting to define a re-usable params type and make use of it for a request. The result is I get this: NameError (uninitialized constant Grape::Validations::Types::SymbolCoercer).

Here is my re-usable param structure:

   helpers do
      params :identifier_type do
        optional :id, type: String
        optional :name, type: String
        exactly_one_of :id, :name
      end
  end

Since there was no clear examples in the docs of how to go about it, I was trying things like this - which just gave me errors.

params do
  required :identifier, type: :identifier_type
end
post '' do
  ...
end

Eventually, I found a pattern that works:

params do
  required :identifier, type: Hash do
    use :identifier_type
  end
end
post '' do
  ...
end

Adding something like this to the documentation would be helpful for others, I think

rwilliams3088 avatar Apr 06 '23 19:04 rwilliams3088

Definitely a yes on documentation, please PR!

We could probably improve the error here. It is trying to coerce a symbol and you get a SymbolCoercer error.

While type: :identifier_type seems nice, maybe it's too nice and we should not allow to say type: <something that's not actually a type>. If you think it's a great idea, maybe try to PR it? Implementing a SymbolCoercer would do it, but I think it won't be that easy ;)

dblock avatar Apr 06 '23 21:04 dblock