gin-gonic / gin

Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
https://gin-gonic.com/
MIT License
78.72k stars 8.01k forks source link

gin string validation #1127

Closed alkesh26 closed 3 years ago

alkesh26 commented 7 years ago

I have a use case like this

type IDData struct {
    ID      string `json:"id" binding:"required"`
    Type    string `json:"type" binding:"required"` // this value should be buzz or fizz only
}

type request struct {
    IDS []IDData        `json:"ids" binding:"required,dive"`
    Timespan int64      `json:"timespan" binding:"required"`
}

I need to validate the type value of IDData struct to be only buzz or fizz else throw error. Is there any validator for this case.

deankarn commented 7 years ago

Hello @alkesh26 please see here for 2 examples on how.

I highly recommend upgrading to v9 of validation if not already done, see here for instructions.