Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
allow additional colon : separator in sort_by query string if fallbac…
…k exists
  • Loading branch information
iaintshine committed Jul 29, 2015
1 parent 2ced483 commit 409d460
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
8 changes: 7 additions & 1 deletion lib/input_sanitizer/v2/types.rb
Expand Up @@ -132,6 +132,12 @@ def call(value, options = {})
key, direction = split(value)
direction = 'asc' if direction.blank?

# special case when fallback takes care of separator sanitization e.g. custom fields
if options[:fallback] && !allowed_directions.include?(direction)
direction = 'asc'
key = value
end

unless valid?(key, direction, options)
raise InputSanitizer::ValueNotAllowedError.new(value)
end
Expand All @@ -157,7 +163,7 @@ def split(value)
def check_options!(options)
fallback = options[:fallback]
if fallback && !fallback.respond_to?(:call)
raise ArgumentError, ":fallback option must be a proc"
raise ArgumentError, ":fallback option must respond to method :call (proc, lambda etc)"
end
end

Expand Down
26 changes: 19 additions & 7 deletions spec/v2/query_sanitizer_spec.rb
@@ -1,5 +1,13 @@
require 'spec_helper'

class CustomFieldsSortByQueryFallback
def self.call(key, direction, context)
filterable_keys = %w(slt number)
_, field = key.split(':', 2)
filterable_keys.include?(field)
end
end

class TestedQuerySanitizer < InputSanitizer::V2::QuerySanitizer
string :status, :allow => ['', 'current', 'past']

Expand All @@ -11,7 +19,7 @@ class TestedQuerySanitizer < InputSanitizer::V2::QuerySanitizer

integer :ids, :collection => true
string :tags, :collection => true
sort_by %w(name updated_at created_at), :default => 'name:asc', :fallback => Proc.new { |key, direction| key == 'custom_field' }
sort_by %w(name updated_at created_at), :default => 'name:asc', :fallback => CustomFieldsSortByQueryFallback
end

class ContextQuerySanitizer < InputSanitizer::V2::QuerySanitizer
Expand Down Expand Up @@ -196,23 +204,27 @@ class ContextForwardingSanitizer < InputSanitizer::V2::PayloadSanitizer
end

it "bails to fallback" do
@params = { :sort_by => 'custom_field' }
@params = { :sort_by => 'custom_field:slt:asc' }
sanitizer.should be_valid
sanitizer[:sort_by].should eq(["custom_field", "asc"])
sanitizer[:sort_by].should eq(["custom_field:slt", "asc"])
end

[
['name', true, ["name", "asc"]],
['name:asc', true, ["name", "asc"]],
['name:desc', true, ["name", "desc"]],
['name:', true, ["name", "asc"]],
['custom_field', true, ['custom_field', 'asc']],
['custom_field:asc', true, ['custom_field', 'asc']],
['custom_field:desc', true, ['custom_field', 'desc']],
['custom_field:', true, ['custom_field', 'asc']],
['custom_field:slt', true, ['custom_field:slt', 'asc']],
['custom_field:slt:', true, ['custom_field:slt', 'asc']],
['custom_field:slt:asc', true, ['custom_field:slt', 'asc']],
['custom_field:slt:desc', true, ['custom_field:slt', 'desc']],
['unknown', false, nil],
['name:invalid', false, nil],
['custom_field', false, nil],
['custom_field:', false, nil],
['custom_field:invalid', false, nil],
['custom_field:invalid:asc', false, nil],
['custom_field:invalid:desc', false, nil],
['custom_field2', false, nil]
].each do |sort_param, valid, expectation|
it "sort by #{sort_param} and returns #{valid}" do
Expand Down

0 comments on commit 409d460

Please sign in to comment.