Sha256: 601fa455af1f65c62448010ef73c5aa9063a934ddccc1d6c5f69a3cd5c9fd6d8

Contents?: true

Size: 1.12 KB

Versions: 5

Compression:

Stored size: 1.12 KB

Contents

class InputSanitizer::V2::CleanPayloadCollectionField < MethodStruct.new(:data, :converter, :collection, :options)
  def call
    return nil if options[:allow_nil] && data == nil

    validate_type
    validate_size

    result, errors = [], {}

    data.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 validate_type
    unless data.respond_to?(:to_ary)
      raise InputSanitizer::TypeMismatchError.new(data, :array)
    end
  end

  def validate_size
    if collection.respond_to?(:fetch)
      if collection[:minimum] && data.length < collection[:minimum]
        raise InputSanitizer::CollectionLengthError.new(data.length, collection[:minimum], collection[:maximum])
      elsif collection[:maximum] && data.length > collection[:maximum]
        raise InputSanitizer::CollectionLengthError.new(data.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_payload_collection_field.rb
input_sanitizer-0.5.0 lib/input_sanitizer/v2/clean_payload_collection_field.rb
input_sanitizer-0.4.1 lib/input_sanitizer/v2/clean_payload_collection_field.rb
input_sanitizer-0.4.0 lib/input_sanitizer/v2/clean_payload_collection_field.rb
input_sanitizer-0.3.33 lib/input_sanitizer/v2/clean_payload_collection_field.rb