mirror of
https://github.com/gogf/gf.git
synced 2025-04-05 11:18:50 +08:00
parent
45e4c9e16c
commit
ae86f66545
@ -27,6 +27,9 @@ type RuleFuncInput struct {
|
||||
// Message specifies the custom error message or configured i18n message for this rule.
|
||||
Message string
|
||||
|
||||
// Field specifies the field for this rule to validate.
|
||||
Field string
|
||||
|
||||
// Value specifies the value for this rule to validate.
|
||||
Value *gvar.Var
|
||||
|
||||
|
@ -145,17 +145,13 @@ func (v *Validator) doCheckValue(ctx context.Context, in doCheckValueInput) Erro
|
||||
switch {
|
||||
// Custom validation rules.
|
||||
case customRuleFunc != nil:
|
||||
if err = customRuleFunc(ctx, RuleFuncInput{
|
||||
err = customRuleFunc(ctx, RuleFuncInput{
|
||||
Rule: ruleItems[index],
|
||||
Message: message,
|
||||
Field: in.Name,
|
||||
Value: gvar.New(value),
|
||||
Data: gvar.New(in.DataRaw),
|
||||
}); err != nil {
|
||||
// The error should have stack info to indicate the error position.
|
||||
if !gerror.HasStack(err) {
|
||||
err = gerror.NewCodeSkip(gcode.CodeValidationFailed, 1, err.Error())
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Builtin validation rules.
|
||||
case customRuleFunc == nil && builtinRule != nil:
|
||||
@ -177,24 +173,26 @@ func (v *Validator) doCheckValue(ctx context.Context, in doCheckValueInput) Erro
|
||||
|
||||
// Error handling.
|
||||
if err != nil {
|
||||
// The error should have error code that is `gcode.CodeValidationFailed`.
|
||||
if gerror.Code(err) == gcode.CodeNil {
|
||||
if e, ok := err.(*gerror.Error); ok {
|
||||
e.SetCode(gcode.CodeValidationFailed)
|
||||
}
|
||||
}
|
||||
|
||||
// Error variable replacement for error message.
|
||||
if !gerror.HasStack(err) {
|
||||
var s string
|
||||
s = gstr.ReplaceByMap(err.Error(), map[string]string{
|
||||
if errMsg := err.Error(); gstr.Contains(errMsg, "{") {
|
||||
errMsg = gstr.ReplaceByMap(errMsg, map[string]string{
|
||||
"{field}": in.Name, // Field name of the `value`.
|
||||
"{value}": gconv.String(value), // Current validating value.
|
||||
"{pattern}": rulePattern, // The variable part of the rule.
|
||||
"{attribute}": in.Name, // The same as `{field}`. It is deprecated.
|
||||
})
|
||||
s, _ = gregex.ReplaceString(`\s{2,}`, ` `, s)
|
||||
err = errors.New(s)
|
||||
errMsg, _ = gregex.ReplaceString(`\s{2,}`, ` `, errMsg)
|
||||
err = errors.New(errMsg)
|
||||
}
|
||||
// The error should have stack info to indicate the error position.
|
||||
if !gerror.HasStack(err) {
|
||||
err = gerror.NewCode(gcode.CodeValidationFailed, err.Error())
|
||||
}
|
||||
// The error should have error code that is `gcode.CodeValidationFailed`.
|
||||
if gerror.Code(err) == gcode.CodeNil {
|
||||
if e, ok := err.(*gerror.Error); ok {
|
||||
e.SetCode(gcode.CodeValidationFailed)
|
||||
}
|
||||
}
|
||||
ruleErrorMap[ruleKey] = err
|
||||
|
||||
|
@ -288,3 +288,25 @@ func Test_CustomRule_Overwrite(t *testing.T) {
|
||||
})
|
||||
g.Dump(gvalid.GetRegisteredRuleMap())
|
||||
}
|
||||
|
||||
func Test_Issue2499(t *testing.T) {
|
||||
ruleName := "required"
|
||||
ruleFunc := func(ctx context.Context, in gvalid.RuleFuncInput) error {
|
||||
return errors.New(in.Message)
|
||||
}
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type T struct {
|
||||
Value string `v:"uid@required"`
|
||||
Data string `p:"data"`
|
||||
}
|
||||
st := &T{
|
||||
Value: "",
|
||||
Data: "123456",
|
||||
}
|
||||
err := g.Validator().
|
||||
RuleFuncMap(map[string]gvalid.RuleFunc{
|
||||
ruleName: ruleFunc,
|
||||
}).Data(st).Run(ctx)
|
||||
t.Assert(err.String(), `The uid field is required`)
|
||||
})
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user