jonschlinkert / is-valid-path

Returns true if a windows file path does not contain any invalid characters.
https://github.com/jonschlinkert
MIT License
12 stars 4 forks source link

Problem with parentheses #6

Open speker2010 opened 2 years ago

speker2010 commented 2 years ago

There is should be something like this 'images/my-photo(1).jpg', isn't it? https://github.com/jonschlinkert/is-valid-path/blob/master/test.js#L15-L20

I think it's correct filename

air2 commented 2 years ago

I have the same issue. Parentheses are incorrectly considered invalid.

air2 commented 2 years ago

I quickly wrote this function to test paths. I guess it can be better, but it is just a quick fix to solve this problem in our code

export function isValidPath(pathName: string) {
    try {
        const resolvedPath = resolve(pathName)
        if (resolvedPath) {
            if (process.platform === 'win32') {
                if (pathName.length > 4 && pathName[2] === '?' && pathName[0] === '\\' && pathName[1] === '\\' && pathName[3] === '\\') { pathName = pathName.substring(4) }
                if (pathName.length > 2 && pathName[1] === ':' && pathName[2] === '\\') { pathName = pathName.substring(2) }
                return !/[<>:"|?*]/.test(pathName)
            }
            return true
        }
    }
    catch(_e: unknown) {
    }
    return false
}