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

sanitize_filename() can return empty strings #20

Closed
mkbloke opened this issue Aug 8, 2021 · 1 comment
Closed

sanitize_filename() can return empty strings #20

mkbloke opened this issue Aug 8, 2021 · 1 comment
Assignees

Comments

@mkbloke
Copy link

mkbloke commented Aug 8, 2021

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
Copy link
Owner

thombashi commented Sep 26, 2021

@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 "").

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants