Repository navigation
Report a required param the request left out as missing only - #3100
Open
ericproulx wants to merge 1 commit into
Open
ericproulx wants to merge 1 commit into
ericproulx wants to merge 1 commit into
Conversation
ericproulx
force-pushed
the
fix/validate-only-sent-params
branch
from
November 21, 2026 18:12
19ea00b to
8049469
Compare
This comment has been minimized.
This comment has been minimized.
ericproulx
force-pushed
the
fix/validate-only-sent-params
branch
from
November 22, 2026 00:53
8049469 to
d4bd395
Compare
Every validator of a `requires` param was called whether the request
sent the param or not, because PresenceValidator has to be called for
a missing one to report it. A custom validator that read the value was
then handed nil and raised, so the request answered 500 instead of
400. The README's own AlphaNumeric example does that:
requires :text, alpha_numeric: true
GET / Before: 500 NoMethodError (undefined method 'match?' for nil)
After: 400 text is missing
The built-in validators added a second error to `is missing`
instead: `n is missing, n does not have a valid value` from `values`,
`email is missing, email is empty` from `allow_blank: false`, and
`ids is missing, ids is invalid` from a `coerce_with` method handed nil.
Only PresenceValidator is now called for an attribute the params do
not hold. Every other validator sees only the params a request sent,
as it already did for an `optional` param, so a missing one is
reported once. Each validator settles this when it is built, so the
attribute loop reads an ivar where it called `required?`. A param
sent as `null` is still validated, and `values` still rejects it.
Asking the params whether they hold each required attribute cost a
nested body of 20 items 5%, and `declared` 4%, in Duel. The built-in
validators no longer ask again whether the params are a Hash that
holds the attribute, as they are now only called when it does. And
CoerceValidator, which every typed param has, is still called for a
required attribute and tells a missing one for itself, from the value
it reads anyway: only a nil is looked up. Every benchmark now
measures the same as master.
UPGRADING notes the dropped second messages, including the one #3010
documented for a nested param with `values`. The README says when
`validate_param!` is called.
Fixes #1693.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
ericproulx
force-pushed
the
fix/validate-only-sent-params
branch
from
November 22, 2026 00:57
d4bd395 to
edd868c
Compare
🟢 Duel: 100% of benchmarks passedCompared edd868c (merged with Details: 39 benchmarks in 155s, 14 unsettled
An unsettled timing was too close to the limit to settle. It doesn't fail the run, but one that's unsettled in run after run is noisy. Across the board:
A timing is slower when a 99.0% confidence interval for its ratio, the candidate's to the baseline's, is above 1.02. How Duel decides |
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
Fixes #1693.
Every validator of a
requiresparam was called whether the request sent the param or not, becausePresenceValidatorhas to be called for a missing one to report it. A custom validator that read the value was then handedniland raised, so the request answered 500 instead of 400. The README's ownAlphaNumericexample does that:The built-in validators added a second error to
is missinginstead:requires :n, type: Integer, values: [1, 2]n is missing, n does not have a valid valuen is missingrequires :email, allow_blank: falseemail is missing, email is emptyemail is missingrequires :n, same_as: :m(msent)n is missing, n is not the same as mn is missingrequires :address, type: Hash do requires :country, values: […] end,address: {}address[country] is missing, address[country] does not have a valid valueaddress[country] is missingrequires :ids, type: Array[Integer], coerce_with: ->(v) { v.split(',') }ids is missing, ids is invalidids is missingOnly
PresenceValidatoris now called for an attribute the params do not hold. Every other validator sees only the params a request sent, as it already did for anoptionalparam, so a missing one is reported once. Each validator settles this when it is built (validates_missing?), so the attribute loop reads an ivar where it calledrequired?. The same goes for an element of an Array scope that is not a Hash:items is invalid, items[key] is missing, items[key] is invalidloses its last entry.A param sent as
nullis still validated, andvaluesstill rejects it (n does not have a valid value), as #3010 made it.Performance
Asking the params whether they hold each required attribute is a
HashWithIndifferentAccess#key?per attribute and validator, which Duel caught on the first push: a nested JSON body of 20 items 5% slower,declared4%. Two changes give it back:hash_like?, andkey?inregexpandexcept_values), as they are now only called when it does.CoerceValidator, which every typed param has, is still called for a required attribute and tells a missing one for itself from the value it reads anyway: only anilis looked up. An optional one still goes through the attribute loop'skey?, as reading a key aHashWithIndifferentAccesslacks costs more than asking (15% on the defaults benchmark when I tried).Locally (Ruby 4.0, YJIT), all 39 Duel benchmarks now measure the same as master, the nested body at 1.006 in instructions.
Contract change
Error bodies for a missing required param lose the second message, including the one #3010 documented for a nested param with
valuesin 4.1.0. A custom validator is no longer called for a required param the request left out. UPGRADING covers both, and points a validator that has to see a missing param tovalidate(request). The README now says whenvalidate_param!is called.Specs
Nine specs had pinned a second message and now expect
is missingalone.validations_spec's "validates when param is not present" pinned the custom validator being called for a missing param and is renamed for what it checks now. New specs cover a custom validator that reads the value of a required param left out at the root and from an element of an Array, and fail without the change (NoMethodError). Three more incoerce_validator_speccover a required param left out at the root and from an element, which fail without coerce's own check, and one sent asnull, still coerced.Test plan
bundle exec rakepasses locally (RuboCop clean, 3348 examples, 0 failures, rebased on Report a nested Array scope that got no Array under its own name only #3099).🤖 Generated with Claude Code