go-playground / validator

:100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving
MIT License
16.41k stars 1.3k forks source link

Use of excludeall with omitempty and startswith filed #1126

Open pridya23 opened 1 year ago

pridya23 commented 1 year ago

Package version eg. v9, v10:

v9, v10

Issue, Question or Enhancement:

Question

Code sample, to showcase or reproduce:

type Script struct { Path string json:"path" validate:"omitempty,startswith=/document/scripts/,excludeall=@!*" }

How can we achieve the validation of path field, with direct filed tags in go validator to exclude special characters.

MysteriousPotato commented 1 year ago

You're missing an "s" in the excludesall rule. It should be excludesall=@!*

That being said, I'm not sure what your use case is but @!* may all be valid path characters depending on the os.

The filepath or dirpath rules, which are platform independent, may be more appropriate.

Ex:

type Script struct {
    Path string `json:"path" validate:"omitempty,startswith=/document/scripts/,dirpath"`
}
pridya23 commented 1 year ago

Thank you for your response.

Use case: To validate that the file path is a valid linux/windows path (without actually looking for path's existence on host).

Tried: Use of filepatha and dirpath, but is does not even allow _ or - in the file name which should be allowed. Hence it does not server the purpose for my use case. https://go.dev/play/p/1SCWdulmB32

Other option would be to use excludesall, but is is not compatible to be used with omitempty and startswith. https://go.dev/play/p/Ww6SKvxfkO_T

So what better approach would be? Is it to use custom validator with regex?

Thanks in advance.

MysteriousPotato commented 1 year ago

Tried: Use of filepatha and dirpath, but is does not even allow _ or - in the file name which should be allowed. Hence it does not server the purpose for my use case. https://go.dev/play/p/1SCWdulmB32

In this example, the dirpath tag is used but the Path property is set to a file path. The filepath and dirpath tags can only be used to validate file paths and directory paths respectively. See full example.

If the path can either point to a file or a directory, you could create your own validation rule like so.

Other option would be to use excludesall, but is is not compatible to be used with omitempty and startswith. https://go.dev/play/p/Ww6SKvxfkO_T

The example you gave does not compile because the Path tag contains an unescaped " and an escaped / which is invalid syntax. By changing excludesall=*<>!\/!"? with excludesall=*<>/!\"?, we can see that go now compiles.

However, the validation will return an error since the excludesall rule includes /, which your paths will inevitably contain.

So what better approach would be? Is it to use custom validator with regex?

From my understanding of your use case, the filepath and/or dirpath tags are the way to go since they were specifically created to validate filesystem paths regardless of the OS. Validating a filesystem path using a regex can be quite tedious and can't cover OS specific rules. I would not recommend this.