Fix trailing-newline false positive in Regex validation (#307) - #356
Open
apoorvdarshan wants to merge 1 commit into
Open
Fix trailing-newline false positive in Regex validation (#307)#356apoorvdarshan wants to merge 1 commit into
apoorvdarshan wants to merge 1 commit into
Conversation
In non-MULTILINE mode Python's $ also matches just before a single
trailing newline, so Regex('^a$').validate('a\n') wrongly returned 'a'
instead of raising SchemaError. Re-check the match with an extra newline
appended so that quirk cannot fire, while preserving substring-matching
for unanchored patterns.
Fixes keleshev#307.
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.
Summary
Regex('^a$').validate('a\n')returned'a'instead of raisingSchemaError(issue #307). In Python's non-MULTILINEmode,$matches not only at the very end of the string but also just before a single trailing newline, sore.search('^a$', 'a\n')is truthy. The bug only affects a single trailing newline;'a\n\n'and'\na'already failed as expected.Fix
In
Regex.validate(schema/__init__.py), when the data ends with a newline, re-run the search with an extra newline appended. That turns a lone trailing\ninto\n\n, which defeats the$-before-newline quirk. If the match disappears, it was a phantom and validation fails; otherwise the value is accepted. Unanchored/substring patterns (e.g.Regex('foo').validate('foo\n')) are unaffected because their match does not rely on that end-anchor position. The appended newline mirrors the data's type (strvsbytes) so byte patterns keep working.The change is a small, localized guard — it does not switch to
fullmatch, preserving the intended substring-match behavior for unanchored patterns.Tests
Added regression cases to
test_regexcovering:Regex('^a$').validate('a\n'),Regex('^[a-z]+$').validate('letters\n')raiseSchemaError.'a'passes;'a\n\n'and'\na'still fail).Regex('foo').validate('foo\n')andRegex('^foo').validate('foo\n')still return the value.Full suite:
120 passed.ruff checkandruff format --checkare clean, andmypyreports no new errors. (One pre-existing README doctest failure is unrelated to this change — it fails identically on pristinemasterdue to a stale expected error message under newer Python.)Disclosure: prepared with AI assistance; reviewed and verified locally.