grape icon indicating copy to clipboard operation
grape copied to clipboard

Header validation DSL

Open thedrow opened this issue 8 years ago • 12 comments

Currently there is no way to validate headers with Grape using the DSL. It'd be useful to be able to specify that a header must be present or must be equal to something without coding it manually. Providing a header validation DSL can help you say things like:

  • The client should never ask to cache the request using Cache-Control
  • The server only responds in UTF-8 charset.
  • A custom header must be present and equal to "Yes"

thedrow avatar Apr 26 '16 07:04 thedrow

for grape-swagger we had a similar discussion … one approach was, to define the header parameters for the request in the same manner as the other one, e.g.:

params do
  requires :X-Rate-Limit-Limit, type: Integer, documentation: {desc: '…', param_type: 'header' }
end

this way, it could be used the params validations, what did you think? on the other side, the request params are consistent

at the moment, the header key from the desc block would be used, with the downside, you mentioned

LeFnord avatar Apr 26 '16 08:04 LeFnord

IMO we want headers do just like params do reusing the exact implementation of params.

dblock avatar Apr 26 '16 11:04 dblock

@dblock Exactly what I was going to say

thedrow avatar Apr 26 '16 11:04 thedrow

maybe we also need body Entity

to simplify json schema from client.

with

params do
  requires :user
end

client must post json like this

{
  "user":
    {
      "id":"123"
    }
}

I just want

{
  "id":"123"
}

pynixwang avatar Feb 20 '17 03:02 pynixwang

@pynixwang Your example should be

params do
  requires :id
end

Body is read into params as long as content-type is JSON or form or such.

dblock avatar Feb 20 '17 15:02 dblock

@dblock swagger-doc show as formData, I want a Model Scheme. and I want just accept json not form...

pynixwang avatar Feb 20 '17 15:02 pynixwang

Swagger is a whole other issue, but you cannot have it both ways - call something a userand expect the DSL to ignore it. You can however move the declarations out of the params part, and then use that.

dblock avatar Feb 20 '17 16:02 dblock

make a body DSL like body :user

and do not merge hash into params, just put into prams so we can reference like params[user]

or new reference body[:user] or just body or something like object and collection for array.

pynixwang avatar Feb 21 '17 03:02 pynixwang

@pynixwang Feel free to enable this with a pull request.

dblock avatar Feb 21 '17 13:02 dblock

@dblock I 'm trying.

pynixwang avatar Feb 22 '17 03:02 pynixwang

+1 I know this is old, but still feel it's relevant. We're half way there in that we can define headers to be re required or not inside the desc block See here. but it would also be nice to be able to validate them with the all_or_none_of, at_least_one_of etc methods already present for params

MiralDesai avatar May 01 '18 10:05 MiralDesai

Agreed...would love to see header validation DSL matching params validation DSL.

dhempy avatar Oct 20 '22 14:10 dhempy