Skip to content

Commit f5cb05b

Browse files
committed
Add specs and docs for always_include_errors_hash
1 parent ac5fc7e commit f5cb05b

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

docs/guide/configuration.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,19 @@ forcing a full page visit instead of an XHR visit on the next request.
103103

104104
See [assets versioning](/guide/asset-versioning).
105105

106+
### `always_include_errors_hash`
107+
108+
**Default**: `nil`
109+
**ENV**: `INERTIA_ALWAYS_INCLUDE_ERRORS_HASH`
110+
111+
@available_since rails=master
112+
113+
Whether to include an empty `errors` hash in the props when no validation errors are present.
114+
115+
When set to `true`, an empty `errors: {}` object will always be included in Inertia responses. When set to `false`, the `errors` key will be omitted when there are no errors. The default value `nil` currently behaves like `false` but shows a deprecation warning.
116+
117+
The default value will be changed to `true` in the next major version.
118+
106119
### `parent_controller`
107120

108121
**Default**: `'::ApplicationController'`

spec/inertia/error_sharing_spec.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,57 @@
11
RSpec.describe 'errors shared automatically', type: :request do
2+
context 'always_include_errors_hash configuration' do
3+
let(:server_version) { 1.0 }
4+
let(:headers) { { 'X-Inertia' => true } }
5+
6+
after { InertiaRails.configure { |c| c.always_include_errors_hash = nil } }
7+
8+
context 'when always_include_errors_hash is true' do
9+
before { InertiaRails.configure { |c| c.always_include_errors_hash = true } }
10+
11+
it 'includes empty errors hash when no errors present' do
12+
get empty_test_path, headers: headers
13+
expect(response.body).to include({ errors: {} }.to_json)
14+
end
15+
16+
it 'still includes actual errors when they exist' do
17+
post redirect_with_inertia_errors_path, headers: headers
18+
get response.headers['Location'], headers: headers
19+
expect(response.body).to include({ errors: { uh: 'oh' } }.to_json)
20+
end
21+
end
22+
23+
context 'when always_include_errors_hash is false' do
24+
before { InertiaRails.configure { |c| c.always_include_errors_hash = false } }
25+
26+
it 'does not include errors hash when no errors present' do
27+
get empty_test_path, headers: headers
28+
expect(response.body).not_to include('"errors"')
29+
end
30+
31+
it 'still includes actual errors when they exist' do
32+
post redirect_with_inertia_errors_path, headers: headers
33+
get response.headers['Location'], headers: headers
34+
expect(response.body).to include({ errors: { uh: 'oh' } }.to_json)
35+
end
36+
end
37+
38+
context 'when always_include_errors_hash is nil (default)' do
39+
before { InertiaRails.configure { |c| c.always_include_errors_hash = nil } }
40+
41+
it 'shows deprecation warning and does not include empty errors hash' do
42+
expect { get empty_test_path, headers: headers }
43+
.to output(/To comply with the Inertia protocol/).to_stderr
44+
expect(response.body).not_to include('"errors"')
45+
end
46+
47+
it 'still includes actual errors when they exist' do
48+
post redirect_with_inertia_errors_path, headers: headers
49+
get response.headers['Location'], headers: headers
50+
expect(response.body).to include({ errors: { uh: 'oh' } }.to_json)
51+
end
52+
end
53+
end
54+
255
context 'rendering errors across redirects' do
356
let(:server_version){ 1.0 }
457
let(:headers){ { 'X-Inertia' => true, 'X-Inertia-Version' => server_version } }

0 commit comments

Comments
 (0)