Files
core/valid/option.go
T

32 lines
867 B
Go
Raw Normal View History

package valid
// Option configures a Validator.
type Option func(*config)
// WithMessageProvider sets a custom MessageProvider.
// Default: DefaultMessages (English).
func WithMessageProvider(mp MessageProvider) Option {
return func(c *config) {
c.mp = mp
}
}
// WithCustomValidator registers a custom validation tag and its function.
// The tag can then be used in struct tags: validate:"mytag" or validate:"mytag=param".
// Panics if registration fails (empty tag or tag that conflicts with a built-in).
func WithCustomValidator(tag string, fn func(FieldLevel) bool) Option {
return func(c *config) {
c.customValidators = append(c.customValidators, customValidator{tag: tag, fn: fn})
}
}
type customValidator struct {
tag string
fn func(FieldLevel) bool
}
type config struct {
mp MessageProvider
customValidators []customValidator
}