Skip to content

Commit 85ed0ff

Browse files
committed
Merge pull request #1063 from yairgo/pass_headers_to_validation_errors
Adding headers to validation errors
2 parents a7879d0 + f95445a commit 85ed0ff

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Next Release
33

44
#### Features
55

6+
* [#1062](https://github.com/intridea/grape/issues/1062): Fix: `Grape::Exceptions::ValidationErrors` will include headers set by `header` - [@yairgo](https://github.com/yairgo).
67
* [#1039](https://github.com/intridea/grape/pull/1039): Added support for custom parameter types - [@rnubel](https://github.com/rnubel).
78
* [#1047](https://github.com/intridea/grape/pull/1047): Adds `given` to DSL::Parameters, allowing for dependent params - [@rnubel](https://github.com/rnubel).
89
* [#1064](https://github.com/intridea/grape/pull/1064): Add public `Grape::Exception::ValidationErrors#full_messages` - [@romanlehnert](https://github.com/romanlehnert).

lib/grape/endpoint.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def run(env)
239239
end
240240

241241
if validation_errors.any?
242-
fail Grape::Exceptions::ValidationErrors, errors: validation_errors
242+
fail Grape::Exceptions::ValidationErrors, errors: validation_errors, headers: header
243243
end
244244

245245
run_filters after_validations

lib/grape/exceptions/validation_errors.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ def initialize(args = {})
1313
@errors[validation_error.params] ||= []
1414
@errors[validation_error.params] << validation_error
1515
end
16-
super message: full_messages.join(', '), status: 400
16+
17+
super message: full_messages.join(', '), status: 400, headers: args[:headers]
1718
end
1819

1920
def each

spec/grape/endpoint_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,4 +941,25 @@ def memoized
941941
expect(last_response.body).to eq File.read(__FILE__)
942942
end
943943
end
944+
945+
context 'validation errors' do
946+
before do
947+
subject.before do
948+
header['Access-Control-Allow-Origin'] = '*'
949+
end
950+
subject.params do
951+
requires :id, type: String
952+
end
953+
subject.get do
954+
'should not get here'
955+
end
956+
end
957+
958+
it 'returns the errors, and passes headers' do
959+
get '/'
960+
expect(last_response.status).to eq 400
961+
expect(last_response.body).to eq 'id is missing'
962+
expect(last_response.headers['Access-Control-Allow-Origin']).to eq('*')
963+
end
964+
end
944965
end

spec/grape/exceptions/validation_errors_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@
55
let(:validation_message) { 'FooBar is invalid' }
66
let(:validation_error) { OpenStruct.new(params: [validation_message]) }
77

8+
context 'initialize' do
9+
let(:headers) {
10+
{
11+
'A-Header-Key' => 'A-Header-Value'
12+
}
13+
}
14+
15+
subject do
16+
described_class.new(errors: [validation_error], headers: headers)
17+
end
18+
19+
it 'should assign headers through base class' do
20+
expect(subject.headers).to eq(headers)
21+
end
22+
end
23+
824
context 'message' do
925
context 'is not repeated' do
1026
let(:error) do

0 commit comments

Comments
 (0)