Fix custom error on a key schema being lost for wrong keys#348
Open
sarathfrancis90 wants to merge 1 commit into
Open
Fix custom error on a key schema being lost for wrong keys#348sarathfrancis90 wants to merge 1 commit into
sarathfrancis90 wants to merge 1 commit into
Conversation
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.
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.
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 genericWrong 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 laterSchemaWrongKeyErroris then built only from the surrounding dict schema's ownerror(usuallyNone), 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 resultingSchemaWrongKeyError. The default wrong-key message is unchanged when no custom error is set.Testing: added
test_wrong_key_reports_key_schema_error(coversRegexandAndkey 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.