Skip to content

Commit e5b9422

Browse files
committed
Merge pull request #239 from adamgotterer/validation
Validation updates: call `before` before validation / added param value when raising ValidationError
2 parents eca3327 + aab18eb commit e5b9422

File tree

7 files changed

+32
-10
lines changed

7 files changed

+32
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ tmtags
1616

1717
## VIM
1818
*.swp
19+
*.swo
1920

2021
## RUBYMINE
2122
.idea

README.markdown

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ end
214214

215215
## Parameter Validation and Coercion
216216

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

219219
```ruby
220220
params do
@@ -260,22 +260,22 @@ end
260260

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

272272
```ruby
273273
params do
274-
requires :name, :doit => true
274+
requires :username, :alpha_numeric => true
275275
end
276276
```
277277

278-
You can also create custom classes that take additional parameters
278+
You can also create custom classes that take parameters
279279
```ruby
280280
class Length < Grape::Validations::SingleOptionValidator
281281
def validate_param!(attr_name, params)
@@ -292,6 +292,20 @@ params do
292292
end
293293
```
294294

295+
### Validation Errors
296+
When validation and coercion erros occur an exception of type `ValidationError` is raised.
297+
If the exception goes uncaught it will respond with a status of 400 and an error message.
298+
You can rescue a `ValidationError` and respond with a custom response.
299+
```ruby
300+
rescue_from ValidationError do |e|
301+
Rack::Response.new({
302+
'status' => e.status,
303+
'message' => e.message,
304+
'param' => e.param
305+
}.to_json, e.status)
306+
end
307+
```
308+
295309

296310

297311
## Headers

lib/grape/endpoint.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,12 @@ def run(env)
293293
self.extend helpers
294294
cookies.read(@request)
295295

296+
run_filters befores
297+
296298
Array(settings[:validations]).each do |validator|
297299
validator.validate!(params)
298300
end
299301

300-
run_filters befores
301302
response_text = instance_eval &self.block
302303
run_filters afters
303304
cookies.write(header)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
require 'grape/exceptions/base'
22

33
class ValidationError < Grape::Exceptions::Base
4+
attr_accessor :param
5+
6+
def initialize(args = {})
7+
@param = args[:param].to_s if args.has_key? :param
8+
super
9+
end
410
end

lib/grape/validations/coerce.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def validate_param!(attr_name, params)
1212
if valid_type?(new_value)
1313
params[attr_name] = new_value
1414
else
15-
raise ValidationError, :status => 400, :message => "invalid parameter: #{attr_name}"
15+
raise ValidationError, :status => 400, :param => attr_name, :message => "invalid parameter: #{attr_name}"
1616
end
1717
end
1818

lib/grape/validations/presence.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Validations
33
class PresenceValidator < Validator
44
def validate_param!(attr_name, params)
55
unless params.has_key?(attr_name)
6-
raise ValidationError, :status => 400, :message => "missing parameter: #{attr_name}"
6+
raise ValidationError, :status => 400, :param => attr_name, :message => "missing parameter: #{attr_name}"
77
end
88
end
99
end

lib/grape/validations/regexp.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Validations
44
class RegexpValidator < SingleOptionValidator
55
def validate_param!(attr_name, params)
66
if params[attr_name] && !( params[attr_name].to_s =~ @option )
7-
raise ValidationError, :status => 400, :message => "invalid parameter: #{attr_name}"
7+
raise ValidationError, :status => 400, :param => attr_name, :message => "invalid parameter: #{attr_name}"
88
end
99
end
1010
end

0 commit comments

Comments
 (0)