govalidator

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 29, 2016 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

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.

Example
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

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL