Sha256: 49073d9810cb2fc3b0466f6fd95cba7fd4c1b11207389baa427d76d8086b5a6a

Contents?: true

Size: 829 Bytes

Versions: 1

Compression:

Stored size: 829 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) }
        raise 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

1 entries across 1 versions & 1 rubygems

Version Path
grape-0.17.0 lib/grape/validations/validators/values.rb