Skip to content

Omitempty on a *string field does not work #698

Closed
@frederikhors

Description

@frederikhors

This is a simple example I cannot make it work and I don't know why!

All what I need is to omit validation (min=10) when Name is empty.

Why isn't it working? Where am I wrong? Maybe because it's a pointer to string?

Reproduction

https://play.golang.org/p/LZWMVjVERYk

Code sample

package main

import (
	"log"

	"github.com/go-playground/validator/v10"
)

type SampleWithPointer struct {
	Name *string `validate:"omitempty,min=10"`
}

func main() {
	name := ""

	swp := &SampleWithPointer{Name: &name}

	validate := validator.New()

	if err := validate.Struct(swp); err != nil {
		log.Printf("Validate SampeWithPointer with error, %v", err)
	}
}

Error

Validate SampeWithPointer with error, Key: 'SampleWithPointer.Name' Error:Field validation for 'Name' failed on the 'min' tag

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