Make ParamsScope#validates pure via ValidationsSpec (closes #2517)#2706
Open
ericproulx wants to merge 1 commit intomasterfrom
Open
Make ParamsScope#validates pure via ValidationsSpec (closes #2517)#2706ericproulx wants to merge 1 commit intomasterfrom
ericproulx wants to merge 1 commit intomasterfrom
Conversation
Danger ReportNo issues found. |
9154bb1 to
3662c16
Compare
`Grape::Validations::ParamsScope#validates(attrs, validations)` used
to consume the validations hash destructively across six helpers
(`infer_coercion`, `derive_validator_options`, `validates_presence`,
`coerce_type`, `document_params`, `extract_details`). The caller's
hash was mutated in place; passing a frozen hash would crash the
pipeline.
Introduces `Grape::Validations::ValidationsSpec` — a small frozen
value object that parses the raw hash once and exposes named
accessors. `validates` becomes a thin dispatcher that hands the spec
to the helpers; the raw hash is never written to. Helpers that used
to read/delete are gone:
* `infer_coercion` → `ValidationsSpec#parse_coerce` (private)
* `derive_validator_options` → `ValidationsSpec#shared_opts`
* `validates_presence` → `ParamsScope#validate_presence(spec, ...)`
* `coerce_type` (the helper) → `ParamsScope#validate_coerce(spec, ...)`
* `guess_coerce_type` → `ValidationsSpec#guess_coerce_type` (private)
* `options_key?` → no longer needed; `ValidationsSpec#resolve_value`
`ParamsDocumentation#document_params` now takes the spec instead of
the raw hash and reads via `spec.raw[...]` without deletion. The
`do_not_document` short-circuit no longer mutates either; it just
returns early.
Behaviour preserved:
* Order of validator dispatch (presence → coerce → others).
* `:message` belonging to presence is suppressed for later
validators (it's in `SPEC_CONSUMED_KEYS`).
* `:allow_blank` and `:length` remain dual-purpose (shared opt + own
validator entry; doc source + own validator entry).
* `check_coerce_with` still raises `ArgumentError` at definition
time when `coerce_with:` is supplied without a type.
Adds:
* `lib/grape/validations/validations_spec.rb` — value object.
* `spec/grape/validations/validations_spec_spec.rb` — unit tests
including a frozen-hash test that asserts `from(frozen_hash)` does
not raise.
Incidentally fixes #2517: because `:message` is in
`SPEC_CONSUMED_KEYS`, `optional :foo, message: 'oops'` no longer
raises `UnknownValidator`. Regression spec added.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
3662c16 to
acb86d1
Compare
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.
Refactors
Grape::Validations::ParamsScope#validatesso that the validations hash supplied by the DSL is never mutated. The caller's hash flows through the pipeline untouched; passing a frozen hash works.Resolves the project memory note
project_validations_param_mutation.md— both Phase 1 (dup-at-boundary) and Phase 2 (pure helpers) in one shot, since the spec object makes Phase 1's defensive dup unnecessary.Closes #2517 — incidentally. With
:messagenow inSPEC_CONSUMED_KEYS,optional :foo, message: 'oops'no longer raisesUnknownValidator(it used to crash because:messagesurvived dispatch when:presencewas absent). Regression spec added.Before
The hash was destructively consumed across six helpers:
infer_coercion:coerce,:coerce_message; deletes:type,:types, sometimes:coerce_withderive_validator_options:fail_fastvalidates_presence:presence,:messagevalidatesitselfvalidations.extract!(:coerce, :coerce_with, :coerce_message)document_paramsvalidations.except!(:desc, :description, :documentation)whendo_not_documentextract_details:desc,:description,:documentationThe contract was implicit: "
validateseats the hash."After
A small frozen
Grape::Validations::ValidationsSpecparses the raw hash once and exposes named accessors:validatesshrinks to a small dispatcher:The caller can pass
validations.freezeand nothing crashes.Helpers retired
infer_coercion(validations)ValidationsSpec#parse_coerce(private)derive_validator_options(validations)ValidationsSpec#shared_optsvalidates_presence(validations, ...)ParamsScope#validate_presence(spec, ...)coerce_type(validations, ...)ParamsScope#validate_coerce(spec, ...)guess_coerce_typeoptions_key?ValidationsSpec#resolve_valueBehaviour preserved
:messagebelongs to presence: it's inSPEC_CONSUMED_KEYS, so it never appears invalidator_entries(no risk of being re-dispatched as aMessageValidator). This is also what fixes Optional parameter with a message attribute exception #2517.:allow_blankis dual-purpose: read intoshared_optsand still appears invalidator_entries(soAllowBlankValidatorruns).:lengthis dual-purpose: read for documentation and dispatched toLengthValidator.check_coerce_withstill raisesArgumentErrorat definition time whencoerce_with:is supplied without a type or withJSON.do_not_documentshort-circuit: still returns early; just no longer mutates.Tests
spec/grape/validations/validations_spec_spec.rbcovering parsing, frozen-hash safety,required?semantics, shared opts, validator-entry filtering.spec/grape/validations/params_documentation_spec.rbrewritten to pass aValidationsSpecinstead of a raw hash. The old "removes desc/description/documentation when do_not_document" test (which asserted mutation) is now "does not store any documented params" + "does not mutate the input validations" — capturing the corrected contract.spec/grape/validations_spec.rb.Test plan
bundle exec rspec spec/grape/validations/validations_spec_spec.rb— 16/0bundle exec rspec spec/grape/validations/params_documentation_spec.rb— 8/0bundle exec rspec spec/grape/validations_spec.rb -e 'optional accepts a :message option without raising'— 1/0bundle exec rspec— 2,278/0bundle exec rubocop lib/ spec/— clean🤖 Generated with Claude Code