ruby-grape / grape

An opinionated framework for creating REST-like APIs in Ruby.
http://www.ruby-grape.org
MIT License
9.89k stars 1.22k forks source link

Header validation DSL #1371

Open thedrow opened 8 years ago

thedrow commented 8 years ago

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:

LeFnord commented 8 years ago

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

dblock commented 8 years ago

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

thedrow commented 8 years ago

@dblock Exactly what I was going to say

pynixwang commented 7 years ago

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"
}
dblock commented 7 years ago

@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.

pynixwang commented 7 years ago

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

dblock commented 7 years ago

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.

pynixwang commented 7 years ago

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.

dblock commented 7 years ago

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

pynixwang commented 7 years ago

@dblock I 'm trying.

MiralDesai commented 6 years ago

+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_ofetc methods already present for params

dhempy commented 2 years ago

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