Skip to content

feat: add audio validation#1539

Open
dapzthelegend wants to merge 4 commits intogo-playground:masterfrom
dapzthelegend:feature/audio-validation
Open

feat: add audio validation#1539
dapzthelegend wants to merge 4 commits intogo-playground:masterfrom
dapzthelegend:feature/audio-validation

Conversation

@dapzthelegend
Copy link

@dapzthelegend dapzthelegend commented Mar 9, 2026

Fixes Or Enhances

Adds built-in audio validation tag support via a new isAudio validator.

isAudio implementation

// isAudio is the validation function for validating if the current field's value contains the path to a valid audio file
func isAudio(fl FieldLevel) bool {
	mime, ok := detectFileMIMEType(fl.Field())
	if !ok {
		return false
	}

	return strings.HasPrefix(mime, "audio/")
}

Example Usage

package main

import (
	"fmt"

	"github.com/go-playground/validator/v10"
)

type Upload struct {
	AudioPath string `validate:"required,audio"`
}

func main() {
	validate := validator.New()

	u := Upload{AudioPath: "testdata/music.mp3"}
	if err := validate.Struct(u); err != nil {
		fmt.Println("validation failed:", err)
		return
	}

	fmt.Println("validation passed")
}

Make sure that you've checked the boxes below before you submit PR:

  • Tests exist or have been written that cover this particular change.

@go-playground/validator-maintainers

@dapzthelegend dapzthelegend requested a review from a team as a code owner March 9, 2026 23:12
@dapzthelegend
Copy link
Author

@the-fanan

@the-fanan
Copy link
Contributor

Looks good to me. Let's ship this please!

zemzale
zemzale previously approved these changes Mar 11, 2026
Copy link
Member

@zemzale zemzale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Comment on lines +1655 to +1680
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
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is something that came to my mind also, but didn't bring it up, since I am not against a specific audio path.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebased this branch on #1539

@dapzthelegend dapzthelegend force-pushed the feature/audio-validation branch from cfd8a26 to fc6e691 Compare March 22, 2026 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants