Sha256: ceca60089068b84f0a153107bef806437fe922a5fdd94452ae8d8748600a0b6a

Contents?: true

Size: 741 Bytes

Versions: 3

Compression:

Stored size: 741 Bytes

Contents

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

      def validate_param!(attr_name, params)
        return unless params[attr_name] || required_for_root_scope?

        values = @values.is_a?(Proc) ? @values.call : @values
        param_array = params[attr_name].nil? ? [nil] : Array.wrap(params[attr_name])
        unless param_array.all? { |param| values.include?(param) }
          fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message_key: :values
        end
      end

      private

      def required_for_root_scope?
        @required && @scope.root?
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
grape-0.13.0 lib/grape/validations/validators/values.rb
grape-0.12.0 lib/grape/validations/validators/values.rb
grape-0.11.0 lib/grape/validations/validators/values.rb