Skip to content

Instantly share code, notes, and snippets.

@seregayoga
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seregayoga/b30e48b09b7b32c42eaf to your computer and use it in GitHub Desktop.
Save seregayoga/b30e48b09b7b32c42eaf to your computer and use it in GitHub Desktop.
package main
import (
"gopkg.in/bluesuncorp/validator.v5"
"log"
)
type MyStruct struct {
StringField string `validate:"required"`
BoolField bool `validate:"required"`
}
func main() {
validate := validator.New("validate", validator.BakedInValidators)
myStruct := MyStruct{
StringField: "some string",
BoolField: false,
}
errs := validate.Struct(myStruct)
log.Println(errs)
}
@deankarn
Copy link

It's not really documented anywhere, but the "required" should not be added to a boolean field.
go's boolean is not tristate, because of that it is always set and so "required" is not really necessary

package main

import (
    "gopkg.in/bluesuncorp/validator.v5"
    "log"
)

type MyStruct struct {
    StringField string `validate:"required"`
    BoolField   bool
}

func main() {
    validate := validator.New("validate", validator.BakedInValidators)

    myStruct := MyStruct{
        StringField: "some string",
        BoolField:   false,
    }

    errs := validate.Struct(myStruct)
    log.Println(errs)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment