thombashi / pathvalidate

A Python library to sanitize/validate a string such as filenames/file-paths/etc.
https://pathvalidate.rtfd.io/
MIT License
210 stars 12 forks source link

sanitize_filename() can return empty strings #20

Closed mkbloke closed 2 years ago

mkbloke commented 2 years ago

Here's another example that doesn't do what I would expect (although perhaps I'm expecting the wrong thing?).

In pathvalidate 2.4.1:

>>> pathvalidate.validate_filename("")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/iac2/st/lib/python3.8/site-packages/pathvalidate/_filename.py", line 260, in validate_filename
    FileNameValidator(
  File "/home/iac2/st/lib/python3.8/site-packages/pathvalidate/_filename.py", line 134, in validate
    validate_pathtype(
  File "/home/iac2/st/lib/python3.8/site-packages/pathvalidate/_common.py", line 49, in validate_pathtype
    raise ValidationError(
pathvalidate.error.ValidationError: reason=NULL_NAME, description=the value must be a not empty

That's what I would expect.

>>> pathvalidate.sanitize_filename("")
''
>>> pathvalidate.sanitize_filename("/")
''
>>> pathvalidate.sanitize_filename("//")
''
>>> pathvalidate.sanitize_filename("?")
''

That's consistent with the character removal, but the end result is not a valid filename, so I would have expected sanitize_filename() to raise an error here?

I can call validate_filename() after sanitize_filename() to take care of this. Is that what I should be doing, or should I be able to expect that sanitize_filename() either returns a usable filename or raises an error?

Cheers.

thombashi commented 2 years ago

@mkbloke Thank you for your feedback.

Since pathvalidate 2.5.0, you can set null_value_handler to sanitize_filename function call. This will change the function behavior when the value after sanitization is empty.

e.g. Raise exception when the value after sanitization is empty:

from pathvalidate import sanitize_filename, ValidationError
from pathvalidate.handler import raise_error

try:
    sanitize_filename("/", null_value_handler=raise_error)
except ValidationError as e:
    print(e)
reason=NULL_NAME, description=the value must be a not empty

The default behavior is not changed for now (return "").