Sha256: bf9c93db188705083950177ec8ec9f2e5e5fd363c53994ebfc2b06ab880ef04c

Contents?: true

Size: 828 Bytes

Versions: 3

Compression:

Stored size: 828 Bytes

Contents

module Grape
  module Validations
    class ValuesValidator < Base
      def initialize(attrs, options, required, scope)
        @values = (options_key?(:value, options) ? options[:value] : options)
        super
      end

      def validate_param!(attr_name, params)
        return unless params.is_a?(Hash)
        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])
        return if param_array.all? { |param| values.include?(param) }
        fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: message(:values)
      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.16.2 lib/grape/validations/validators/values.rb
grape-0.16.1 lib/grape/validations/validators/values.rb
grape-0.15.0 lib/grape/validations/validators/values.rb