Sha256: b3279c6729689664114ff49e2052dad4378ea99618a10a80e03f25e2e21244cc

Contents?: true

Size: 1.57 KB

Versions: 6

Compression:

Stored size: 1.57 KB

Contents

module Grape

  class API
    Boolean = Virtus::Attribute::Boolean
  end
  
  module Validations
    
    class CoerceValidator < SingleOptionValidator
      def validate_param!(attr_name, params)
        new_value = coerce_value(@option, params[attr_name])
        if valid_type?(new_value)
          params[attr_name] = new_value
        else
          raise ValidationError, :status => 400, :param => attr_name, :message => "invalid parameter: #{attr_name}"
        end
      end
  
      class InvalidValue; end
    private
    
      def _valid_array_type?(type, values)
        values.all? do |val|
          _valid_single_type?(type, val)
        end
      end
      
      def _valid_single_type?(klass, val)
        # allow nil, to ignore when a parameter is absent
        return true if val.nil?
        if klass == Virtus::Attribute::Boolean
          val.is_a?(TrueClass) || val.is_a?(FalseClass)
        elsif klass == Rack::Multipart::UploadedFile
          val.is_a?(Hashie::Mash) && val.key?(:tempfile)
        else
          val.is_a?(klass)
        end
      end
      
      def valid_type?(val)
        if @option.is_a?(Array)
          _valid_array_type?(@option[0], val)
        else
          _valid_single_type?(@option, val)
        end
      end
      
      def coerce_value(type, val)
        converter = Virtus::Attribute.build(:a, type)
        converter.coerce(val)
      
      # not the prettiest but some invalid coercion can currently trigger
      # errors in Virtus (see coerce_spec.rb)
      rescue
        InvalidValue.new
      end
      
    end
    
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
fragrant-0.0.5 vendor/bundle/ruby/1.9.1/gems/grape-0.2.2/lib/grape/validations/coerce.rb
fragrant-0.0.4 vendor/grape/lib/grape/validations/coerce.rb
grape-0.2.2 lib/grape/validations/coerce.rb
fragrant-0.0.3 vendor/grape/lib/grape/validations/coerce.rb
fragrant-0.0.2 vendor/grape/lib/grape/validations/coerce.rb
fragrant-0.0.1 vendor/grape/lib/grape/validations/coerce.rb