Skip to content

Commit f4da70c

Browse files
committed
Merge pull request #336 from Azdaroth/fix-validation-message-attribute-format
fix validation error message attribute format
2 parents 91d0a1a + a3f627b commit f4da70c

3 files changed

Lines changed: 39 additions & 6 deletions

File tree

lib/jsonapi/exceptions.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,11 @@ def json_api_error(attr_key, message)
323323
end
324324

325325
def pointer(attr_or_relationship_name)
326+
formatted_attr_or_relationship_name = format_key(attr_or_relationship_name)
326327
if resource_relationships.include?(attr_or_relationship_name)
327-
"/data/relationships/#{attr_or_relationship_name}"
328+
"/data/relationships/#{formatted_attr_or_relationship_name}"
328329
else
329-
"/data/attributes/#{attr_or_relationship_name}"
330+
"/data/attributes/#{formatted_attr_or_relationship_name}"
330331
end
331332
end
332333
end

test/controllers/controller_test.rb

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2159,11 +2159,8 @@ def test_create_simple_namespaced
21592159
end
21602160

21612161
class FactsControllerTest < ActionController::TestCase
2162-
def setup
2163-
JSONAPI.configuration.json_key_format = :camelized_key
2164-
end
2165-
21662162
def test_type_formatting
2163+
JSONAPI.configuration.json_key_format = :camelized_key
21672164
get :show, {id: '1'}
21682165
assert_response :success
21692166
assert json_response['data'].is_a?(Hash)
@@ -2177,6 +2174,40 @@ def test_type_formatting
21772174
assert_equal 'abc', json_response['data']['attributes']['photo']
21782175
assert_equal false, json_response['data']['attributes']['cool']
21792176
end
2177+
2178+
def test_create_with_invalid_data
2179+
JSONAPI.configuration.json_key_format = :dasherized_key
2180+
set_content_type_header!
2181+
post :create,
2182+
{
2183+
data: {
2184+
type: 'facts',
2185+
attributes: {
2186+
bio: '',
2187+
:"quality-rating" => '',
2188+
:"spouse-name" => '',
2189+
salary: 100000,
2190+
:"date-time-joined" => '',
2191+
birthday: '',
2192+
bedtime: '',
2193+
photo: 'abc',
2194+
cool: false
2195+
},
2196+
relationships: {
2197+
}
2198+
}
2199+
}
2200+
2201+
assert_response :unprocessable_entity
2202+
2203+
assert_equal "/data/attributes/spouse-name", json_response['errors'][0]['source']['pointer']
2204+
assert_equal "can't be blank", json_response['errors'][0]['detail']
2205+
assert_equal "spouse-name - can't be blank", json_response['errors'][0]['title']
2206+
2207+
assert_equal "/data/attributes/bio", json_response['errors'][1]['source']['pointer']
2208+
assert_equal "can't be blank", json_response['errors'][1]['detail']
2209+
assert_equal "bio - can't be blank", json_response['errors'][1]['title']
2210+
end
21802211
end
21812212

21822213
class Api::V2::BooksControllerTest < ActionController::TestCase

test/fixtures/active_record.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ class Preferences < ActiveRecord::Base
303303
end
304304

305305
class Fact < ActiveRecord::Base
306+
validates :spouse_name, :bio, presence: true
306307
end
307308

308309
class Like < ActiveRecord::Base

0 commit comments

Comments
 (0)