Skip to content

Conversation

@aliworkshop
Copy link

hey everyone!

if rules changes like this we can send params to custom messages

AddCustomRule("max", func(field string, rule string, message string, value interface{}) error {
		mustLen := strings.TrimPrefix(rule, "max:")
		lenInt, err := strconv.Atoi(mustLen)
		if err != nil {
			panic(errStringToInt)
		}
		lenFloat, err := strconv.ParseFloat(mustLen, 64)
		if err != nil {
			panic(errStringToFloat)
		}
		errMsg := fmt.Errorf("The %s field value can not be greater than %d", field, lenInt)
		if message != "" {
			errMsg = fmt.Errorf(message, lenInt)
		}
		errMsgFloat := fmt.Errorf("The %s field value can not be greater than %f", field, lenFloat)
		if message != "" {
			errMsgFloat = fmt.Errorf(message, lenFloat)
		}
		rv := reflect.ValueOf(value)
		switch rv.Kind() {
		case reflect.String:
			inLen := rv.Len()
			if inLen > lenInt {
				if message != "" {
					return fmt.Errorf(message, lenInt)
				}
				return fmt.Errorf("The %s field must be maximum %d char", field, lenInt)
			}
...

and use such as this

	messages := govalidator.MapData{
		"title": []string{"required: Is Required"},
		"description":    []string{"required: Is Required","max: must be %d chars maximum"},
		"file:image": []string{"required: Is Required"},
		"file:file": []string{"required: Is Required"},
	}

thedevsaddam and others added 6 commits February 7, 2019 12:58
* Added `ValidateStruct` to validate a struct without a request (#57) (#58)

Fixes #12

* Update MAP_VALIDATION.md

Fixed typo
change format for printf, fix validate uint field type of struct
* Added Mac Address validator

* Fixed typo adress instead of address

* Fixed tests for mac address message error
@coveralls
Copy link

coveralls commented Sep 1, 2019

Coverage Status

Coverage decreased (-0.06%) to 89.256% when pulling fa05c58 on master into 02db365 on dev.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants