feat: add audio validation#1539
Conversation
|
Looks good to me. Let's ship this please! |
zemzale
left a comment
There was a problem hiding this comment.
One small thing that could be changed, but other than that don't see an issue with this. Is 90% the same thing as isImage.
There is an idea maybe that we can just have a generic mimetype or smth validator, but that is besides this PR.
baked_in.go
Outdated
| field := fl.Field() | ||
|
|
||
| switch field.Kind() { | ||
| case reflect.String: | ||
| filePath := field.String() | ||
| fileInfo, err := os.Stat(filePath) | ||
| if err != nil { | ||
| return false | ||
| } | ||
|
|
||
| if fileInfo.IsDir() { | ||
| return false | ||
| } | ||
|
|
||
| file, err := os.Open(filePath) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| defer func() { | ||
| _ = file.Close() | ||
| }() | ||
|
|
||
| mime, err := mimetype.DetectReader(file) | ||
| if err != nil { | ||
| return false | ||
| } |
There was a problem hiding this comment.
Everything down to here is exactly the same code as isImage so maybe we can extract that logic so we don't have to rewrite it twice.
There was a problem hiding this comment.
We could create a isMimeType validator, that matches based on type and/or subtyle
would be resuable in the code base to avoid repetition and would also allow more flexibility for when using the library
Something like:
validate.Var("photo.png", "mimetype=image/png") //type and subtype validation
validate.Var("photo.png", "mimetype=image/*") //type validation only
There was a problem hiding this comment.
That is something that came to my mind also, but didn't bring it up, since I am not against a specific audio path.
…dded standalone isMIMEType validator for flexibility
5cf08c7 to
cfd8a26
Compare
cfd8a26 to
fc6e691
Compare
Fixes Or Enhances
Adds built-in
audiovalidation tag support via a newisAudiovalidator.isAudioimplementationExample Usage
Make sure that you've checked the boxes below before you submit PR:
@go-playground/validator-maintainers