Skip to content

fix: align _validate_allowed type check with _validate_forbidden#637

Open
gaoflow wants to merge 1 commit into
pyeve:1.3.xfrom
gaoflow:fix/allowed-sequence-check
Open

fix: align _validate_allowed type check with _validate_forbidden#637
gaoflow wants to merge 1 commit into
pyeve:1.3.xfrom
gaoflow:fix/allowed-sequence-check

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 24, 2026

Copy link
Copy Markdown

Problem

_validate_allowed used Iterable for its instance check while _validate_forbidden used Sequence. Since Mapping (dict) is an Iterable but not a Sequence, this caused inconsistent behavior:

  • allowed: [{"key": "val"}] with value {"key": "val"}: iterated over dict keys, found "key" not in [{"key": "val"}], and incorrectly rejected the value.
  • forbidden: [{"key": "val"}] with value {"key": "val"}: correctly checked the dict as a whole value.

Fix

Changed _validate_allowed from using Iterable to Sequence (matching _validate_forbidden), so both rules treat dict and set values as single values and only iterate over sequence elements.

Changes

  • cerberus/validator.py: one line changed (Iterable -> Sequence in _validate_allowed)
  • cerberus/tests/test_validation.py: added test covering the inconsistency

_validate_allowed used Iterable (which includes Mapping) while
_validate_forbidden used Sequence (which excludes Mapping). This caused
'allowed' to iterate over dict keys instead of checking the dict as a
single value, producing inconsistent results between the two rules.

Change _validate_allowed from Iterable to Sequence to match
_validate_forbidden, so both rules treat dict and set values as single
values and only iterate over sequence elements.
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.

1 participant