mardi 6 juin 2023

How to access to interface of a pointered function

For the validator I have the following interface

type validatorInterface interface {
    IsValid() error
}

func customValidation(fl validator.FieldLevel) bool {
    field := fl.Field().Interface()
    validatorObject, ok := field.(validatorInterface)
    if !ok {
        return false
    }
    err := validatorObject.IsValid()
    if err != nil {
        return false
    }

    return true
}

It work perfect if the field to validate has the function implemented without pointer. Like that:

func (s s) IsValid()

but the conversion to the interface fails if it is implemented like that

func (s *s) IsValid()

How can I improve the customValidation to solve the problem





Aucun commentaire:

Enregistrer un commentaire