Skip to content
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

Ability to fail fast on params validation #1423

Closed
akozhemiakin opened this issue Jun 13, 2016 · 4 comments
Closed

Ability to fail fast on params validation #1423

akozhemiakin opened this issue Jun 13, 2016 · 4 comments

Comments

@akozhemiakin
Copy link

Correct me if I'm wrong, but currently all validators will be executed even if some of them fails during the validation process. I believe this is the corresponding code chunk, showing the nature of this behavior:

def run_validators(validators, request)
  validation_errors = []

  validators.each do |validator|
    begin
      validator.validate(request)
    rescue Grape::Exceptions::Validation => e
      validation_errors << e
    end
  end

  validation_errors.any? && raise(Grape::Exceptions::ValidationErrors, errors: validation_errors, headers: header)
end

I believe that it should be possible to instruct Grape to fail fast during the validation process.

One possible case: We need to validate that some string matches some pattern (for example it is an email) and than we need to validate if this email is in the white list. For now even if this email is malformed, it will be checked against the white list (which has no sense cause it is malformed anyway).

The DSL may look like this:

params do
  requires :email, allow_blank: false, regexp: /@/, fail_fast: true
end
@dblock
Copy link
Member

dblock commented Jun 14, 2016

I like this idea as long as we ensure the validation order.

@connorshea
Copy link

Is this resolved now?

@dgasper
Copy link
Contributor

dgasper commented Oct 7, 2016

Yes, should probably be closed.

@dblock
Copy link
Member

dblock commented Oct 7, 2016

Closing via #1499.

@dblock dblock closed this as completed Oct 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants