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

ruby-grape / grape / 25973567226

16 May 2026 09:36PM UTC coverage: 96.968% (+0.001%) from 96.967%
25973567226

push

github

web-flow
Encapsulate Grape::Validations::Validators::Base state behind readers (#2725)

Route all internal reads of the Base validator's instance variables
through accessor methods instead of touching @ivars directly, in Base
and every subclass.

- Add private `attr_reader :options, :scope, :required, :allow_blank,
  :exception_message`; `:attrs` stays a public reader (unchanged
  external behavior).
- Booleans get `?` aliases consistent with `fail_fast?`:
  `required?`, `allow_blank?`, `fail_fast?` (now `attr_reader` + alias).
- Only `attrs`/`fail_fast?` remain public — the readers actually
  crossing the validator boundary (Grape::Endpoint#run_validators).
- Rename locals that shadowed the new readers (`attrs`/`scope`).

Pure internal refactor: no behavior change, full validation and
endpoint suites green.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

1084 of 1169 branches covered (92.73%)

Branch coverage included in aggregate %.

38 of 39 new or added lines in 9 files covered. (97.44%)

3522 of 3581 relevant lines covered (98.35%)

31690.47 hits per line

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

92.31
/lib/grape/validations/validators/default_validator.rb
1
# frozen_string_literal: true
2

3
module Grape
37✔
4
  module Validations
37✔
5
    module Validators
37✔
6
      class DefaultValidator < Base
37✔
7
        def initialize(attrs, options, required, scope, opts)
37✔
8
          super
5,025✔
9
          # !important, lazy call at runtime
10
          @default_call =
11
            if options.is_a?(Proc)
5,025✔
12
              options.arity.zero? ? proc { options.call } : options
384✔
13
            elsif options.duplicable?
4,801!
14
              proc { options.dup }
7,746✔
15
            else
NEW
16
              proc { options }
×
17
            end
18
        end
19

20
        def validate!(params)
37✔
21
          attributes = SingleAttributeIterator.new(attrs, scope, params)
4,449✔
22
          attributes.each do |resource_params, attr_name|
4,449✔
23
            next unless scope.meets_dependency?(resource_params, params)
4,609✔
24

25
            resource_params[attr_name] = @default_call.call(resource_params) if hash_like?(resource_params) && resource_params[attr_name].nil?
4,513✔
26
          end
27
        end
28
      end
29
    end
30
  end
31
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