New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access request in custom validators. #986

Open
nalbeza opened this Issue Apr 15, 2015 · 7 comments

Comments

Projects
None yet
4 participants
@nalbeza

nalbeza commented Apr 15, 2015

Is it possible to access request context in custom validators ?

It would be very useful, for example to check for uniqueness or use helpers such as current_user.

@dblock

This comment has been minimized.

Member

dblock commented Apr 15, 2015

It certainly is. See https://github.com/intridea/grape/blob/master/lib/grape/validations/params_scope.rb#L8 for how it plucks various things at construction time - I think this is what you're looking for.

Use the mailing list for questions, if the above doesn't work post some code of what you'd like to accomplish.

@dblock dblock closed this Apr 15, 2015

@dblock dblock reopened this Apr 17, 2015

@dblock dblock changed the title from Additional context in custom validations to Access request in custom validators. Apr 17, 2015

@suan

This comment has been minimized.

Contributor

suan commented Oct 14, 2015

@dblock Would you mind providing a more specific example of how we can get at the request from within a validator? I see that I can access the @scope variable from within my validator, but I don't see the params_scope containing any info about the actual request

(In my case, I'd like to get at the request path and headers)

EDIT: Oh, did you re-open this cause you saw that there wasn't a way to achieve this after all?

@dblock

This comment has been minimized.

Member

dblock commented Oct 14, 2015

Right @suan, I reopened it because I don't actually see how. I'd welcome someone solving it!

@glennpratt

This comment has been minimized.

Contributor

glennpratt commented Jan 7, 2016

@dblock locally I've changed the signature of Validator#validate! (with backwards compatibility using arity) to:

def validate!(params, env)

Is env too broad?

@glennpratt

This comment has been minimized.

Contributor

glennpratt commented Jan 7, 2016

Oh, I see @request includes env anyway. I'll just do that instead.

@glennpratt

This comment has been minimized.

Contributor

glennpratt commented Jan 7, 2016

So, should we replace params with request (breaking backwards compatibility) or only send request if the method arity != 1?

@glennpratt

This comment has been minimized.

Contributor

glennpratt commented Jan 8, 2016

See #1242

glennpratt added a commit to glennpratt/grape that referenced this issue Jan 8, 2016

pdrakeweb added a commit to acquia/grape that referenced this issue Jan 9, 2016

Merge pull request #1 from glennpratt/request-in-validations
Pass request to validators along with params. ruby-grape#986

glennpratt added a commit to glennpratt/grape that referenced this issue Jan 18, 2016

glennpratt added a commit to glennpratt/grape that referenced this issue Jan 18, 2016

dblock added a commit to dblock/grape that referenced this issue Jan 19, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment