Skip to content

Commit

Permalink
Added an UPGRADING note for #1295, rename message_key to message.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Mar 7, 2016
1 parent 252bfd2 commit 77ec1cd
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 15 deletions.
18 changes: 18 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@ end

See [#1283](https://github.com/ruby-grape/grape/pull/1283) for more information.

#### Changes to Grape::Exceptions::Validation parameters

When raising `Grape::Exceptions::Validation` explicitly, replace `message_key` with `message`.

For example,

```ruby
fail Grape::Exceptions::Validation, params: [:oauth_token_secret], message_key: :presence
```

becomes

```ruby
fail Grape::Exceptions::Validation, params: [:oauth_token_secret], message: :presence
```

See [#1295](https://github.com/ruby-grape/grape/pull/1295) for more information.

### Upgrading to >= 0.14.0

#### Changes to availability of DSL methods in filters
Expand Down
65 changes: 50 additions & 15 deletions spec/grape/api/custom_validations_spec.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
require 'spec_helper'

describe Grape::Validations do
before do
module CustomValidationsSpec
class DefaultLength < Grape::Validations::Base
def validate_param!(attr_name, params)
@option = params[:max].to_i if params.key?(:max)
return if params[attr_name].length <= @option
fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: "must be at the most #{@option} characters long"
end
end
class InBody < Grape::Validations::PresenceValidator
def validate(request)
validate!(request.env['api.request.body'])
context 'using a custom length validator' do
before do
module CustomValidationsSpec
class DefaultLength < Grape::Validations::Base
def validate_param!(attr_name, params)
@option = params[:max].to_i if params.key?(:max)
return if params[attr_name].length <= @option
fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: "must be at the most #{@option} characters long"
end
end
end
end
end

context 'using a custom length validator' do
subject do
Class.new(Grape::API) do
params do
Expand Down Expand Up @@ -52,6 +46,15 @@ def app
end

context 'using a custom body-only validator' do
before do
module CustomValidationsSpec
class InBody < Grape::Validations::PresenceValidator
def validate(request)
validate!(request.env['api.request.body'])
end
end
end
end
subject do
Class.new(Grape::API) do
params do
Expand All @@ -78,4 +81,36 @@ def app
expect(last_response.body).to eq 'text is missing'
end
end

context 'using a custom validator with message_key' do
before do
module CustomValidationsSpec
class WithMessageKey < Grape::Validations::PresenceValidator
def validate_param!(attr_name, _params)
fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: :presence
end
end
end
end
subject do
Class.new(Grape::API) do
params do
requires :text, with_message_key: true
end
get do
'bacon'
end
end
end

def app
subject
end

it 'fails with message' do
get '/', text: 'foobar'
expect(last_response.status).to eq 400
expect(last_response.body).to eq 'text is missing'
end
end
end

0 comments on commit 77ec1cd

Please sign in to comment.