Skip to content

fix(checkExact): detect unknown fields under wildcard when specific sub-paths are declared#1362

Open
mahmoodhamdi wants to merge 1 commit intoexpress-validator:masterfrom
mahmoodhamdi:fix/issue-1242-checkExact-wildcard
Open

fix(checkExact): detect unknown fields under wildcard when specific sub-paths are declared#1362
mahmoodhamdi wants to merge 1 commit intoexpress-validator:masterfrom
mahmoodhamdi:fix/issue-1242-checkExact-wildcard

Conversation

@mahmoodhamdi
Copy link
Contributor

Summary

Fixes #1242.

When checkExact is used together with wildcard validation chains (e.g. body('*').isObject()) alongside specific sub-path chains (e.g. body('*.id'), body('*.qty')), unknown nested fields inside array elements are not detected. A field like wrong in [{ id: 1, qty: 100 }, { id: 2, wrong: 1, qty: 100 }] passes checkExact without any error.

Root Cause

selectUnknownFields builds a tree of known field paths. When body('*') is registered, it creates the tree { '*': { '': {} } }. The '' leaf in findUnknownFields means "this branch is validated as a whole — all descendants are implicitly known", causing the function to short-circuit and skip children. This happens even when more specific sub-paths like *.id and *.qty are also in the tree (producing { '*': { '': {}, 'id': { '': {} }, 'qty': { '': {} } } }), because the '' check fires first.

Fix

Introduce isCoveredAsWhole(tree) which returns true only when:

  1. The '' leaf is present, AND
  2. There are no more-specific sub-keys (other than '' and '**')

When specific sub-keys co-exist with '', those sub-keys define constrained knowledge. The '' marker means "the field's value is validated" but does NOT imply all descendants are implicitly known. Anything not listed as a specific sub-key is flagged as unknown.

Behaviour

Known fields Request Unknown fields detected
['*'] only [{ id: 1, extra: 2 }] none (fully covered by wildcard)
['*', '*.id', '*.qty'] [{ id: 1, qty: 100, wrong: 1 }] [0].wrong
['*.id', '*.qty'] [{ id: 1, qty: 100, wrong: 1 }] [0].wrong

Test plan

  • New tests added in src/middlewares/exact.spec.ts covering the wildcard + specific sub-paths scenario with both checkExact chains and checkSchema.
  • New tests added in src/field-selection.spec.ts covering selectUnknownFields directly.
  • All 317 tests pass (npm test).

…ub-paths are declared

When a wildcard field (e.g. body('*')) and more specific sub-paths (e.g. body('*.id'),
body('*.qty')) are both registered with checkExact, unknown nested fields were not
detected. The wildcard's leaf marker ('') in the tree caused findUnknownFields to
short-circuit and treat all descendants as implicitly known, even when specific
sub-paths defined constrained knowledge.

Introduce isCoveredAsWhole() which only returns true when the '' leaf is present
and there are no more-specific sub-keys. When specific sub-keys co-exist with '',
they take precedence and define exactly what is known, so unregistered nested
fields are correctly flagged as unknown.

Fixes express-validator#1242
@coveralls
Copy link

Coverage Status

coverage: 100.0%. remained the same
when pulling 7a999ab on mahmoodhamdi:fix/issue-1242-checkExact-wildcard
into 0b1dbe3 on express-validator:master.

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.

Is checkExact() with wildcards supported?

2 participants