Skip to content

Fix custom error on a key schema being lost for wrong keys#348

Open
sarathfrancis90 wants to merge 1 commit into
keleshev:masterfrom
sarathfrancis90:fix-wrong-key-custom-error
Open

Fix custom error on a key schema being lost for wrong keys#348
sarathfrancis90 wants to merge 1 commit into
keleshev:masterfrom
sarathfrancis90:fix-wrong-key-custom-error

Conversation

@sarathfrancis90

Copy link
Copy Markdown

Fixes #345.

Bug: when a key schema defines a custom error, e.g. Schema({Regex(pattern, error="Invalid profile name"): dict}), and a data key doesn't match it, the key is reported as a wrong key but the custom message is dropped — you only get the generic Wrong key 'bar' in ... and the explicit error never reaches you.

Cause: in Schema.validate's dict branch, a key that fails to match any key schema is silently swallowed (except SchemaError: pass). The later SchemaWrongKeyError is then built only from the surrounding dict schema's own error (usually None), so the rejecting key schema's custom error is gone.

Fix: when a rejecting key schema carries a custom error, remember it, and surface it in the resulting SchemaWrongKeyError. The default wrong-key message is unchanged when no custom error is set.

Testing: added test_wrong_key_reports_key_schema_error (covers Regex and And key schemas, plus the no-custom-error case). Verified it fails before the fix and passes after. Full suite (120 tests) passes; ruff check/format clean; no new mypy errors.

When a data key fails to match a key schema that defines a custom
`error`, the key is reported as a wrong key but the custom message was
discarded, so the explicit error never reached the user (issue keleshev#345).

Remember the custom error raised by a rejecting key schema and surface
it in the resulting SchemaWrongKeyError. The default wrong-key message is
unchanged when no custom error is set.
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.

error arg ignored and cascade of exceptions

1 participant