Skip to content

Commit 944f724

Browse files
committed
Ensure validation errors use correct formatter
Previously all validation errors returned to the client included keys formatted in the ruby underscore style. To ensure clients receive consistently formatted keys they should be formatted in the error message before responding.
1 parent 2cc9e61 commit 944f724

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

lib/jsonapi/exceptions.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,11 @@ class ValidationErrors < Error
284284
attr_accessor :messages
285285
def initialize(messages)
286286
@messages = messages
287+
@key_formatter = JSONAPI.configuration.key_formatter
288+
end
289+
290+
def format_key(key)
291+
@key_formatter.format(key)
287292
end
288293

289294
def errors
@@ -292,7 +297,7 @@ def errors
292297
element[1].map do |message|
293298
JSONAPI::Error.new(code: JSONAPI::VALIDATION_ERROR,
294299
status: :unprocessable_entity,
295-
title: "#{element[0]} - #{message}",
300+
title: "#{format_key(element[0])} - #{message}",
296301
detail: message,
297302
path: "/#{element[0]}")
298303
end

test/controllers/controller_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1662,7 +1662,7 @@ def test_create_validations_missing_attribute
16621662
assert_equal 2, json_response['errors'].size
16631663
assert_equal JSONAPI::VALIDATION_ERROR, json_response['errors'][0]['code']
16641664
assert_equal JSONAPI::VALIDATION_ERROR, json_response['errors'][1]['code']
1665-
assert_match /date_joined - can't be blank/, response.body
1665+
assert_match /dateJoined - can't be blank/, response.body
16661666
assert_match /name - can't be blank/, response.body
16671667
end
16681668

0 commit comments

Comments
 (0)