sflags: github.com/octago/sflags/validator/govalidator Index | Examples | Files

package govalidator

import "github.com/octago/sflags/validator/govalidator"

Package govalidator adds support for govalidator library. Part of this package was taken from govalidator private api and covered by MIT license.

The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Index

Examples

Package Files

govalidator.go

func New Uses

func New() func(val string, field reflect.StructField, obj interface{}) error

New returns ValidateFunc for govalidator library. Supports default String validators in TagMap and ParamTagMap. Doesn't support custom type validators and required filters. Please check all available functions at https://github.com/asaskevich/govalidator.

Code:

type config struct {
    Host string `valid:"host"`
    Port int    `valid:"port"`
}
cfg := &config{
    Host: "127.0.0.1",
    Port: 6000,
}
// Use gflags.ParseToDef if you want default `flag.CommandLine`
fs, err := gflag.Parse(cfg, sflags.Validator(New()))
if err != nil {
    log.Fatalf("err: %v", err)
}
fs.Init("text", flag.ContinueOnError)
fs.SetOutput(ioutil.Discard)

// if we pass a wrong domain to the host flag, we'll get a error.
if err = fs.Parse([]string{"-host", "wrong domain"}); err != nil {
    fmt.Printf("err: %v\n", err)
}
// if we pass a wrong port to the port flag, we'll get a error.
if err = fs.Parse([]string{"-port", "800000"}); err != nil {
    fmt.Printf("err: %v\n", err)
}

Output:

err: invalid value "wrong domain" for flag -host: `wrong domain` does not validate as host
err: invalid value "800000" for flag -port: `800000` does not validate as port

Package govalidator imports 5 packages (graph) and is imported by 1 packages. Updated 2016-12-28. Refresh now. Tools for package owners. This is an inactive package (no imports and no commits in at least two years).