Skip to content

Commit 1100291

Browse files
committed
Add config to always include empty errors hash
1 parent a7f87fd commit 1100291

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

lib/inertia_rails/configuration.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ class Configuration
2626
# Used to detect version drift between server and client.
2727
version: nil,
2828

29-
# Allows configuring the base controller for StaticController
29+
# Allows configuring the base controller for StaticController.
3030
parent_controller: '::ApplicationController',
31+
32+
# Whether to include empty `errors` hash to the props when no errors are present.
33+
always_include_errors_hash: nil,
3134
}.freeze
3235

3336
OPTION_NAMES = DEFAULTS.keys.freeze

lib/inertia_rails/controller.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,22 @@ def inertia_configuration
144144
end
145145

146146
def inertia_shared_data
147-
initial_data = session[:inertia_errors].present? ? {errors: session[:inertia_errors]} : {}
147+
initial_data =
148+
if session[:inertia_errors].present?
149+
{ errors: session[:inertia_errors] }
150+
elsif inertia_configuration.always_include_errors_hash
151+
{ errors: {} }
152+
else
153+
if inertia_configuration.always_include_errors_hash.nil?
154+
InertiaRails.deprecator.warn(
155+
"To comply with the Inertia protocol, an empty errors hash `{errors: {}}` " \
156+
"will be included to all responses by default starting with InertiaRails 4.0. " \
157+
"To opt-in now, set `config.always_include_errors_hash = true`. " \
158+
"To disable this warning, set it to `false`."
159+
)
160+
end
161+
{}
162+
end
148163

149164
self.class._inertia_shared_data.filter_map { |shared_data|
150165
if shared_data.respond_to?(:call)

0 commit comments

Comments
 (0)