Skip to content

Commit 836f03b

Browse files
jmksbrunoocasali
authored andcommitted
Add utils function to warn on non-snake case attributes
1 parent 5bf72a4 commit 836f03b

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

lib/meilisearch/utils.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ def self.version_error_handler(method_name)
5252
raise e.class, message_builder(e.message, method_name)
5353
end
5454

55+
def self.warn_on_non_conforming_attribute_names(body)
56+
non_snake_case = body.keys.grep_v(/^[a-z0-9_]+$/)
57+
58+
return if non_snake_case.empty?
59+
60+
message = <<~MSG
61+
Attributes will be expected to be snake_case in future versions of MeiliSearch.
62+
63+
Non-conforming attributes: #{non_snake_case.join(', ')}
64+
MSG
65+
66+
warn(message)
67+
end
68+
5569
private_class_method :parse, :message_builder
5670
end
5771
end

spec/meilisearch/utils_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,31 @@
7171
end
7272
end.to raise_error(MeiliSearch::CommunicationError, /that `my_method` call requires/)
7373
end
74+
75+
describe '.warn_on_non_conforming_attribute_names' do
76+
it 'warns when using camelCase attributes' do
77+
attrs = { attributesToHighlight: ['field'] }
78+
79+
expect do
80+
Utils.warn_on_non_conforming_attribute_names(attrs)
81+
end.to output(include('Attributes will be expected to be snake_case', 'attributesToHighlight')).to_stderr
82+
end
83+
84+
it 'warns when using a mixed case' do
85+
attrs = { distinct_ATTribute: 'title' }
86+
87+
expect do
88+
Utils.warn_on_non_conforming_attribute_names(attrs)
89+
end.to output(include('Attributes will be expected to be snake_case', 'distinct_ATTribute')).to_stderr
90+
end
91+
92+
it 'does not warn when using snake_case' do
93+
attrs = { q: 'query', attributes_to_highlight: ['field'] }
94+
95+
expect do
96+
Utils.warn_on_non_conforming_attribute_names(attrs)
97+
end.not_to output.to_stderr
98+
end
99+
end
74100
end
75101
end

0 commit comments

Comments
 (0)