Sha256: d0d90c7c3259c5316de7e7c3e47848c43f42418f5f3640fe7e3e087c98ab5fcd

Contents?: true

Size: 719 Bytes

Versions: 2

Compression:

Stored size: 719 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 - values).empty?
          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

2 entries across 2 versions & 1 rubygems

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