Skip to content

Report a required param the request left out as missing only - #3100

Open
ericproulx wants to merge 1 commit into
masterfrom
fix/validate-only-sent-params
Open

ericproulx wants to merge 1 commit into
masterfrom
fix/validate-only-sent-params

Conversation

@ericproulx

@ericproulx ericproulx commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #1693.

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:

params do
  requires :text, alpha_numeric: true
end
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:

declaration, nothing sent before after
requires :n, type: Integer, values: [1, 2] n is missing, n does not have a valid value n is missing
requires :email, allow_blank: false email is missing, email is empty email is missing
requires :n, same_as: :m (m sent) n is missing, n is not the same as m n is missing
requires :address, type: Hash do requires :country, values: […] end, address: {} address[country] is missing, address[country] does not have a valid value address[country] is missing
requires :ids, type: Array[Integer], coerce_with: ->(v) { v.split(',') } ids is missing, ids is invalid ids is missing

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 (validates_missing?), so the attribute loop reads an ivar where it called required?. 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 invalid loses its last entry.

A param sent as null is still validated, and values still 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, declared 4%. Two changes give it back:

  • The built-in validators no longer ask again whether the params are a Hash that holds the attribute (hash_like?, and key? in regexp and except_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 a nil is looked up. An optional one still goes through the attribute loop's key?, as reading a key a HashWithIndifferentAccess lacks 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 values in 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 to validate(request). The README now says when validate_param! is called.

Specs

Nine specs had pinned a second message and now expect is missing alone. 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 in coerce_validator_spec cover a required param left out at the root and from an element, which fail without coerce's own check, and one sent as null, still coerced.

Test plan

🤖 Generated with Claude Code

@ericproulx
ericproulx force-pushed the fix/validate-only-sent-params branch from 19ea00b to 8049469 Compare November 21, 2026 18:12
@github-actions

This comment has been minimized.

@ericproulx
ericproulx force-pushed the fix/validate-only-sent-params branch from 8049469 to d4bd395 Compare November 22, 2026 00:53
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
ericproulx force-pushed the fix/validate-only-sent-params branch from d4bd395 to edd868c Compare November 22, 2026 00:57
@github-actions

Copy link
Copy Markdown

🟢 Duel: 100% of benchmarks passed

Compared edd868c (merged with master) with 2952721 (where HEAD left origin/master).

Details: 39 benchmarks in 155s, 14 unsettled
Benchmark Verdict Timings (ratio [interval])
callbacks: a before callback that halts with error! unsettled cpu 1.002 [0.969, 1.034] unsettled
mount: GET a route of a mounted API unsettled cpu 0.996 [0.970, 1.024] unsettled
declared: only the params sent unsettled cpu 1.008 [0.999, 1.023] unsettled
errors: error! with a hash and headers unsettled cpu 1.011 [0.984, 1.032] unsettled
errors: rescue_from :all with a handler unsettled cpu 1.038 [1.001, 1.077] unsettled
format: present one object with a representer unsettled cpu 1.015 [0.980, 1.053] unsettled
format: take the format from the extension unsettled cpu 1.005 [0.976, 1.024] unsettled
params: validate a form body unsettled cpu 1.014 [0.987, 1.053] unsettled
params: validate a nested JSON body of 20 items unsettled cpu 1.011 [0.993, 1.024] unsettled
routing: GET a route with a path param unsettled cpu 0.999 [0.975, 1.023] unsettled
routing: GET the last param route of 200 unsettled cpu 0.999 [0.949, 1.063] unsettled
routing: 404 among 200 routes unsettled cpu 1.000 [0.852, 1.130] unsettled
routing: GET three levels of nested namespaces with requirements unsettled cpu 1.007 [0.974, 1.038] unsettled
versioning: the Accept-Version header unsettled cpu 1.014 [0.979, 1.036] unsettled
callbacks: before, validation, after and finally callbacks, with helpers same cpu 0.985 [0.971, 1.003]
compile: an API of 20 routes same cpu 1.001 [0.982, 1.016]
compile: an API that mounts others same cpu 1.004 [0.996, 1.011]
declared: all declared params, with missing ones as nil same cpu 1.000 [0.993, 1.010]
declared: with string keys same cpu 1.000 [0.992, 1.010]
errors: error! with a message same cpu 0.985 [0.964, 1.000]
errors: rescue_from a class with a handler same cpu 0.960 [0.910, 0.993]
errors: rescue_from :all with the default handler same cpu 0.958 [0.937, 0.996]
format: render a small JSON object same cpu 1.000 [0.986, 1.018]
format: render a JSON array of 50 hashes same cpu 0.995 [0.978, 1.010]
format: present 50 objects with a representer same cpu 1.007 [0.999, 1.015]
format: negotiate the format from a browser Accept header same cpu 0.992 [0.957, 1.010]
params: coerce a path param to an Integer same cpu 1.002 [0.966, 1.020]
params: coerce and validate a query string of 10 params same cpu 0.998 [0.983, 1.019]
params: fill in defaults for missing optional params same cpu 0.993 [0.961, 1.020]
params: validate a JSON body with a custom validator same cpu 1.002 [0.988, 1.017]
params: reject invalid params with 400 same cpu 1.001 [0.979, 1.015]
routing: GET a static route same cpu 1.002 [0.986, 1.018]
routing: GET the last static route of 200 same cpu 1.001 [0.979, 1.014]
routing: 405 for a method a path does not answer same cpu 1.002 [0.988, 1.020]
routing: GET a wildcard route same cpu 0.980 [0.946, 1.007]
versioning: a version in the path same cpu 0.981 [0.927, 1.017]
versioning: a vendor media type in the Accept header same cpu 0.964 [0.915, 1.006]
versioning: an Accept header cascading past a newer version same cpu 1.003 [0.992, 1.017]
versioning: a version param same cpu 0.991 [0.975, 1.006]

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:

  • cpu 1.000 [0.994, 1.007] unsettled (limit 1.0050, 39 benchmarks)

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Array of hashes not failing correctly when array is empty

1 participant