2021-01-12 10:46:39 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
|
|
package gvalid
|
|
|
|
|
|
2021-05-13 00:16:45 +08:00
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"github.com/gogf/gf/i18n/gi18n"
|
|
|
|
|
)
|
2021-05-11 19:20:39 +08:00
|
|
|
|
2021-01-12 10:46:39 +08:00
|
|
|
// Validator is the validation manager.
|
|
|
|
|
type Validator struct {
|
2021-05-13 00:16:45 +08:00
|
|
|
ctx context.Context // Context containing custom context variables.
|
|
|
|
|
i18nManager *gi18n.Manager // I18n manager for error message translation.
|
|
|
|
|
|
2021-01-12 10:46:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// New creates and returns a new Validator.
|
|
|
|
|
func New() *Validator {
|
2021-05-13 00:16:45 +08:00
|
|
|
return &Validator{
|
2021-05-13 20:56:52 +08:00
|
|
|
ctx: context.TODO(), // Initialize an empty context.
|
|
|
|
|
i18nManager: gi18n.Instance(), // Use default i18n manager.
|
2021-05-13 00:16:45 +08:00
|
|
|
}
|
2021-01-12 10:46:39 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-13 00:16:45 +08:00
|
|
|
// I18n sets the i18n manager for the validator.
|
|
|
|
|
func (v *Validator) I18n(i18nManager *gi18n.Manager) *Validator {
|
|
|
|
|
v.i18nManager = i18nManager
|
|
|
|
|
return v
|
2021-01-12 10:46:39 +08:00
|
|
|
}
|
2021-05-11 19:20:39 +08:00
|
|
|
|
|
|
|
|
// Ctx is a chaining operation function which sets the context for next validation.
|
|
|
|
|
func (v *Validator) Ctx(ctx context.Context) *Validator {
|
2021-05-13 00:16:45 +08:00
|
|
|
v.ctx = ctx
|
|
|
|
|
return v
|
2021-05-11 19:20:39 +08:00
|
|
|
}
|