Sha256: f61ac104afde236a2f4847aeed96683de285cbcd011cc8018a2407a8fee02f5e

Contents?: true

Size: 1.08 KB

Versions: 5

Compression:

Stored size: 1.08 KB

Contents

class InputSanitizer::V2::CleanQueryCollectionField < MethodStruct.new(:data, :converter, :collection, :options)
  def call
    validate_type
    validate_size

    result, errors = [], {}
    items.each_with_index do |value, idx|
      begin
        result << converter.call(value, options)
      rescue InputSanitizer::ValidationError => e
        errors[idx] = e
      end
    end

    if errors.any?
      raise InputSanitizer::CollectionError.new(errors)
    else
      result
    end
  end

  private
  def items
    @items ||= data.to_s.split(',')
  end

  def validate_type
    InputSanitizer::V2::Types::StringCheck.new.call(data)
  end

  def validate_size
    if collection.respond_to?(:fetch)
      if collection[:minimum] && items.length < collection[:minimum]
        raise InputSanitizer::CollectionLengthError.new(items.length, collection[:minimum], collection[:maximum])
      elsif collection[:maximum] && items.length > collection[:maximum]
        raise InputSanitizer::CollectionLengthError.new(items.length, collection[:minimum], collection[:maximum])
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
input_sanitizer-0.6.0 lib/input_sanitizer/v2/clean_query_collection_field.rb
input_sanitizer-0.5.0 lib/input_sanitizer/v2/clean_query_collection_field.rb
input_sanitizer-0.4.1 lib/input_sanitizer/v2/clean_query_collection_field.rb
input_sanitizer-0.4.0 lib/input_sanitizer/v2/clean_query_collection_field.rb
input_sanitizer-0.3.33 lib/input_sanitizer/v2/clean_query_collection_field.rb