84 lines
1.7 KiB
Go
Executable File
84 lines
1.7 KiB
Go
Executable File
package svc
|
|
|
|
import (
|
|
"regexp"
|
|
"time"
|
|
|
|
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
|
|
},
|
|
}
|
|
}
|
|
|
|
func WithHTTPURL() vi.Option {
|
|
return vi.Option{
|
|
ValidatorName: "httpurl",
|
|
ValidatorFunc: func(fl validator.FieldLevel) bool {
|
|
value := fl.Field().String()
|
|
// 使用正則檢查是否以 http:// 或 https:// 開頭
|
|
re := regexp.MustCompile(`^https?://`)
|
|
|
|
return re.MatchString(value)
|
|
},
|
|
}
|
|
}
|
|
|
|
func WithRfc3339Format() vi.Option {
|
|
return vi.Option{
|
|
ValidatorName: "rfc3339",
|
|
ValidatorFunc: func(fl validator.FieldLevel) bool {
|
|
// 使用正則檢查是否以 http:// 或 https:// 開頭
|
|
_, err := time.Parse(time.RFC3339, fl.Field().String())
|
|
|
|
return err == nil
|
|
},
|
|
}
|
|
}
|