keleshev / schema

Schema validation just got Pythonic
MIT License
2.86k stars 214 forks source link

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

Open callumgare opened 7 months ago

callumgare commented 7 months ago

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!

mutricyl commented 5 months ago

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'