Skip to content

How to validate string is either nil or longer than 0 #1454

@evgeniy-ivlev-archie

Description

@evgeniy-ivlev-archie

Hello.

I need the string to either be nil or longer than 0.

func init() {
	validate = validator.New()

	validate.RegisterValidation("nil_or_non_empty_string", func(fl validator.FieldLevel) bool {
                // NOTHING WORKS HERE
                // I CANNOT DETECT NIL POINTER
         
		v := fl.Field()

		if v.IsNil() {
			return true
		}

		if v.String() != "" {
			return true
		}

		return false
        }
}

Can someone please help me?
Thanks kindly

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions