Sha256: 1ac068dc300b6f964a6f2d8557a5927f63abc7b7ab461a68360391b75d321e3a

Contents?: true

Size: 771 Bytes

Versions: 1

Compression:

Stored size: 771 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.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_key: :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.14.0 lib/grape/validations/validators/values.rb