-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new rule
required-if-all
for package gvalid (#3455)
- Loading branch information
1 parent
2523889
commit 91f449d
Showing
4 changed files
with
134 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// 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 builtin | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
"github.com/gogf/gf/v2/errors/gcode" | ||
"github.com/gogf/gf/v2/errors/gerror" | ||
"github.com/gogf/gf/v2/util/gconv" | ||
"github.com/gogf/gf/v2/util/gutil" | ||
) | ||
|
||
// RuleRequiredIfAll implements `required-if-all` rule: | ||
// Required if all given field and its value are equal. | ||
// | ||
// Format: required-if-all:field,value,... | ||
// Example: required-if-all:id,1,age,18 | ||
type RuleRequiredIfAll struct{} | ||
|
||
func init() { | ||
Register(RuleRequiredIfAll{}) | ||
} | ||
|
||
func (r RuleRequiredIfAll) Name() string { | ||
return "required-if-all" | ||
} | ||
|
||
func (r RuleRequiredIfAll) Message() string { | ||
return "The {field} field is required" | ||
} | ||
|
||
func (r RuleRequiredIfAll) Run(in RunInput) error { | ||
var ( | ||
required = true | ||
array = strings.Split(in.RulePattern, ",") | ||
foundValue interface{} | ||
dataMap = in.Data.Map() | ||
) | ||
if len(array)%2 != 0 { | ||
return gerror.NewCodef( | ||
gcode.CodeInvalidParameter, | ||
`invalid "%s" rule pattern: %s`, | ||
r.Name(), | ||
in.RulePattern, | ||
) | ||
} | ||
for i := 0; i < len(array); { | ||
var ( | ||
tk = array[i] | ||
tv = array[i+1] | ||
eq bool | ||
) | ||
_, foundValue = gutil.MapPossibleItemByKey(dataMap, tk) | ||
if in.Option.CaseInsensitive { | ||
eq = strings.EqualFold(tv, gconv.String(foundValue)) | ||
} else { | ||
eq = strings.Compare(tv, gconv.String(foundValue)) == 0 | ||
} | ||
if !eq { | ||
required = false | ||
break | ||
} | ||
i += 2 | ||
} | ||
if required && isRequiredEmpty(in.Value.Val()) { | ||
return errors.New(in.Message) | ||
} | ||
return nil | ||
} |