Go Struct and Field validation, including Cross Field and Cross Field Cross Struct validation
go-playground/validator
or my own custom validation func that operates on a model struct. are there any recommendations here? It seems like this repo would do the same thing, but then I'd have to come up with error messages to display to the user
go get
this package $ go get github.com/go-playground/validator/v10
cannot find package "github.com/go-playground/validator/v10" in any of:
/usr/local/go/src/github.com/go-playground/validator/v10 (from $GOROOT)
/home/arjunmahishi/go/src/github.com/go-playground/validator/v10 (from $GOPATH)
go get github.com/go-playground/validator
Hello, I'm new to this library and have a question on validation for specific strings.
Question
Which way is good to validate specific string such as some status string defined like this ?
I need to apply the same type validation to multiple places.
const (
StatusSuccess = "success"
StatusFail = "fail"
StatusProcessing = "processing"
)
var validStatusList = []string{
StatusSuccess,
StatusFail,
StatusProcessing,
}
Background
I found 2 ways to do it using struct tag or custom validation.
I'm wondering costome valiation is better for the case we would like to use variables for validation in my current understanding because in struct tag way like the following, the strings needd to be hardcoded in struct tag.
type SomeStruct struct {
status string `form:"status" json:"status" binding:"required,oneof=success fail processing"`
}
alphanum
then alphanumunicode