File: same_as.rb

package info (click to toggle)
ruby-grape 1.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,156 kB
  • sloc: ruby: 25,265; makefile: 7
file content (29 lines) | stat: -rw-r--r-- 734 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# frozen_string_literal: true

module Grape
  module Validations
    module Validators
      class SameAsValidator < Base
        def validate_param!(attr_name, params)
          confirmation = options_key?(:value) ? @option[:value] : @option
          return if params[attr_name] == params[confirmation]

          raise Grape::Exceptions::Validation.new(
            params: [@scope.full_name(attr_name)],
            message: build_message
          )
        end

        private

        def build_message
          if options_key?(:message)
            @option[:message]
          else
            format I18n.t(:same_as, scope: 'grape.errors.messages'), parameter: @option
          end
        end
      end
    end
  end
end