validator icon indicating copy to clipboard operation
validator copied to clipboard

Skip validation for slice of struct for all index using structExcept()

Open jaimeenkpatel opened this issue 4 years ago • 3 comments

Package version v9:

I am using structExcept() to exclude certain validation. In below code snippet, I want to exclude validation on SessionInfo for all index elements of VenueInfo when SessionInfo is nil. Same way, I want to exclude validation on TicketDetails for all elements of SessionInfo for all elements of VenueInfo.

Issue, Question or Enhancement:

Currently, the only possible way to exclude from validation is by pass all the index value for SessionInfo and TicketDetails. Is there any better approach?

Code sample, to showcase or reproduce:

I have share a code snippet of the uses cases.

Use case 1: skip validation of VenueInfo all together - this worked. Use case 2: skip validation of SessionInfo for all index, validate only venueName. Use case 3: skip validation of TicketInfo for all index, validate only venueName, ShowTime for all index of VenueInfo & SessionInfo

package main

import (
    "fmt"
    "time"

    "gopkg.in/go-playground/validator.v9"
)

type (
    // Event model
    Event struct {
        VenueInfo []VenueDetails `json:"venueInfo,omitempty" validate:"min=1,dive,required"`
    }

    // TicketDetails model
    TicketDetails struct {
        TicketTypeName string `json:"ticketTypeName,omitempty" validate:"required"`
    }

    // SessionDetails model
    SessionDetails struct {
        ShowTime   time.Time       `json:"showTime,omitempty" validate:"required"`
        TicketInfo []TicketDetails `json:"ticketInfo,omitempty" validate:"required_with_all=ShowTime,min=1,dive"`
    }

    // VenueDetails model
    VenueDetails struct {
        VenueName   string           `json:"venueName,omitempty" validate:"required"`
        SessionInfo []SessionDetails `json:"sessionInfo,omitempty" validate:"required_with_all=Name,min=1,dive"`
    }
)

func main() {
    validate := validator.New()
    eventDetails := &Event{
        VenueInfo: []VenueDetails{
            {
                VenueName: "Bengaluru International Exhibition Centre",
                SessionInfo: []SessionDetails{
                    {
                        ShowTime: time.Now(),
                        // TicketInfo: []TicketDetails{
                        //  {
                        //      TicketTypeName: "Gold Class",
                        //  },
                        // },
                    },
                },
            },
            {
                VenueName: "Bengaluru International Exhibition Centre",
                SessionInfo: []SessionDetails{
                    {
                        ShowTime: time.Now(),
                        // TicketInfo: []TicketDetails{
                        //  {
                        //      TicketTypeName: "Gold Class",
                        //  },
                        // },
                    },
                },
            },
        },
    }

    // 1st use case : applying excluding validation for VenueInfo
    excludeVenueInfo := []string{"VenueInfo"}
    if err := validate.StructExcept(eventDetails, excludeVenueInfo...); err != nil {
        fmt.Println(err.Error())
    } else {
        fmt.Println("Success")
    }

    // 2nd use case : applying excluding validation for SessionInfo & TicketInfo
    excludeSessionInfo := []string{"VenueInfo[0].SessionInfo"}
    if err := validate.StructExcept(eventDetails, excludeSessionInfo...); err != nil {
        fmt.Println(err.Error())
    } else {
        fmt.Println("Success")
    }

    // 3rd use case : applying excluding validation for TicketInfo
    excludeTicketInfo := []string{"VenueInfo[0].SessionInfo[0].TicketInfo"}
    if err := validate.StructExcept(eventDetails, excludeTicketInfo...); err != nil {
        fmt.Println(err.Error())
    } else {
        fmt.Println("Success")
    }
}

jaimeenkpatel avatar Sep 06 '20 15:09 jaimeenkpatel

@jaimeenkpatel I would be great if the example could be simplified into one use case. I'm not 100% sue I'm growing what you want.

Are you saying that you want to skip validation for all VenueInfo where. single nested SessionInfo is null?

deankarn avatar Sep 27 '20 23:09 deankarn

It is something like if i say skip VenueInfo, it should skip for all element of the array. similarly, it i say skip SessionInfo, it should skip for all element of array irrespective it SessionInfo is nil or not.

Currently, i need to explicitly need to pass fully qualified path for every index element. It cannot scale if i have 100 elements.

jaimeenkpatel avatar Sep 28 '20 14:09 jaimeenkpatel

There are some news about this?

leandrorebelo avatar Sep 14 '23 16:09 leandrorebelo