简体   繁体   中英

Golang Validator : structExcept

I am trying to validate a golang struct based on some criteria. I am using this library validator.v9 . I am using structExcept() to exclude certain validation. 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.

Sample Code Snippet.

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")
    }
}

Use omitempty in your validation.

package main

import (
    "fmt"
    "gopkg.in/go-playground/validator.v9"
)

type (

    AlienObject struct {
        A string `json:"a,omitempty" validate:"omitempty,min=6"`
        B string `json:"b,omitempty" validate:"omitempty,min=6"`
    }
)

func main() {
    validate := validator.New()
    h := AlienObject{A:"abcdooii"}
    if err := validate.Struct(h); err != nil {
        fmt.Println(err.Error())
    } else {
        fmt.Println("Success")
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM