Move declaration-coherence checks into ValidationsSpec#2720
Open
ericproulx wants to merge 1 commit into
Open
Conversation
`check_incompatible_option_values` and `validate_value_coercion` were private methods on `ParamsScope`, invoked from `#validates` right after building the `ValidationsSpec`. Both are pure cross-field invariants computed entirely from fields the spec already owns (`default`, `values`, `except_values`, `coerce_type`). `ValidationsSpec` already enforced one construction-time invariant (the `:type` + `:types` mutual-exclusion `ArgumentError`), so concentrating the rest there makes "you cannot construct an incoherent spec" true by definition: the invariant no longer depends on a caller remembering to run the checks after `.from(...)`. `ParamsScope#validates` shrinks to build-spec → document → dispatch. - Moved both methods into `ValidationsSpec` as private; a new private `validate!` runs them from `initialize`, before `freeze`. - Removed both from `ParamsScope` and the two call sites in `#validates`. - `check_coerce_with` deliberately stays in `ParamsScope#validate_coerce`: it is gated on the remountable-API skip (`return unless coerce_options[:type]`, because a base instance has no resolved type until the mounted instance replays) and is therefore not a pure construction-time invariant. Spec fixture update: `params_documentation_spec` built a spec with `default: 42, values: [1, 2, 3]` purely to exercise `document_params`; that combination can no longer be constructed, so the fixture now uses a coherent `default: 1`. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
55b22ab to
0a099e9
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
check_incompatible_option_valuesandvalidate_value_coercionwere private methods onParamsScope, invoked from#validatesimmediately after building theValidationsSpec. Both are pure cross-field invariants computed entirely from fields the spec already owns (default,values,except_values,coerce_type).ValidationsSpecalready enforced one construction-time invariant — the:type+:typesmutual-exclusionArgumentErrorin#initialize. Concentrating the rest there makes "you cannot construct an incoherent spec" true by definition: the invariant no longer depends on a caller remembering to run the checks after.from(...).ParamsScope#validatescollapses to build-spec → document → dispatch.Changes
check_incompatible_option_valuesandvalidate_value_coercionintoValidationsSpecas private methods.ValidationsSpec#validate!runs them from#initialize, beforefreeze.ParamsScopeand removed the two call sites in#validates.Deliberately left behind:
check_coerce_withIt looks similar but isn't a pure construction-time invariant. It's invoked from
ParamsScope#validate_coerce, gated on the remountable-API skip (return unless coerce_options[:type]— a base instance has no resolved type until the mounted instance replays). Moving it intoValidationsSpec#initializewould make a remountable API raise on the base instance before its type resolves. It stays invalidate_coerce.Behaviour change
Constructing a
ValidationsSpecwith an incoherent combination (e.g.default: 42, values: [1, 2, 3]) now raisesGrape::Exceptions::IncompatibleOptionValuesat construction instead of only whenParamsScope#validatesreached the check. For real APIs this is identical (the spec is always built inside#validates). One documentation spec built such a deliberately-incompatible spec just to exercisedocument_params; its fixture now uses a coherentdefault: 1.ValidationsSpecgains a dependency onGrape::Exceptions::IncompatibleOptionValues— acceptable since it already raisesArgumentErrorand is Grape-internal.Test plan
bundle exec rspec— 2307 examples, 0 failures🤖 Generated with Claude Code