Skip to content

Validation updates: call before before validation / added param value when raising ValidationError #239

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

Merged
merged 5 commits into from
Sep 6, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ tmtags

## VIM
*.swp
*.swo

## RUBYMINE
.idea
Expand Down
26 changes: 20 additions & 6 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ end

## Parameter Validation and Coercion

You can define validations and coercion options for your parameters using `params`.
You can define validations and coercion options for your parameters using a `params` block.

```ruby
params do
Expand Down Expand Up @@ -260,22 +260,22 @@ end

### Custom Validators
```ruby
class doit < Grape::Validations::Validator
class AlphaNumeric < Grape::Validations::Validator
def validate_param!(attr_name, params)
unless params[attr_name] == 'im custom'
throw :error, :status => 400, :message => "#{attr_name}: is not custom!"
unless params[attr_name] =~ /^[[:alnum:]]+$/
throw :error, :status => 400, :message => "#{attr_name}: must consist of alpha-numeric characters"
end
end
end
```

```ruby
params do
requires :name, :doit => true
requires :username, :alpha_numeric => true
end
```

You can also create custom classes that take additional parameters
You can also create custom classes that take parameters
```ruby
class Length < Grape::Validations::SingleOptionValidator
def validate_param!(attr_name, params)
Expand All @@ -292,6 +292,20 @@ params do
end
```

### Validation Errors
When validation and coercion erros occur an exception of type `ValidationError` is raised.
If the exception goes uncaught it will respond with a status of 400 and an error message.
You can rescue a `ValidationError` and respond with a custom response.
```ruby
rescue_from ValidationError do |e|
Rack::Response.new({
'status' => e.status,
'message' => e.message,
'param' => e.param
}.to_json, e.status)
end
```



## Headers
Expand Down
3 changes: 2 additions & 1 deletion lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,12 @@ def run(env)
self.extend helpers
cookies.read(@request)

run_filters befores

Array(settings[:validations]).each do |validator|
validator.validate!(params)
end

run_filters befores
response_text = instance_eval &self.block
run_filters afters
cookies.write(header)
Expand Down
6 changes: 6 additions & 0 deletions lib/grape/exceptions/validation_error.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
require 'grape/exceptions/base'

class ValidationError < Grape::Exceptions::Base
attr_accessor :param

def initialize(args = {})
@param = args[:param].to_s if args.has_key? :param
super
end
end
2 changes: 1 addition & 1 deletion lib/grape/validations/coerce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def validate_param!(attr_name, params)
if valid_type?(new_value)
params[attr_name] = new_value
else
raise ValidationError, :status => 400, :message => "invalid parameter: #{attr_name}"
raise ValidationError, :status => 400, :param => attr_name, :message => "invalid parameter: #{attr_name}"
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/presence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Validations
class PresenceValidator < Validator
def validate_param!(attr_name, params)
unless params.has_key?(attr_name)
raise ValidationError, :status => 400, :message => "missing parameter: #{attr_name}"
raise ValidationError, :status => 400, :param => attr_name, :message => "missing parameter: #{attr_name}"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Validations
class RegexpValidator < SingleOptionValidator
def validate_param!(attr_name, params)
if params[attr_name] && !( params[attr_name].to_s =~ @option )
raise ValidationError, :status => 400, :message => "invalid parameter: #{attr_name}"
raise ValidationError, :status => 400, :param => attr_name, :message => "invalid parameter: #{attr_name}"
end
end
end
Expand Down