go-playground / validator

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

Custom validator not called for multipart.FileHeader type #747

Open brpaz opened 3 years ago

brpaz commented 3 years ago

Package version eg. v9, v10:

v10

Issue, Question or Enhancement:

Hello. I am trying to define a custom validator in a multipart.FileHeader type to validate file type and size, but it´s not being called.

Code sample, to showcase or reproduce:

type CreateTeamDto struct {
    Name string                `form:"name" validate:"required"`
    Logo *multipart.FileHeader `form:"logo" validate:"required,filetype"`
}

Validator Definition

import (
    goValidator "github.com/go-playground/validator/v10"
)

validator := goValidator.New()
validator.RegisterValidation("filetype", FileTypeValidator, true)

func FileTypeValidator(fl validator.FieldLevel) bool {
    fmt.Println("file type validator!!!!!!!!!!!")
    return false
}

The required validation works. And If I use this validator in other field in also works. But on the *multipart.FileHeader the function is never called.

Balake commented 3 years ago

Following, running into a similar issue.

Balake commented 3 years ago

Following, running into a similar issue.

I got around my issue with RegisterStructValidation

Thearas commented 3 years ago

+1. Any follow-up?

vtopc commented 3 years ago

https://github.com/go-playground/validator/issues/470#issuecomment-487990618