app-cloudep-trade-service/internal/svc/validator.go

56 lines
1.1 KiB
Go
Raw Permalink Normal View History

2024-10-25 16:42:58 +00:00
package svc
import (
vi "code.30cm.net/digimon/library-go/validator"
"github.com/go-playground/validator/v10"
"github.com/shopspring/decimal"
)
// WithDecimalGt 是否大於等於
func WithDecimalGt() vi.Option {
return vi.Option{
ValidatorName: "decimalGt",
ValidatorFunc: func(fl validator.FieldLevel) bool {
if val, ok := fl.Field().Interface().(string); ok {
value, err := decimal.NewFromString(val)
if err != nil {
return false
}
conditionValue, err := decimal.NewFromString(fl.Param())
if err != nil {
return false
}
return value.GreaterThan(conditionValue)
}
return true
},
}
}
// WithDecimalGte 是否大於等於
func WithDecimalGte() vi.Option {
return vi.Option{
ValidatorName: "decimalGte",
ValidatorFunc: func(fl validator.FieldLevel) bool {
if val, ok := fl.Field().Interface().(string); ok {
value, err := decimal.NewFromString(val)
if err != nil {
return false
}
conditionValue, err := decimal.NewFromString(fl.Param())
if err != nil {
return false
}
return value.GreaterThanOrEqual(conditionValue)
}
return true
},
}
}