validator

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2019 License: Apache-2.0 Imports: 11 Imported by: 2

README

go-validator

GoDoc

Package validator provides common validation helpers commonly used in operations tools. Additionally structures can be marked up with tags indicating the validation of individual keys and the entire struct can be validated in one go

Documentation

Overview

Package validator provides common validation helpers commonly used in operations tools. Additionally structures can be marked up with tags indicating the validation of individual keys and the entire struct can be validated in one go

Example (Enum)
package main

import (
	"fmt"

	"github.com/choria-io/go-validator/enum"
)

func main() {
	valid := []string{"one", "two", "three"}

	ok, err := enum.ValidateSlice([]string{"one", "two"}, valid)
	if !ok {
		panic(err)
	}

	fmt.Println("slice 1 is valid")

	ok, err = enum.ValidateSlice([]string{"5", "6"}, valid)
	if !ok {
		fmt.Println("slice 2 is invalid")
	}

	// string is valid
	ok, err = enum.ValidateString("one", valid)
	if !ok {
		panic(err)
	}

	fmt.Println("string is valid")

}
Output:

slice 1 is valid
slice 2 is invalid
string is valid
Example (Maxlength)
package main

import (
	"fmt"

	"github.com/choria-io/go-validator/maxlength"
)

func main() {
	ok, err := maxlength.ValidateString("a short string", 20)
	if !ok {
		panic(err)
	}

	fmt.Println("string validates")

}
Output:

string validates
Example (Shellsafe)
package main

import (
	"fmt"

	"github.com/choria-io/go-validator/shellsafe"
)

func main() {
	// a sell safe command, unsafe might be `un > safe`
	ok, err := shellsafe.Validate("safe")
	if !ok {
		panic(err)
	}

	fmt.Printf("string is safe")
}
Output:

string is safe
Example (Struct)
package main

import (
	"fmt"

	validator "github.com/choria-io/go-validator"
)

type Request struct {
	Command string   `validate:"shellsafe"`
	Flags   []string `validate:"enum=debug,verbose"`
	Args    string   `validate:"maxlength=128"`
	AnyIP   string   `validate:"ipaddress"` // can also check ipv4 and ipv6
	User    string   `validate:"regex=^\\w+$"`
}

func main() {
	r := Request{
		Command: "/bin/some/command",
		Flags:   []string{"debug"},
		Args:    "hello world",
		AnyIP:   "2a00:1450:4003:807::200e",
		User:    "bob",
	}

	ok, err := validator.ValidateStruct(r)
	if !ok {
		panic(err)
	}

	fmt.Println("valid request")

	ok, err = validator.ValidateStructField(r, "Command")
	if !ok {
		panic(err)
	}

	fmt.Println("valid field")

}
Output:

valid request
valid field

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateStruct

func ValidateStruct(target interface{}) (bool, error)

ValidateStruct validates all keys in a struct using their validate tag

func ValidateStructField

func ValidateStructField(target interface{}, field string) (bool, error)

ValidateStructField validates one field in a struct

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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