Skip to content

Trying to override "required" tag validation but doesn't work #1019

Answered by andriantonee
andriantonee asked this question in Q&A
Discussion options

You must be logged in to vote

after i check baked_in.go, i finally understand why required tag is listed as restrictedTags.

func hasValue(fl FieldLevel) bool {
	field := fl.Field()
	switch field.Kind() {
	case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
		return !field.IsNil()
	default:
		if fl.(*validate).fldIsPointer && field.Interface() != nil {
			return true
		}
		return field.IsValid() && field.Interface() != reflect.Zero(field.Type()).Interface()
	}
}

it use private struct fl.(*validate).fldIsPointer to validate.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by andriantonee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant