Go Struct and Field validation, including Cross Field and Cross Field Cross Struct validation
context.Context
and then use one of the validation functions that accept it eg. validate.StructCtx
, but that's if it's dynamic, if looking to embed static logic from the application see here for an example go-playground/validator#462
Documentation can be found here with all tags https://godoc.org/gopkg.in/go-playground/validator.v9
At present there is no built in "Money" nor "times" validator due to different locales having to validate differently, but you can create your own validations :)
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