Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Trailing newline in value to validate ignored when using regex #307

Open
callumgare opened this issue Jan 16, 2024 · 1 comment
Open

Comments

@callumgare
Copy link

The string a\n should fail validation with the schema Regex("^a$") but that does not happen. You can see this by running:

python3 -c 'from schema import Regex; Regex("^a$").validate("a\\n")'

This appears to only be true for a single trailing newline. Multiple trailing newlines or a leading newline both fail as expected:

python3 -c 'from schema import Regex; Regex("^a$").validate("a\\n\\n")' # this fails
python3 -c 'from schema import Regex; Regex("^a$").validate("\\na")' # as does this

I had a look though the existing issues but can't see this reported anywhere. I'm using the latest version (0.7.5) with Python 3.11.6 on macOS.

Thanks!

@callumgare callumgare changed the title Trailing newline in value to validate ignored when using regex [Bug] Trailing newline in value to validate ignored when using regex Jan 16, 2024
@mutricyl
Copy link

Refering to python regex documentation:

$
Matches the end of the string or just before the newline at the end of the string (...)

schema Regex class is build python re. It looks like a feature.

note that you may have written too many \.
under windows:

>>> Regex("^a$").validate("a")    
'a'
>>> Regex("^a$").validate("a\n")
'a\n'
>>> Regex("^a$").validate(r"a\n") 
schema.SchemaError: Regex('^a$') does not match 'a\\n'
>>> Regex("^a$").validate("a\\n") 
schema.SchemaError: Regex('^a$') does not match 'a\\n' 

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

No branches or pull requests

3 participants
@callumgare @mutricyl and others