• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ruby-grape / grape / 12435856714

20 Dec 2024 05:34PM UTC coverage: 98.335% (+0.5%) from 97.809%
12435856714

Pull #2516

github

web-flow
Merge 44784638b into 30b3a4377
Pull Request #2516: Dynamic registration

191 of 196 new or added lines in 26 files covered. (97.45%)

1 existing line in 1 file now uncovered.

3544 of 3604 relevant lines covered (98.34%)

62105.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.56
/lib/grape/validations/validators/base.rb
1
# frozen_string_literal: true
2

3
module Grape
51✔
4
  module Validations
51✔
5
    module Validators
51✔
6
      class Base
51✔
7
        attr_reader :attrs
51✔
8

9
        # Creates a new Validator from options specified
10
        # by a +requires+ or +optional+ directive during
11
        # parameter definition.
12
        # @param attrs [Array] names of attributes to which the Validator applies
13
        # @param options [Object] implementation-dependent Validator options
14
        # @param required [Boolean] attribute(s) are required or optional
15
        # @param scope [ParamsScope] parent scope for this Validator
16
        # @param opts [Hash] additional validation options
17
        def initialize(attrs, options, required, scope, opts)
51✔
18
          @attrs = Array(attrs)
139,686✔
19
          @option = options
139,686✔
20
          @required = required
139,686✔
21
          @scope = scope
139,686✔
22
          @fail_fast = opts[:fail_fast]
139,686✔
23
          @allow_blank = opts[:allow_blank]
139,686✔
24
        end
25

26
        # Validates a given request.
27
        # @note Override #validate! unless you need to access the entire request.
28
        # @param request [Grape::Request] the request currently being handled
29
        # @raise [Grape::Exceptions::Validation] if validation failed
30
        # @return [void]
31
        def validate(request)
51✔
32
          return unless @scope.should_validate?(request.params)
138,915✔
33

34
          validate!(request.params)
114,467✔
35
        end
36

37
        # Validates a given parameter hash.
38
        # @note Override #validate if you need to access the entire request.
39
        # @param params [Hash] parameters to validate
40
        # @raise [Grape::Exceptions::Validation] if validation failed
41
        # @return [void]
42
        def validate!(params)
51✔
43
          attributes = SingleAttributeIterator.new(self, @scope, params)
105,226✔
44
          # we collect errors inside array because
45
          # there may be more than one error per field
46
          array_errors = []
105,226✔
47

48
          attributes.each do |val, attr_name, empty_val|
105,226✔
49
            next if !@scope.required? && empty_val
113,746✔
50
            next unless @scope.meets_dependency?(val, params)
113,746✔
51

52
            validate_param!(attr_name, val) if @required || (val.respond_to?(:key?) && val.key?(attr_name))
113,466✔
53
          rescue Grape::Exceptions::Validation => e
54
            array_errors << e
15,401✔
55
          end
56

57
          raise Grape::Exceptions::ValidationArrayErrors.new(array_errors) if array_errors.any?
105,226✔
58
        end
59

60
        def self.inherited(klass)
51✔
61
          super
1,987✔
62
          return if klass.name.blank?
1,987✔
63

64
          short_validator_name = klass.name.demodulize.underscore.delete_suffix('_validator')
816✔
65
          Validations.register(short_validator_name, klass)
816✔
66
        end
67

68
        def message(default_key = nil)
51✔
69
          options = instance_variable_get(:@option)
16,601✔
70
          options_key?(:message) ? options[:message] : default_key
16,601✔
71
        end
72

73
        def options_key?(key, options = nil)
51✔
74
          options = instance_variable_get(:@option) if options.nil?
25,001✔
75
          options.respond_to?(:key?) && options.key?(key) && !options[key].nil?
25,001✔
76
        end
77

78
        def fail_fast?
51✔
79
          @fail_fast
17,446✔
80
        end
81
      end
82
    end
83
  end
84
end
85

86
Grape::Validations::Base = Class.new(Grape::Validations::Validators::Base) do
51✔
87
  def self.inherited(*)
51✔
88
    Grape.deprecator.warn 'Grape::Validations::Base is deprecated! Use Grape::Validations::Validators::Base instead.'
40✔
UNCOV
89
    super
×
90
  end
91
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc