Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data validation-validation rules-confusing required-if #3441

Closed
cococolanosugar opened this issue Mar 31, 2024 · 3 comments
Closed

Data validation-validation rules-confusing required-if #3441

cococolanosugar opened this issue Mar 31, 2024 · 3 comments
Labels
done This issue is done, which may be release in next version. enhancement help wanted planned This issue/proposal is planned into our next steps.

Comments

@cococolanosugar
Copy link
Contributor

required-if的困惑

关于required-if校验规则:

中文文档中说明为:
必需参数(当任意所给定字段值与所给值相等时,即:当field字段的值为value时,当前验证字段为必须参数)。多个字段以,号分隔。

而其英文注释为:
Required unless all given field and its value are equal.

而其代码实现为:

// 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/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

// RuleRequiredIf implements `required-if` rule:
// Required unless all given field and its value are equal.
//
// Format:  required-if:field,value,...
// Example: required-if: id,1,age,18
type RuleRequiredIf struct{}

func init() {
	Register(RuleRequiredIf{})
}

func (r RuleRequiredIf) Name() string {
	return "required-if"
}

func (r RuleRequiredIf) Message() string {
	return "The {field} field is required"
}

func (r RuleRequiredIf) Run(in RunInput) error {
	var (
		required   = false
		array      = strings.Split(in.RulePattern, ",")
		foundValue interface{}
	)
	// It supports multiple field and value pairs.
	if len(array)%2 == 0 {
		for i := 0; i < len(array); {
			tk := array[i]
			tv := array[i+1]
			_, foundValue = gutil.MapPossibleItemByKey(in.Data.Map(), tk)
			if in.Option.CaseInsensitive {
				required = strings.EqualFold(tv, gconv.String(foundValue))
			} else {
				required = strings.Compare(tv, gconv.String(foundValue)) == 0
			}
			if required {
				break
			}
			i += 2
		}
	}

	if required && isRequiredEmpty(in.Value.Val()) {
		return errors.New(in.Message)
	}
	return nil
}

综合看起来,代码实现跟中文说明相符,而与英文注释不符。

同理required-unless也有相应confuse!

建议:

将required-if拆分为多个更易于理解的规则,例如:

required-if-all-eq: 如果所有条件符合,则该字段必须;
required-if-any-eq: 如果任一条件符合,则该字段必须;

@Issues-translate-bot Issues-translate-bot changed the title 数据校验-校验规则-令人迷惑的required-if Data validation-validation rules-confusing required-if Mar 31, 2024
@cococolanosugar
Copy link
Contributor Author

I make a pr 3455, please have a look.

@gqcn gqcn added enhancement help wanted planned This issue/proposal is planned into our next steps. and removed question labels Apr 7, 2024
Copy link

github-actions bot commented Apr 7, 2024

Hello @cococolanosugar. We like your proposal/feedback and would appreciate a contribution via a Pull Request by you or another community member. We thank you in advance for your contribution and are looking forward to reviewing it!
你好 @cococolanosugar。我们喜欢您的提案/反馈,并希望您或其他社区成员通过拉取请求做出贡献。我们提前感谢您的贡献,并期待对其进行审查。

@gqcn
Copy link
Member

gqcn commented Apr 7, 2024

@cococolanosugar It seems that the comments of the function is incorrect.

@gqcn gqcn added the done This issue is done, which may be release in next version. label Sep 19, 2024
@gqcn gqcn closed this as completed Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
done This issue is done, which may be release in next version. enhancement help wanted planned This issue/proposal is planned into our next steps.
Projects
None yet
Development

No branches or pull requests

2 participants