Sha256: 39b31b8f57a600e81ed1d1a888f847e3f6d52e60518a6591d06576a921878dd6

Contents?: true

Size: 773 Bytes

Versions: 3

Compression:

Stored size: 773 Bytes

Contents

module Grape
  module Validations
    class DefaultValidator < Base
      def initialize(attrs, options, required, scope)
        @default = options
        super
      end

      def validate_param!(attr_name, params)
        params[attr_name] = @default.is_a?(Proc) ? @default.call : @default unless params.key?(attr_name)
      end

      def validate!(params)
        attrs = AttributesIterator.new(self, @scope, params)
        parent_element = @scope.element
        attrs.each do |resource_params, attr_name|
          if resource_params[attr_name].nil?
            validate_param!(attr_name, resource_params)
            params[parent_element] = resource_params if parent_element && params[parent_element].nil?
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
grape-0.11.0 lib/grape/validations/validators/default.rb
grape-0.10.1 lib/grape/validations/validators/default.rb
grape-0.10.0 lib/grape/validations/validators/default.rb