validator
validator copied to clipboard
Validate type shopspring/decimal custom type function not called
Package version eg. v8, v9:
v9
Issue, Question or Enhancement:
I'm using shopspring/decimal for decimal type on my struct. I know I can use RegisterCustomTypeFunc to register function to return float64 value for decimal.Decimal type. However, after hours of troubleshooting, I can't get it to work, even the custom function is not called at all. All validation rules are ignored with no error
On the example for sql.Nullxxx type, it's working fine. After investigating, it appears that extractTypeInternal function on util.js can't find matching customFunc type, even though it is there. Any idea and suggestion is appreciated. Thank you
Code sample, to showcase or reproduce:
package main
import (
"fmt"
"reflect"
"github.com/shopspring/decimal"
"gopkg.in/go-playground/validator.v9"
)
type decimalObject struct {
debug decimal.Decimal `validate:"required"`
}
func main() {
validate := validator.New()
validate.RegisterCustomTypeFunc(ValidateDecimalType, decimal.Decimal{})
sample, _ := decimal.NewFromString("2.5")
model := decimalObject {
debug: sample,
}
err := validate.Struct(model)
if err != nil {
fmt.Printf("Err(s):\n%+v\n", err)
}
}
func ValidateDecimalType(field reflect.Value) interface{} {
fmt.Println("validate decimal")
fieldDecimal, ok := field.Interface().(decimal.Decimal)
if ok {
fmt.Println("is decimal", fieldDecimal)
value, _ := fieldDecimal.Float64()
return value
}
return nil
}
@kenny26 @tawoli The reason the field is skipped is because it's unexposed debug
vs Debug
try this https://github.com/go-playground/validator/issues/515#issuecomment-1235164600
@kenny26 Thank you your question. Were you able to resolve the issue with the feedback provided? For now, we're going to close this issue but please feel free to reach out if you have any additional questions.