Encapsulate Grape::Validations::Validators::Base state behind readers#2725
Open
ericproulx wants to merge 1 commit into
Open
Encapsulate Grape::Validations::Validators::Base state behind readers#2725ericproulx wants to merge 1 commit into
ericproulx wants to merge 1 commit into
Conversation
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>
2dbf98c to
c39cc2f
Compare
Danger ReportNo issues found. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Grape::Validations::Validators::Baseexposed its state as bare instance variables that every subclass reached into directly (@options,@scope,@required,@allow_blank,@exception_message,@attrs,@fail_fast). This PR routes all internal reads through accessor methods so the ivars are an implementation detail, and gives the boolean attributes consistent?predicates.Pure internal refactor — no behavior change.
What changed
BaseAdded private
attr_reader :options, :scope, :required, :allow_blank, :exception_message.:attrsstays a publicattr_reader(left intentionally — making it private would be an externally observable change for third-party code callingvalidator.attrs).Boolean readers now have
?aliases, consistent with the existingfail_fast?:fail_fast?was a hand-writtendef; it's now anattr_reader+aliaslike the others. Onlyattrsandfail_fast?stay public — they are the only readers that cross the validator boundary (Grape::Endpoint#run_validatorscallsvalidator.validate/validator.fail_fast?); everything else is now private.Subclasses — every direct
@ivarread replaced with the reader:default_validator,coerce_validator,multiple_params_base,values_validator,length_validator,oneof_validator,at_least_one_of_validator,all_or_none_of_validator. Two locals that would have shadowed the new readers were renamed (attrs→attributesinDefaultValidator#validate!,scope→current_scopeinValuesValidator#required_for_root_scope?).The
@optionalias ivar (# TODO: remove in next major release) is deliberately left untouched — it's an externally-facing deprecated alias and folding it in is a separate decision.Visibility (verified by introspection)
attrs,fail_fast?fail_fast,options,scope,required,allow_blank,exception_message,required?,allow_blank?Testing
spec/grape/validations+spec/grape/endpoint_spec.rb: 795 examples, 0 failures🤖 Generated with Claude Code