Skip to content

Panic Bad field type *string, when using required_without and omitempty at the same time #654

Closed
@nigelis

Description

@nigelis

Package version eg. v8, v9:

v10.3.0

Issue, Question, or Enhancement:

When validating a string pointer with required_without and omitempty at the same time, calling Struct() leads to panic.

Using omitempty merely is ok. Validating a string instead of a string pointer is ok.

Code sample, to showcase or reproduce:

package main

import (
	"log"

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

type Sample struct {
	Name string `validate:"omitempty,max=1024"`
	ID   string `validate:"required_without=Name,omitempty,max=64"`
}

type SampleWithPointer struct {
	Name *string `validate:"omitempty,max=1024"`
	ID   *string `validate:"required_without=Name,omitempty,max=64"`
}

func main() {
	name := "name"

	s := &Sample{Name: name}
	swp := &SampleWithPointer{Name: &name}

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

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