diff --git a/lib/jsonapi/filtering.rb b/lib/jsonapi/filtering.rb index 70d47db..1079506 100644 --- a/lib/jsonapi/filtering.rb +++ b/lib/jsonapi/filtering.rb @@ -17,6 +17,9 @@ def self.extract_attributes_and_predicates(requested_field) .detect_and_strip_from_string!(field_name) predicates << Ransack::Predicate.named(predicate) end + unless predicates.present? || Ransack.options[:ignore_unknown_conditions] + raise ArgumentError, "No valid predicates for #{requested_field}" + end [field_name.split(/_and_|_or_/), predicates.reverse] end diff --git a/spec/filtering_spec.rb b/spec/filtering_spec.rb index f3bd80a..63a8d0f 100644 --- a/spec/filtering_spec.rb +++ b/spec/filtering_spec.rb @@ -22,6 +22,30 @@ expect(predicates[1].name).to eq('eq') end end + + context 'with an invalidate predicate' do + context 'with default configuration' do + it 'does not return any predicates' do + attributes, predicates = JSONAPI::Filtering + .extract_attributes_and_predicates('attr1_eqx') + expect(attributes).to eq(['attr1_eqx']) + expect(predicates.size).to eq(0) + end + end + context 'with ignore_unknown_conditions as false' do + before do + Ransack.configure { |c| c.ignore_unknown_conditions = false } + end + after do + Ransack.configure { |c| c.ignore_unknown_conditions = true } + end + it 'raises an error' do + expect do + JSONAPI::Filtering.extract_attributes_and_predicates('attr1_eqx') + end.to raise_error ArgumentError + end + end + end end describe 'GET /users' do